tangled
alpha
login
or
join now
benharri.org
/
aoc
0
fork
atom
my advent of code solutions
0
fork
atom
overview
issues
pulls
pipelines
2024 day 4 part 2
benharri.org
1 year ago
f7bdb331
917738f2
+30
-12
2 changed files
expand all
collapse all
unified
split
Solutions
2024
Day04.cs
Tests
Test2024.cs
+28
-10
Solutions/2024/Day04.cs
reviewed
···
5
5
/// </summary>
6
6
public sealed class Day04() : Day(2024, 4, "Ceres Search")
7
7
{
8
8
+
private char[][] _grid = [];
9
9
+
8
10
private static readonly List<(int dx, int dy)> Directions =
9
11
[
10
12
(-1, -1), (-1, 0), (-1, 1),
···
12
14
(1, -1), (1, 0), (1, 1),
13
15
];
14
16
17
17
+
private static bool OutOfBounds(char[][] grid, int x, int y) =>
18
18
+
x < 0 || x >= grid[0].Length || y < 0 || y >= grid.Length;
19
19
+
20
20
+
public override void ProcessInput() => _grid = Input.Select(line => line.ToArray()).ToArray();
21
21
+
15
22
public override object Part1()
16
23
{
17
17
-
var grid = Input.Select(line => line.ToArray()).ToArray();
18
24
var count = 0;
19
25
20
20
-
for (var y = 0; y < grid.Length; y++)
21
21
-
for (var x = 0; x < grid[0].Length; x++)
22
22
-
if (grid[y][x] == 'X')
26
26
+
for (var y = 0; y < _grid.Length; y++)
27
27
+
for (var x = 0; x < _grid[0].Length; x++)
28
28
+
if (_grid[y][x] == 'X')
23
29
{
24
30
foreach (var (dx, dy) in Directions)
25
31
{
26
32
int magnitude = 1, newY = y + dy, newX = x + dx;
27
27
-
if (OutOfBounds(grid, newX, newY)) continue;
33
33
+
if (OutOfBounds(_grid, newX, newY)) continue;
28
34
29
29
-
while (grid[newY][newX] == "XMAS"[magnitude])
35
35
+
while (_grid[newY][newX] == "XMAS"[magnitude])
30
36
{
31
37
if (++magnitude > 3)
32
38
{
···
37
43
newY += dy;
38
44
newX += dx;
39
45
40
40
-
if (OutOfBounds(grid, newX, newY)) break;
46
46
+
if (OutOfBounds(_grid, newX, newY)) break;
41
47
}
42
48
}
43
49
}
···
45
51
return count;
46
52
}
47
53
48
48
-
private static bool OutOfBounds(char[][] grid, int x, int y) =>
49
49
-
x < 0 || x >= grid[0].Length || y < 0 || y >= grid.Length;
54
54
+
55
55
+
public override object Part2()
56
56
+
{
57
57
+
var count = 0;
58
58
+
59
59
+
for (var y = 1; y < _grid.Length - 1; y++)
60
60
+
for (var x = 1; x < _grid[0].Length - 1; x++)
61
61
+
if (_grid[y][x] == 'A')
62
62
+
if (((_grid[y - 1][x - 1] == 'M' && _grid[y + 1][x + 1] == 'S')
63
63
+
|| (_grid[y - 1][x - 1] == 'S' && _grid[y + 1][x + 1] == 'M'))
64
64
+
&& ((_grid[y + 1][x - 1] == 'M' && _grid[y - 1][x + 1] == 'S')
65
65
+
|| (_grid[y + 1][x - 1] == 'S' && _grid[y - 1][x + 1] == 'M')))
66
66
+
count++;
50
67
51
51
-
public override object Part2() => "";
68
68
+
return count;
69
69
+
}
52
70
}
+2
-2
Tests/Test2024.cs
reviewed
···
8
8
[DataRow(typeof(Day01), "1319616", "27267728")]
9
9
[DataRow(typeof(Day02), "321", "386")]
10
10
[DataRow(typeof(Day03), "163931492", "76911921")]
11
11
-
[DataRow(typeof(Day04), "2573", "")]
11
11
+
[DataRow(typeof(Day04), "2573", "1850")]
12
12
public void CheckAllDays(Type dayType, string part1, string part2) =>
13
13
Common.CheckDay(dayType, part1, part2);
14
14
···
16
16
[DataRow(typeof(Day01), "11", "31")]
17
17
[DataRow(typeof(Day02), "2", "4")]
18
18
[DataRow(typeof(Day03), "161", "48")]
19
19
-
[DataRow(typeof(Day04), "18", "")]
19
19
+
[DataRow(typeof(Day04), "18", "9")]
20
20
public void CheckTestInputs(Type dayType, string part1, string part2) =>
21
21
Common.CheckDay(dayType, part1, part2, true);
22
22
}