Skip to content

Commit

Permalink
day 16 now runs in 35 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
norganos committed Dec 16, 2023
1 parent 8f4fb2c commit 603fbb2
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/main/kotlin/de/linkel/aoc/Day16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,28 @@ class Day16: AbstractLinesAdventDay<Int>() {

fun run(grid: Grid<Thing>, startBeam: Beam): Int {
return generateSequence(
setOf(startBeam)
) { beams ->
beams + beams
.asSequence()
.map(Beam::step)
.filter { it.pos in grid }
.map {
it to grid[it.pos]
}
.flatMap { (beam, thing) ->
thing?.modify(beam) ?: listOf(beam)
}
.toSet()
setOf(startBeam) to emptySet<Beam>()
) { (activeBeams, oldBeams) ->
Pair(
activeBeams
.asSequence()
.map { it.step() }
.filter { it.pos in grid }
.map {
it to grid[it.pos]
}
.flatMap { (beam, thing) ->
thing?.modify(beam) ?: listOf(beam)
}
.filter { it !in oldBeams }
.toSet(),
oldBeams + activeBeams
.filter { it.pos in grid }
)
}
.zipWithNext()
.takeWhile { (old, new) -> old != new }
.map{ (_, new) -> new }
.last()
// .also {
// print(grid, it)
// }
.map { it.pos }
.filter { it in grid }
.first { it.first.isEmpty() }
.second
.map{ it.pos }
.toSet()
.count()
}
Expand Down

0 comments on commit 603fbb2

Please sign in to comment.