diff --git a/2024/src/main/kotlin/day8.kt b/2024/src/main/kotlin/day8.kt index 8242290..97db043 100644 --- a/2024/src/main/kotlin/day8.kt +++ b/2024/src/main/kotlin/day8.kt @@ -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 pairs(list: List) = sequence { + for (i in list.indices) { + for (j in 0..