-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11DumboOctopus.kt
50 lines (38 loc) · 2.08 KB
/
Day11DumboOctopus.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package adventofcode.year2021
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.neighbors
class Day11DumboOctopus(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val grid by lazy { input.lines().map { it.map { it.toString().toInt() } } }
private fun List<List<Int>>.incrementAll() = map { it.map(Int::inc) }
private fun List<List<Int>>.increment(points: List<Pair<Int, Int>>) =
mapIndexed { y, row -> row.mapIndexed { x, col -> col + points.filter { it.first == x && it.second == y }.size } }
private fun List<List<Int>>.flashes() = flatMapIndexed { y, row -> row.mapIndexedNotNull { x, col -> if (col > 9) x to y else null } }
private fun List<List<Int>>.reset() = map { row -> row.map { col -> if (col > 9) 0 else col } }
private fun List<List<Int>>.simulate(steps: Int) =
generateSequence(0 to this) { (previousFlashes, previousGrid) ->
val incrementedGrid = previousGrid.incrementAll()
val (newFlashes, newGrid, _) =
generateSequence(
Triple(0, incrementedGrid, emptySet<Pair<Int, Int>>()),
) { (currentFlashes, currentGrid, alreadyFlashed) ->
val flashes = currentGrid.flashes().minus(alreadyFlashed)
val neighbors = flashes.flatMap { currentGrid.neighbors(it.first, it.second, true) }
when (flashes.size) {
0 -> null
else -> Triple(currentFlashes + flashes.size, currentGrid.increment(neighbors), alreadyFlashed.plus(flashes))
}
}
.last()
previousFlashes + newFlashes to newGrid.reset()
}
.take(steps + 1)
.last()
override fun partOne() = grid.simulate(100).first
override fun partTwo() =
generateSequence(0 to grid) { (previousStep, previousGrid) ->
previousStep + 1 to previousGrid.simulate(1).second
}
.first { (_, grid) -> grid.flatten().all { it == 0 } }
.first
}