Skip to content

Commit d6649a3

Browse files
committed
Solve 2024 day 8
1 parent 7de74e3 commit d6649a3

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

2024.Tests/Day08Tests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Basje.AdventOfCode.Y2024.D08;
2+
3+
namespace Basje.AdventOfCode.Y2024.Tests;
4+
5+
public static class Day08Tests
6+
{
7+
[Fact]
8+
public static void Part1()
9+
{
10+
var solution = new Day08();
11+
const string input = """
12+
............
13+
........0...
14+
.....0......
15+
.......0....
16+
....0.......
17+
......A.....
18+
............
19+
............
20+
........A...
21+
.........A..
22+
............
23+
............
24+
""";
25+
26+
var answer = solution.SolvePart1(input);
27+
28+
answer.Should().Be(14);
29+
}
30+
31+
[Fact]
32+
public static void Part2()
33+
{
34+
var solution = new Day08();
35+
const string input = """
36+
............
37+
........0...
38+
.....0......
39+
.......0....
40+
....0.......
41+
......A.....
42+
............
43+
............
44+
........A...
45+
.........A..
46+
............
47+
............
48+
""";
49+
50+
var answer = solution.SolvePart2(input);
51+
52+
answer.Should().Be(34);
53+
}
54+
}

2024/D08/Day08.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
namespace Basje.AdventOfCode.Y2024.D08;
2+
3+
public class Day08 : Solution<IGrouping<char,((int X, int Y) Coordinates, char Character)>[]>
4+
{
5+
protected override IGrouping<char,((int X, int Y) Coordinates, char Character)>[] ParseInput(string input)
6+
{
7+
return input
8+
.PerLine()
9+
.IgnoreEmptyLines()
10+
.SelectMany((line, y) => line.Select((character, x) => ((X: x, Y: y), character)))
11+
.GroupBy(cell => cell.character)
12+
.ToArray();
13+
}
14+
15+
protected override object SolvePart1(IGrouping<char,((int X, int Y) Coordinates, char Character)>[] groups)
16+
{
17+
var (maxX, maxY) = groups
18+
.Single(group => group.Key == '.')
19+
.Max(cell => cell.Coordinates);
20+
21+
List<(int X, int Y)> antinodes = [];
22+
23+
foreach (var group in groups)
24+
{
25+
if (group.Key == '.') continue;
26+
27+
var items = group.ToArray();
28+
29+
for (var i = 0; i < items.Length; i++)
30+
{
31+
for (var j = i + 1; j < items.Length; j++)
32+
{
33+
var left = items[i].Coordinates;
34+
var right = items[j].Coordinates;
35+
36+
var diffX = right.X - left.X;
37+
var diffY = right.Y - left.Y;
38+
39+
var firstAntinode = (X: right.X + diffX, Y: right.Y + diffY);
40+
var secondAntinode = (X: left.X - diffX, Y: left.Y - diffY);
41+
42+
if (firstAntinode.X >= 0 && firstAntinode.X <= maxX && firstAntinode.Y >= 0 && firstAntinode.Y <= maxY)
43+
{
44+
antinodes.Add(firstAntinode);
45+
}
46+
47+
if (secondAntinode.X >= 0 && secondAntinode.X <= maxX && secondAntinode.Y >= 0 && secondAntinode.Y <= maxY)
48+
{
49+
antinodes.Add(secondAntinode);
50+
}
51+
}
52+
}
53+
}
54+
55+
return antinodes.Distinct().Count();
56+
}
57+
58+
protected override object SolvePart2(IGrouping<char,((int X, int Y) Coordinates, char Character)>[] groups)
59+
{
60+
var (maxX, maxY) = groups
61+
.Single(group => group.Key == '.')
62+
.Max(cell => cell.Coordinates);
63+
64+
List<(int X, int Y)> antinodes = [];
65+
66+
var antennas = groups
67+
.Where(group => group.Key != '.')
68+
.SelectMany(group => group.ToArray())
69+
.Select(item => item.Coordinates);
70+
71+
foreach (var group in groups)
72+
{
73+
if (group.Key == '.') continue;
74+
75+
var items = group.ToArray();
76+
77+
for (var i = 0; i < items.Length; i++)
78+
{
79+
for (var j = i + 1; j < items.Length; j++)
80+
{
81+
var left = items[i].Coordinates;
82+
var right = items[j].Coordinates;
83+
84+
var diffX = right.X - left.X;
85+
var diffY = right.Y - left.Y;
86+
87+
var firstAntinode = (X: right.X + diffX, Y: right.Y + diffY);
88+
var secondAntinode = (X: left.X - diffX, Y: left.Y - diffY);
89+
90+
while (firstAntinode.X >= 0 && firstAntinode.X <= maxX && firstAntinode.Y >= 0 && firstAntinode.Y <= maxY)
91+
{
92+
antinodes.Add(firstAntinode);
93+
94+
firstAntinode = (X: firstAntinode.X + diffX, Y: firstAntinode.Y + diffY);
95+
}
96+
97+
while (secondAntinode.X >= 0 && secondAntinode.X <= maxX && secondAntinode.Y >= 0 && secondAntinode.Y <= maxY)
98+
{
99+
antinodes.Add(secondAntinode);
100+
101+
secondAntinode = (X: secondAntinode.X - diffX, Y: secondAntinode.Y - diffY);
102+
}
103+
}
104+
}
105+
}
106+
107+
antinodes.AddRange(antennas);
108+
109+
return antinodes.Distinct().Count();
110+
}
111+
}
112+
113+
internal static class Day08Extensions
114+
{
115+
116+
}

2024/D08/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...O.....0...............................p..k.....
2+
O.........o....w..T.........................p.....
3+
..................w..........oM...................
4+
.............................................Y....
5+
o.............T...........................z.....pk
6+
.....................................z..Y....t.F..
7+
...........T..........................F.......Y...
8+
...................A............z...k..M..........
9+
....O.........j....w.....................M........
10+
..........w....T..................M..k............
11+
.............5.............................F.....t
12+
......................A.............F....E........
13+
.....................S.........A..................
14+
.P................................................
15+
........................A...E.............x...t...
16+
............j.........................t.........x.
17+
.......................j.........................x
18+
....................................E........c....
19+
.............P.......E............................
20+
...............j..5...............q...............
21+
..............P..............................Qc...
22+
..........C..........s................c........x..
23+
.............C...r................................
24+
.....C......V..........q...................Q......
25+
...........yX.........q...................Q.......
26+
.....X....................e.............m.........
27+
.2.................e..7....m.......c..............
28+
......i..........e...K..............f....U...WQ...
29+
...X....................e....................V...Y
30+
...............5..X.....0.........................
31+
..C..i......5..3...sK......J.........f..B.........
32+
2............3.............0I...a.........BNb.....
33+
.........................K............m...........
34+
.r........3...............s....7...m.v...f.......b
35+
........3........7.....Iy..........q...b.N........
36+
.....R.1.......................n.....a.B.......VN.
37+
......R.........9...................a...W.........
38+
..........7.6................S....................
39+
..............r.......s...0........nb....W..f..B..
40+
...2...........I......K...........................
41+
..............................u...n............U..
42+
............r......y.............O............W...
43+
.......R..........v..u................N...V.......
44+
..........R.8..4.9..y........u....................
45+
...8...............v................J.............
46+
.....8..............4.........Z.........n.....J.U.
47+
...........4i....................Z..S.............
48+
..............9...........1.u.S................J..
49+
8...6....9..4......a........Z.1...................
50+
....................v..i.............Z............

0 commit comments

Comments
 (0)