aboutsummaryrefslogtreecommitdiff
path: root/src/Day11.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/Day11.kt')
-rw-r--r--src/Day11.kt74
1 files changed, 36 insertions, 38 deletions
diff --git a/src/Day11.kt b/src/Day11.kt
index 9a15344..db56d61 100644
--- a/src/Day11.kt
+++ b/src/Day11.kt
@@ -1,50 +1,48 @@
-fun flashSequence(input: Map<Pos2D, Int>) = sequence {
- val map = input.toMutableMap()
-
- while (true) {
- val flashed = mutableSetOf<Pos2D>()
- fun canFlash(entry: Map.Entry<Pos2D, Int>): Boolean = entry.value > 9 && !flashed.contains(entry.key)
-
- // 1)
- map.forEach { (pos, energy) -> map[pos] = energy + 1 }
-
- // 2)
- while (map.any(::canFlash)) {
- map
- .filter(::canFlash)
- .forEach { (pos, _) ->
- flashed.add(pos)
- Pos2D.directions8.map { pos + it }.forEach {
- if (map.containsKey(it)) {
- map[it] = map[it]!! + 1
+object Day11 {
+ private fun flashSequence(input: Map<Pos2D, Int>) = sequence {
+ val map = input.toMutableMap()
+
+ while (true) {
+ val flashed = mutableSetOf<Pos2D>()
+ fun canFlash(entry: Map.Entry<Pos2D, Int>): Boolean = entry.value > 9 && !flashed.contains(entry.key)
+
+ // 1)
+ map.forEach { (pos, energy) -> map[pos] = energy + 1 }
+
+ // 2)
+ while (map.any(::canFlash)) {
+ map
+ .filter(::canFlash)
+ .forEach { (pos, _) ->
+ flashed.add(pos)
+ Pos2D.directions8.map { pos + it }.forEach {
+ if (map.containsKey(it)) {
+ map[it] = map[it]!! + 1
+ }
}
}
- }
- }
+ }
+
+ // 3)
+ flashed.forEach { map[it] = 0 }
- // 3)
- flashed.forEach { map[it] = 0 }
+ yield(flashed.size)
+ }
+ }
- yield(flashed.size)
+ fun bothParts(input: List<String>) = flashSequence(parseToMap(input)).let { seq ->
+ seq.take(100).sum() to seq.indexOfFirst { it == 100 } + 1
}
}
fun main() {
- fun part1(input: List<String>): Int =
- flashSequence(parseToMap(input))
- .take(100)
- .sum()
-
- fun part2(input: List<String>): Int =
- flashSequence(parseToMap(input))
- .indexOfFirst { it == 100 } + 1
-
-
val testInput = readInputAsLines("Day11_test")
- check(part1(testInput) == 1656)
- check(part2(testInput) == 195)
+ val testOutput = Day11.bothParts(testInput)
+ check(testOutput.first == 1656)
+ check(testOutput.second == 195)
val input = readInputAsLines("Day11")
- println(part1(input))
- println(part2(input))
+ val output = Day11.bothParts(input)
+ println(output.first)
+ println(output.second)
}