Skip to content

Commit

Permalink
[2024/8] Resonant Collinearity (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 8, 2024
1 parent 020ccd1 commit ad121a8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 |||||||| | | | | | | | | | | | | | | | | | | 14 |
| 2024 |||||||| | | | | | | | | | | | | | | | | | | 15 |

## 🛷 How to run

Expand Down Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt
Original file line number Diff line number Diff line change
@@ -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<Int, Int>.isInBounds(gridSize: Int) = toList().all { it in 0 until gridSize }
}
}
50 changes: 50 additions & 0 deletions src/main/resources/inputs/year2024/day08.txt
Original file line number Diff line number Diff line change
@@ -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................
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package adventofcode.year2024

import adventofcode.PuzzleBaseSpec

class Day08ResonantCollinearitySpec : PuzzleBaseSpec(14)
12 changes: 12 additions & 0 deletions src/test/resources/inputs/year2024/day08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

0 comments on commit ad121a8

Please sign in to comment.