|
| 1 | +// Advent of Code 2024, Day 08. |
| 2 | +// By Sebastian Raaphorst, 2024. |
| 3 | + |
| 4 | +package day08 |
| 5 | + |
| 6 | +import common.day |
| 7 | +import common.readInput |
| 8 | + |
| 9 | +typealias Frequency = Char |
| 10 | +typealias Position = Pair<Int, Int> |
| 11 | +typealias AntennaMap = Map<Frequency, Set<Position>> |
| 12 | + |
| 13 | +private operator fun Position.minus(other: Position): Position = |
| 14 | + Position(this.first - other.first, this.second - other.second) |
| 15 | + |
| 16 | +private operator fun Position.plus(other: Position): Position = |
| 17 | + Position(this.first + other.first, this.second + other.second) |
| 18 | + |
| 19 | +data class Grid(val height: Int, val width: Int, val antennae: AntennaMap) { |
| 20 | + /** |
| 21 | + * Calculate the slope of the line that the points are on, namely: |
| 22 | + * pos1 - pos2 |
| 23 | + * Then pos1 = pos2 + delta, and pos2 = pos1 - delta, so we want the |
| 24 | + * other two candidates, namely: |
| 25 | + * 1. pos1 + delta |
| 26 | + * 2. pos2 - delta |
| 27 | + * and if they are in the grid, then they contribute. |
| 28 | + */ |
| 29 | + private fun calculateAntinodePair(pos1: Position, pos2: Position): List<Position> { |
| 30 | + val delta = pos1 - pos2 |
| 31 | + return listOf(pos1 + delta, pos2 - delta) |
| 32 | + .filter { (row, col) -> row in (0 until height) && col in (0 until width) } |
| 33 | + .also { it.forEach { t -> println("For $pos1 + $pos2 -> $t") }} |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Calculate the slope of the line that the antinodes are on, namely |
| 38 | + * pos1 - pos2 |
| 39 | + * and then continue to calculate all possible other candidates (including themselves). |
| 40 | + */ |
| 41 | + private fun calculateAllAntinodes(pos1: Position, pos2: Position): Set<Position> { |
| 42 | + val delta = pos1 - pos2 |
| 43 | + tailrec fun aux(newAntinodes: Set<Position> = setOf(pos1, pos2), |
| 44 | + antinodes: Set<Position> = emptySet()): Set<Position> = when { |
| 45 | + newAntinodes.isEmpty() -> antinodes |
| 46 | + else -> { |
| 47 | + val possibleNewAntinodes = newAntinodes.flatMap { listOf(it + delta, it - delta) } |
| 48 | + .toSet() |
| 49 | + .filter { (row, col) -> row in (0 until height) && col in (0 until width) } |
| 50 | + .toSet() |
| 51 | + aux(possibleNewAntinodes, antinodes + newAntinodes) |
| 52 | + } |
| 53 | + } |
| 54 | + return aux() |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * For a given set of positions corresponding to a frequency, calculate |
| 59 | + * the set of generated antinodes. |
| 60 | + */ |
| 61 | + private fun calculateAntinodesForFrequency(positions: Set<Position>): Set<Position> = |
| 62 | + positions.withIndex() |
| 63 | + .flatMap { (idx, pos1) -> positions.drop(idx + 1) |
| 64 | + .flatMap { pos2 -> |
| 65 | + calculateAntinodePair(pos1, pos2) |
| 66 | + } |
| 67 | + }.toSet() |
| 68 | + |
| 69 | + private fun calculateAntinodesForFrequency2(positions: Set<Position>): Set<Position> = |
| 70 | + positions.withIndex() |
| 71 | + .flatMap { (idx, pos1) -> positions.drop(idx + 1) |
| 72 | + .flatMap { pos2 -> |
| 73 | + calculateAllAntinodes(pos1, pos2) |
| 74 | + } |
| 75 | + }.toSet() |
| 76 | + |
| 77 | + fun calculateAntinodeCount(): Int = |
| 78 | + antennae.values |
| 79 | + .flatMap(::calculateAntinodesForFrequency) |
| 80 | + .toSet() |
| 81 | + .size |
| 82 | + |
| 83 | + fun calculateAntinodeCount2() : Int = |
| 84 | + antennae.values |
| 85 | + .flatMap(::calculateAntinodesForFrequency2) |
| 86 | + .toSet() |
| 87 | + .size |
| 88 | + |
| 89 | + companion object { |
| 90 | + private fun parseMap(input: String): AntennaMap = |
| 91 | + input.lines() |
| 92 | + .withIndex() |
| 93 | + .flatMap { (rowIdx, row) -> |
| 94 | + row.withIndex() |
| 95 | + .filter { (_, frequency) -> frequency != '.' } |
| 96 | + .map { (colIdx, frequency) -> frequency to Pair(rowIdx, colIdx) } |
| 97 | + }.groupBy({ it.first }, { it.second }) |
| 98 | + .mapValues { (_, positions) -> positions.toSet() } |
| 99 | + |
| 100 | + fun parse(input: String) = |
| 101 | + Grid(input.lines().size, input.lines().get(0).length, parseMap(input)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fun answer1(input: String): Int = |
| 106 | + Grid.parse(input).calculateAntinodeCount() |
| 107 | + |
| 108 | +fun answer2(input: String): Int = |
| 109 | + Grid.parse(input).calculateAntinodeCount2() |
| 110 | + |
| 111 | + |
| 112 | +fun main() { |
| 113 | + val input = readInput({}::class.day()).trim() |
| 114 | + |
| 115 | + println("--- Day 8: Resonant Collinearity ---") |
| 116 | + |
| 117 | + // Part 1: 376 |
| 118 | + println("Part 1: ${answer1(input)}") |
| 119 | + |
| 120 | + // Part 2: |
| 121 | +// println("Part 2: ${answer2(input)}") |
| 122 | +} |
0 commit comments