Day11 2021
This commit is contained in:
parent
b5e8ea0a3b
commit
118b56e5f3
63
2021/src/main/kotlin/Day11.kt
Normal file
63
2021/src/main/kotlin/Day11.kt
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
|
import be.vandewalleh.aoc.utils.BaseDay
|
||||||
|
import be.vandewalleh.aoc.utils.input.Day
|
||||||
|
|
||||||
|
@Day
|
||||||
|
class Day11 : BaseDay() {
|
||||||
|
|
||||||
|
private data class Point(val x: Int, val y: Int) {
|
||||||
|
fun adjacents() = listOf(
|
||||||
|
copy(y = y - 1),
|
||||||
|
copy(x = x + 1),
|
||||||
|
copy(y = y + 1),
|
||||||
|
copy(x = x - 1),
|
||||||
|
copy(y = y - 1, x = x - 1),
|
||||||
|
copy(x = x + 1, y = y + 1),
|
||||||
|
copy(y = y + 1, x = x - 1),
|
||||||
|
copy(x = x + 1, y = y - 1),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val map by lazy {
|
||||||
|
input.lines.value.flatMapIndexed { y, line ->
|
||||||
|
line.mapIndexed { x, value -> Point(x, y) to value.digitToInt() }
|
||||||
|
}.toMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun part1(): Int {
|
||||||
|
val map = map.toMap(HashMap())
|
||||||
|
var count = 0
|
||||||
|
repeat(100) { count += step(map) }
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun step(map: HashMap<Point, Int>): Int {
|
||||||
|
map.keys.forEach { map[it] = map[it]!! + 1 }
|
||||||
|
val flashed = hashSetOf<Point>()
|
||||||
|
while (true) {
|
||||||
|
val new = map.entries.asSequence()
|
||||||
|
.filter { it.value > 9 }
|
||||||
|
.filter { it.key !in flashed }
|
||||||
|
.onEach { flashed += it.key }
|
||||||
|
.flatMap { it.key.adjacents() }
|
||||||
|
.filter { it in map }
|
||||||
|
.toList()
|
||||||
|
|
||||||
|
if (new.isEmpty()) break
|
||||||
|
new.forEach { map[it] = map[it]!! + 1 }
|
||||||
|
}
|
||||||
|
|
||||||
|
map.entries.filter { it.value > 9 }
|
||||||
|
.map { it.key }
|
||||||
|
.forEach { map[it] = 0 }
|
||||||
|
|
||||||
|
return flashed.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun part2(): Int {
|
||||||
|
val map = map.toMap(HashMap())
|
||||||
|
repeat(10000) { if (step(map) == map.size) return it + 1 }
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
10
2021/src/main/resources/day11.txt
Normal file
10
2021/src/main/resources/day11.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
7222221271
|
||||||
|
6463754232
|
||||||
|
3373484684
|
||||||
|
4674461265
|
||||||
|
1187834788
|
||||||
|
1175316351
|
||||||
|
8211411846
|
||||||
|
4657828333
|
||||||
|
5286325337
|
||||||
|
5771324832
|
||||||
22
2021/src/test/kotlin/Day11Test.kt
Normal file
22
2021/src/test/kotlin/Day11Test.kt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
|
class Day11Test : BaseDayTest(11) {
|
||||||
|
override val example = """
|
||||||
|
5483143223
|
||||||
|
2745854711
|
||||||
|
5264556173
|
||||||
|
6141336146
|
||||||
|
6357385478
|
||||||
|
4167524645
|
||||||
|
2176841721
|
||||||
|
6882881134
|
||||||
|
4846848554
|
||||||
|
5283751526
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
override val part1Example = 1656
|
||||||
|
override val part1Answer = 1661
|
||||||
|
|
||||||
|
override val part2Example = 195
|
||||||
|
override val part2Answer = 334
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user