-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2024/8] Resonant Collinearity (Part 1)
- Loading branch information
Showing
5 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/adventofcode/year2024/Day08ResonantCollinearity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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................ |
5 changes: 5 additions & 0 deletions
5
src/test/kotlin/adventofcode/year2024/Day08ResonantCollinearitySpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package adventofcode.year2024 | ||
|
||
import adventofcode.PuzzleBaseSpec | ||
|
||
class Day08ResonantCollinearitySpec : PuzzleBaseSpec(14) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ |