Skip to content

Commit 6a5edfa

Browse files
Merge pull request #30 from MartinZikmund/feature/aoc2025-day4
2 parents 2220f4c + d2dd831 commit 6a5edfa

4 files changed

Lines changed: 152 additions & 1 deletion

File tree

src/AdventOfCode.Puzzles/2025/03/Part2/AoC2025Day3Part2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AdventOfCode.Puzzles._2025._03.Part2;
22

3-
public class AoC2025Day3Part2 : IPuzzleSolution
3+
public class AoC2025Day4Part2 : IPuzzleSolution
44
{
55
public async Task<string> SolveAsync(StreamReader inputReader)
66
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
namespace AdventOfCode.Puzzles._2025._04.Part1;
3+
4+
public class AoC2025Day4Part1 : IPuzzleSolution
5+
{
6+
private const char Roll = '@';
7+
private char[,] _map;
8+
private int _width;
9+
private int _height;
10+
11+
public async Task<string> SolveAsync(StreamReader inputReader)
12+
{
13+
var lines = await inputReader.ReadAllLinesAsync();
14+
_width = lines[0].Length;
15+
_height = lines.Count;
16+
_map = new char[lines[0].Length, lines.Count];
17+
for (int x = 0; x < _width; x++)
18+
{
19+
for (int y = 0; y < _height; y++)
20+
{
21+
_map[x, y] = lines[y][x];
22+
}
23+
}
24+
25+
int movable = 0;
26+
for (int y = 0; y < _height; y++)
27+
{
28+
for (int x = 0; x < _width; x++)
29+
{
30+
if (_map[x, y] == Roll && IsMovable(x, y))
31+
{
32+
movable++;
33+
}
34+
}
35+
}
36+
37+
return movable.ToString();
38+
}
39+
40+
private bool IsMovable(int x, int y)
41+
{
42+
var adjacentRolls = 0;
43+
foreach (var direction in Directions.WithDiagonals)
44+
{
45+
var neighborPosition = new Point(x + direction.X, y + direction.Y);
46+
if (neighborPosition.X < 0 || neighborPosition.X >= _width ||
47+
neighborPosition.Y < 0 || neighborPosition.Y >= _height)
48+
{
49+
continue;
50+
}
51+
52+
if (_map[neighborPosition.X, neighborPosition.Y] == Roll)
53+
{
54+
adjacentRolls++;
55+
}
56+
}
57+
58+
return adjacentRolls < 4;
59+
}
60+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
namespace AdventOfCode.Puzzles._2025._04.Part2;
2+
3+
public class AoC2025Day4Part2 : IPuzzleSolution
4+
{
5+
private const char Roll = '@';
6+
private char[,] _map;
7+
private int _width;
8+
private int _height;
9+
10+
public async Task<string> SolveAsync(StreamReader inputReader)
11+
{
12+
var lines = await inputReader.ReadAllLinesAsync();
13+
_width = lines[0].Length;
14+
_height = lines.Count;
15+
_map = new char[lines[0].Length, lines.Count];
16+
for (int x = 0; x < _width; x++)
17+
{
18+
for (int y = 0; y < _height; y++)
19+
{
20+
_map[x, y] = lines[y][x];
21+
}
22+
}
23+
24+
var total = 0;
25+
while (true)
26+
{
27+
var movablePositions = FindMovable();
28+
if (movablePositions.Length == 0)
29+
{
30+
break;
31+
}
32+
33+
total += movablePositions.Length;
34+
35+
foreach (var pos in movablePositions)
36+
{
37+
_map[pos.X, pos.Y] = '.';
38+
}
39+
}
40+
41+
return total.ToString();
42+
}
43+
44+
private Point[] FindMovable()
45+
{
46+
var positions = new List<Point>();
47+
for (int y = 0; y < _height; y++)
48+
{
49+
for (int x = 0; x < _width; x++)
50+
{
51+
if (_map[x, y] == Roll && IsMovable(x, y))
52+
{
53+
positions.Add(new Point(x, y));
54+
}
55+
}
56+
}
57+
58+
return positions.ToArray();
59+
}
60+
61+
private bool IsMovable(int x, int y)
62+
{
63+
var adjacentRolls = 0;
64+
foreach (var direction in Directions.WithDiagonals)
65+
{
66+
var neighborPosition = new Point(x + direction.X, y + direction.Y);
67+
if (neighborPosition.X < 0 || neighborPosition.X >= _width ||
68+
neighborPosition.Y < 0 || neighborPosition.Y >= _height)
69+
{
70+
continue;
71+
}
72+
73+
if (_map[neighborPosition.X, neighborPosition.Y] == Roll)
74+
{
75+
adjacentRolls++;
76+
}
77+
}
78+
79+
return adjacentRolls < 4;
80+
}
81+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..@@.@@@@.
2+
@@@.@.@.@@
3+
@@@@@.@.@@
4+
@.@@@@..@.
5+
@@.@@@@.@@
6+
.@@@@@@@.@
7+
.@.@.@.@@@
8+
@.@@@.@@@@
9+
.@@@@@@@@.
10+
@.@.@@@.@.

0 commit comments

Comments
 (0)