Day 8. Could have been faster if I think first.

This commit is contained in:
Chris Hodges 2024-12-08 07:14:18 +01:00
parent 15ce1ad06e
commit 9d61ce1bc4
2 changed files with 180 additions and 0 deletions

View File

@ -217,6 +217,18 @@ class CharGrid {
return matches return matches
} }
fun collectMatches(predicate: (char: Char) -> Boolean): List<Pair<Char, RelPos>> {
val matches = ArrayList<Pair<Char, RelPos>>()
for (r in 0 until height) {
for (c in 0 until width) {
if (predicate(get(c, r))) {
matches.add(get(c, r) to RelPos(c, r))
}
}
}
return matches
}
fun addBorder(borderChar: Char = bChar): CharGrid { fun addBorder(borderChar: Char = bChar): CharGrid {
val topBottom = CharArray(width + 2) { borderChar } val topBottom = CharArray(width + 2) { borderChar }
return CharGrid(Array(height + 2) { return CharGrid(Array(height + 2) {

168
src/aoc2024/Day08.kt Normal file
View File

@ -0,0 +1,168 @@
package aoc2024
import CharGrid
import RelPos
import println
import readInput
/*
--- Day 8: Resonant Collinearity ---
You find yourselves on the roof of a top-secret Easter Bunny installation.
While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!
Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
So, for these two antennas with frequency a, they create the two antinodes marked with #:
..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........
Antennas with different frequencies don't create antinodes; A and a count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a-frequency antinode at its location:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......A...
..........
..........
The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A-frequency antenna:
......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, there are 14 total unique locations that contain an antinode within the bounds of the map.
Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
*/
fun main() {
val inlineTestInput = """
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
fun part1(input: List<String>): Int {
val grid = CharGrid(input)
val antennas = grid.collectMatches { it != '.' }.groupBy({ it.first }) { it.second }
val antinodes = HashSet<RelPos>()
for (at in antennas.values) {
for ((i1, p1) in at.withIndex()) {
for (i2 in i1 + 1 until at.size) {
val p2 = at[i2]
val ap1 = RelPos(p2.dc + (p2.dc - p1.dc), p2.dr + (p2.dr - p1.dr))
val ap2 = RelPos(p1.dc - (p2.dc - p1.dc), p1.dr - (p2.dr - p1.dr))
if (grid.isInside(ap1)) antinodes.add(ap1)
if (grid.isInside(ap2)) antinodes.add(ap2)
}
}
}
return antinodes.size
}
fun part2(input: List<String>): Int {
val grid = CharGrid(input)
val antennas = grid.collectMatches { it != '.' }.groupBy({ it.first }) { it.second }
val antinodes = HashSet<Pair<RelPos, RelPos>>()
for (at in antennas.values) {
for ((i1, p1) in at.withIndex()) {
for (i2 in i1 + 1 until at.size) {
val p2 = at[i2]
val dc = p2.dc - p1.dc
val dr = p2.dr - p1.dr
val sc = p1.dc
val sr = p1.dr
antinodes.add(RelPos(sc, sr) to RelPos(dc, dr))
}
}
}
var sum = 0
for (r in 0 until grid.height) {
for (c in 0 until grid.width) {
if (antinodes.any {
val rr = r - it.first.dr
val rc = c - it.first.dc
val f1 = rr / it.second.dr
val f2 = rc / it.second.dc
((rr % it.second.dr) == 0) &&
((rc % it.second.dc) == 0) &&
(f1 == f2)
}) {
sum++
}
}
}
return sum
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2024/Day08_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 14)
//check(testInputPart2Result == 34)
val input = readInput("aoc2024/Day08")
part1(input).println()
part2(input).println()
}