Day 8 part 1

This commit is contained in:
Sven Weidauer 2024-12-08 19:33:27 +01:00
parent dc54e2d776
commit 7ace3b8119
2 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,42 @@
fun main() {
val grid = Grid.read("day8.txt")
val antennasByFrequency = mutableMapOf<Char, MutableList<Grid.Coordinate>>()
for (y in 0..<grid.height) {
for (x in 0..<grid.width) {
val value = grid.get(x, y)
if (value != '.') {
antennasByFrequency.getOrPut(value) { mutableListOf() }.add(Grid.Coordinate(x, y))
}
}
}
fun isAntinode(coordinate: Grid.Coordinate, frequency: Char, antennas: List<Grid.Coordinate>) =
antennas.any { antenna ->
val dx = antenna.x - coordinate.x
val dy = antenna.y - coordinate.y
val secondAntenna = Grid.Coordinate(antenna.x + dx, antenna.y + dy)
secondAntenna != antenna && grid.inside(secondAntenna) && grid.get(secondAntenna) == frequency
}
fun isAntinode(coordinate: Grid.Coordinate) =
antennasByFrequency.any { (frequency, positions) ->
isAntinode(coordinate, frequency, positions)
}
val antinodes = mutableSetOf<Grid.Coordinate>()
for (y in 0..<grid.height) {
for (x in 0..<grid.width) {
val coordinate = Grid.Coordinate(x, y)
if (isAntinode(coordinate)) {
antinodes.add(coordinate)
}
}
}
println("Part 1: ${antinodes.count()}")
}

View file

@ -0,0 +1,50 @@
.....................U.........w..................
l.................................................
...........o.a................U...w...............
............................................W.....
..........T....................s.............7....
.............................................W....
.........T..............4....n.d.H.........5......
......T.....oj...U.....n...w......H...........z...
.G..x..........................E.....V..H.........
.........a....................d....s.......7w.....
...j....r.............o.............V.......d...W.
.......r..J.Goa.U...............n................z
.........Jj.........M..........Pv.................
...J...........t..3..M..............sLV...........
...................t................n.............
....r...........X...........M........v............
...x....t......I......a.PM...............W........
...........1.Bj....I........vO.h.dL...............
.........6....Rr......B...X........h..5v.L..z.....
......1G...........x.....3B.......5...............
.................B....0..........4..E.............
.....................X.....5..h....P....f.....D...
.......1........J.....eK..........................
..................I....R....K...........k.........
......G..................O........................
...........H...9...............K8.P.4..k..E.......
............1....3.............8.F.............f..
.........................4........................
.l...........X............9.......................
....N.................R...t.e.....................
...g............3..R.........e....h.........f.....
...........................e......i...............
................2...I.7..9..O.....s.........k.....
....................6...9E.............F..O.......
........................KN........................
.......g......................Z.........F..f...Y..
...........................A....i.................
...........6g...b........8.......y.....S..........
..l.....6.....m...............8...................
....u..m...b...............p...A..................
..............b.p........................k........
....m......2...........Z..y....i..................
........g2.....b.........i....D..ZF...............
......2.0...........p............N..........A.....
...m.............S...y........A...Z...N...........
..S..l..........................................Y.
........S....0u.................y......DY.........
...........0.........................D............
.................u...................p...Y........
.......u..........................................