Skip to content

Commit 6e5fa32

Browse files
Merge pull request #33 from MartinZikmund/feature/aoc2025-day7
2 parents b06e8d0 + 06f765b commit 6e5fa32

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace AdventOfCode.Puzzles._2025._07.Part1;
2+
3+
public class AoC2025Day7Part1 : IPuzzleSolution
4+
{
5+
private int _height;
6+
private int _width;
7+
private char[,] _map;
8+
private HashSet<Point> _splitters;
9+
10+
public async Task<string> SolveAsync(StreamReader inputReader)
11+
{
12+
var lines = await inputReader.ReadAllLinesAsync();
13+
_height = lines.Count;
14+
_width = lines[0].Length;
15+
_map = new char[lines[0].Length, lines.Count];
16+
Point startingPoint = new Point();
17+
for (var row = 0; row < lines.Count; row++)
18+
{
19+
var line = lines[row];
20+
for (var column = 0; column < line.Length; column++)
21+
{
22+
_map[column, row] = line[column];
23+
if (line[column] == 'S')
24+
{
25+
startingPoint = new Point(column, row);
26+
}
27+
}
28+
}
29+
30+
_splitters = new HashSet<Point>();
31+
32+
Beam(startingPoint);
33+
34+
return _splitters.Count.ToString();
35+
}
36+
37+
private void Beam(Point currentPoint)
38+
{
39+
if (_map[currentPoint.X, currentPoint.Y] is '.' or 'S')
40+
{
41+
if (currentPoint.Y + 1 < _height)
42+
{
43+
Beam(new(currentPoint.X, currentPoint.Y + 1));
44+
}
45+
}
46+
else
47+
{
48+
if (_splitters.Contains(currentPoint))
49+
{
50+
return;
51+
}
52+
53+
_splitters.Add(currentPoint);
54+
if (currentPoint.X - 1 >= 0)
55+
{
56+
Beam(new(currentPoint.X - 1, currentPoint.Y));
57+
}
58+
if (currentPoint.X + 1 < _width)
59+
{
60+
Beam(new(currentPoint.X + 1, currentPoint.Y));
61+
}
62+
}
63+
}
64+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace AdventOfCode.Puzzles._2025._07.Part2;
2+
3+
public class AoC2025Day7Part2 : IPuzzleSolution
4+
{
5+
private int _height;
6+
private int _width;
7+
private char[,] _map;
8+
private long[,] _paths;
9+
10+
public async Task<string> SolveAsync(StreamReader inputReader)
11+
{
12+
var lines = await inputReader.ReadAllLinesAsync();
13+
_height = lines.Count;
14+
_width = lines[0].Length;
15+
_map = new char[lines[0].Length, lines.Count];
16+
_paths = new long[lines[0].Length, lines.Count];
17+
Point startingPoint = new Point();
18+
for (var row = 0; row < lines.Count; row++)
19+
{
20+
var line = lines[row];
21+
for (var column = 0; column < line.Length; column++)
22+
{
23+
_map[column, row] = line[column];
24+
if (line[column] == 'S')
25+
{
26+
startingPoint = new Point(column, row);
27+
}
28+
}
29+
}
30+
31+
return CountPaths(startingPoint).ToString();
32+
}
33+
34+
private long CountPaths(Point currentPoint)
35+
{
36+
if (_map[currentPoint.X, currentPoint.Y] is '.' or 'S')
37+
{
38+
if (currentPoint.Y + 1 < _height)
39+
{
40+
if (_paths[currentPoint.X, currentPoint.Y + 1] == 0)
41+
{
42+
_paths[currentPoint.X, currentPoint.Y + 1] = CountPaths(new(currentPoint.X, currentPoint.Y + 1));
43+
}
44+
45+
return _paths[currentPoint.X, currentPoint.Y + 1];
46+
}
47+
else
48+
{
49+
return 1;
50+
}
51+
}
52+
else
53+
{
54+
if (currentPoint.X - 1 >= 0 && _paths[currentPoint.X - 1, currentPoint.Y] == 0)
55+
{
56+
_paths[currentPoint.X - 1, currentPoint.Y] = CountPaths(new(currentPoint.X - 1, currentPoint.Y));
57+
}
58+
if (currentPoint.X + 1 < _width && _paths[currentPoint.X + 1, currentPoint.Y] == 0)
59+
{
60+
_paths[currentPoint.X + 1, currentPoint.Y] = CountPaths(new(currentPoint.X + 1, currentPoint.Y));
61+
}
62+
63+
return _paths[currentPoint.X - 1, currentPoint.Y] + _paths[currentPoint.X + 1, currentPoint.Y];
64+
}
65+
}
66+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.......S.......
2+
...............
3+
.......^.......
4+
...............
5+
......^.^......
6+
...............
7+
.....^.^.^.....
8+
...............
9+
....^.^...^....
10+
...............
11+
...^.^...^.^...
12+
...............
13+
..^...^.....^..
14+
...............
15+
.^.^.^.^.^...^.
16+
...............

0 commit comments

Comments
 (0)