diff --git a/README.md b/README.md index 4114aae..4e14d39 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ | 2021 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | ★ | ★ | | | | | | | | | | | | 26 | | 2022 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | | | | | | | ☆ | | | | ☆ | 28 | | 2023 | ★ | ★ | ★ | ★ | ☆ | ★ | ☆ | | | | | | | | | | | | | | | | | | | 12 | -| 2024 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | | | | | | | | | | | | | | | | | | 14 | +| 2024 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ☆ | | | | | | | | | | | | | | | | | | 15 | ## 🛷 How to run @@ -171,6 +171,7 @@ e.g. `HandyHaversacks`)* | | 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [[Code](src/main/kotlin/adventofcode/year2024/Day05PrintQueue.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day05PrintQueueSpec.kt)] | `6498` | `5017` | | | 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [[Code](src/main/kotlin/adventofcode/year2024/Day06GuardGallivant.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day06GuardGallivantSpec.kt)] | `5212` | `1767` | | | 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [[Code](src/main/kotlin/adventofcode/year2024/Day07BridgeRepair.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day07BridgeRepairSpec.kt)] | `8401132154762` | `95297119227552` | +| | 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [[Code](src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day08ResonantCollinearitySpec.kt)] | `252` | | ## 🕯️ Useful commands diff --git a/src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt b/src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt new file mode 100644 index 0000000..d96bab1 --- /dev/null +++ b/src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt @@ -0,0 +1,35 @@ +package adventofcode.year2024 + +import adventofcode.Puzzle +import adventofcode.PuzzleInput +import adventofcode.common.Tuple.minus +import adventofcode.common.Tuple.plus + +class Day08ResonantCollinearity(customInput: PuzzleInput? = null) : Puzzle(customInput) { + private val gridSize by lazy { input.lines().size } + + override fun partOne() = + input + .lines() + .flatMapIndexed { y, row -> row.mapIndexed { x, char -> (x to y) to char } } + .filterNot { (_, char) -> char == '.' } + .groupBy({ it.second }, { it.first }) + .map { (char, antennas) -> + char to antennas.flatMap { a -> antennas.mapNotNull { b -> if (a != b) setOf(a, b) else null } }.toSet() + } + .map { (char, antennaPairs) -> + char to + antennaPairs.map { pair -> + val distance = pair.last() - pair.first() + setOf(pair.first() - distance, pair.last() + distance).filter { it.isInBounds((gridSize)) } + } + } + .map { (_, antinodes) -> antinodes.flatten() } + .reduce { allAntinodes, antinodes -> allAntinodes + antinodes } + .toSet() + .size + + companion object { + fun Pair.isInBounds(gridSize: Int) = toList().all { it in 0 until gridSize } + } +} diff --git a/src/main/resources/inputs/year2024/day08.txt b/src/main/resources/inputs/year2024/day08.txt new file mode 100644 index 0000000..dbf737a --- /dev/null +++ b/src/main/resources/inputs/year2024/day08.txt @@ -0,0 +1,50 @@ +.............4....O..........w....R............... +.................................f................ +..............4...j......NW0...................... +....................R..W.......................... +...............R.................................. +.................................................. +v.......................f.......0W................ +.....9L............l...N.........w................ +....L....9.......ON........8...................... +.1.........49L........f..0..N..................... +..........................V...l................... +..........4....................................... +.....................j...................3.....U.. +....O.....U....................................... +........J......................l.................. +.O....s.Q.......j.....l.....w..........F...q...... +.................................................. +.U.......................j..8..................... +................U...............................3. +2.............................J............3...... +..............................F................... +.....s...R...........J..................F......... +.s......................x..........F.....q........ +.......2.....Q........3........x.................. +...........v......................u............... +..............v...........n......8............q... +.......f..................8........i.............. +.5..................1n..............P.....i....... +............7............Q..................X..... +......5...p....................V.................. +.................J..........nx............q....... +.......p............W...........................0. +......2.............p.5.....1....P................ +......I.................7.X....i...P.............. +............s.....r...w................V.......... +...............or...6.................V........... +............................PS.7.................. +..........o...........................S........... +...........5..............o..1.......n............ +...........I.........r.......7.......6............ +.................o.r...........X.................. +................................x.........u....... +.........p..Q....2................................ +.........v.................S.....................u +I...........................S.....6............... +.................................................. +.......I.......................................... +.................................................. +.......................................6.......... +.................................X................ \ No newline at end of file diff --git a/src/test/kotlin/adventofcode/year2024/Day08ResonantCollinearitySpec.kt b/src/test/kotlin/adventofcode/year2024/Day08ResonantCollinearitySpec.kt new file mode 100644 index 0000000..707686b --- /dev/null +++ b/src/test/kotlin/adventofcode/year2024/Day08ResonantCollinearitySpec.kt @@ -0,0 +1,5 @@ +package adventofcode.year2024 + +import adventofcode.PuzzleBaseSpec + +class Day08ResonantCollinearitySpec : PuzzleBaseSpec(14) diff --git a/src/test/resources/inputs/year2024/day08.txt b/src/test/resources/inputs/year2024/day08.txt new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/src/test/resources/inputs/year2024/day08.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file