Day 8 part 2

This commit is contained in:
Sven Weidauer 2024-12-09 21:13:26 +01:00
parent dc670cfe15
commit 6f13a6f281
2 changed files with 42 additions and 0 deletions

View file

@ -39,4 +39,36 @@ fun main() {
}
println("Part 1: ${antinodes.count()}")
antinodes.clear()
for (positions in antennasByFrequency.values) {
for ((first, second) in pairs(positions)) {
val dx = second.x - first.x
val dy = second.y - first.y
var pos = first
while (grid.inside(pos)) {
antinodes.add(pos)
pos = Grid.Coordinate(pos.x + dx, pos.y + dy)
}
pos = first
while (grid.inside(pos)) {
antinodes.add(pos)
pos = Grid.Coordinate(pos.x - dx, pos.y - dy)
}
}
}
println("Part 2: ${antinodes.count()}")
}
fun <T> pairs(list: List<T>) = sequence {
for (i in list.indices) {
for (j in 0..<i) {
yield(list[i] to list[j])
}
}
}

View file

@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........