-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.cs
75 lines (62 loc) · 2.32 KB
/
Day8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Aoc2024Net.Utilities;
namespace Aoc2024Net.Days
{
internal sealed class Day8 : Day
{
public override object? SolvePart1() =>
Solve(false);
public override object? SolvePart2() =>
Solve(true);
public int Solve(bool moreAntinodes)
{
var (grid, width, height) = InputData.GetInputCharGrid();
var coords = grid.GetAllCoordinates();
var frequenciesGroups = coords
.GroupBy(grid.At)
.Where(g => g.Key != '.')
.Select(g => g.ToArray())
.ToArray();
var anodes = new HashSet<Coordinate>();
void CalculateAntinodes(Coordinate position, Coordinate direction)
{
do
{
position += direction;
anodes.Add(position);
}
while (grid.IsInGrid(width, height, position) && moreAntinodes);
}
foreach (var frequencies in frequenciesGroups)
{
var coordinatePairs = GetCoordinatePairs(frequencies).ToArray();
foreach (var (a, b) in coordinatePairs)
{
var topNode = a.Y <= b.Y ? a : b;
var bottomNode = topNode == a ? b : a;
var xDistance = topNode.X - bottomNode.X;
var yDistance = bottomNode.Y - topNode.Y;
CalculateAntinodes(topNode, new Coordinate(xDistance, -yDistance));
CalculateAntinodes(bottomNode, new Coordinate(-xDistance, yDistance));
}
if (moreAntinodes)
{
foreach (var c in frequencies)
{
anodes.Add(c);
}
}
}
return anodes.Count(c => grid.IsInGrid(width, height, c));
}
private static IEnumerable<(Coordinate A, Coordinate B)> GetCoordinatePairs(Coordinate[] coords)
{
for (var i = 0; i < coords.Length; i++)
{
for (var j = i + 1; j < coords.Length; j++)
{
yield return (coords[i], coords[j]);
}
}
}
}
}