-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay08.cs
More file actions
122 lines (103 loc) · 3.85 KB
/
Day08.cs
File metadata and controls
122 lines (103 loc) · 3.85 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace aoc.y2024;
[RunnableDay(year: 2024, day: 8)]
class Day08 : Day
{
public override string PartOne()
{
var map = ReadInputLines().Select(line => line.ToCharArray()).ToArray();
var antenasLocations = FindAllAntenas(map);
var antinodesPositions = new HashSet<(int, int)>();
foreach (var entry in antenasLocations)
{
if (entry.Value.Length > 1)
{
for (var i = 0; i < entry.Value.Length; i++)
{
for (var j = i + 1; j < entry.Value.Length; j++)
{
var antinode1 = AntinodeOf(entry.Value[i], entry.Value[j]);
if (IsPositionInRange(antinode1, map))
{
antinodesPositions.Add(antinode1);
}
var antinode2 = AntinodeOf(entry.Value[j], entry.Value[i]);
if (IsPositionInRange(antinode2, map))
{
antinodesPositions.Add(antinode2);
}
}
}
}
}
return antinodesPositions.Count.ToString();
}
public override string PartTwo()
{
var map = ReadInputLines().Select(line => line.ToCharArray()).ToArray();
var antenasLocations = FindAllAntenas(map);
var antinodesPositions = new HashSet<(int, int)>();
foreach (var entry in antenasLocations)
{
if (entry.Value.Length > 1)
{
for (var i = 0; i < entry.Value.Length; i++)
{
for (var j = i + 1; j < entry.Value.Length; j++)
{
var antinodesDir1 = AllValidAntinodesFromTo(map, entry.Value[i], entry.Value[j]);
var antinodesDir2 = AllValidAntinodesFromTo(map, entry.Value[j], entry.Value[i]);
antinodesPositions = [.. antinodesPositions, .. antinodesDir1, .. antinodesDir2];
}
}
}
}
return antinodesPositions.Count.ToString();
}
private static Dictionary<char, (int, int)[]> FindAllAntenas(char[][] map)
{
var antenasLocations = new Dictionary<char, (int, int)[]>();
for (var y = 0; y < map.Length; y++)
{
for (var x = 0; x < map[0].Length; x++)
{
if (map[y][x] != '.')
{
if (antenasLocations.TryGetValue(map[y][x], out var locations))
{
antenasLocations[map[y][x]] = [.. locations, (x, y)];
}
else
{
antenasLocations[map[y][x]] = [(x, y)];
}
}
}
}
return antenasLocations;
}
private static bool IsPositionInRange((int, int) position, char[][] map)
{
return position.Item1 >= 0 && position.Item1 < map[0].Length && position.Item2 >= 0 && position.Item2 < map.Length;
}
private static (int, int) AntinodeOf((int, int) source, (int, int) target)
{
var modX = target.Item1 - source.Item1;
var modY = target.Item2 - source.Item2;
return (target.Item1 + modX, target.Item2 + modY);
}
private static HashSet<(int, int)> AllValidAntinodesFromTo(char[][] map, (int, int) source, (int, int) target)
{
HashSet<(int, int)> result = [];
result.Add(source);
result.Add(target);
var antinode = AntinodeOf(source, target);
while (IsPositionInRange(antinode, map))
{
result.Add(antinode);
source = target;
target = antinode;
antinode = AntinodeOf(source, target);
}
return result;
}
}