-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11PlutonianPebbles.kt
42 lines (33 loc) · 1.47 KB
/
Day11PlutonianPebbles.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
package adventofcode.year2024
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day11PlutonianPebbles(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val stones by lazy { input.split(" ").map(String::toLong) }
private val cache = mutableMapOf<Pair<Long, Int>, Long>()
override fun partOne() = stones.sumOf { stone -> blink(stone, cache, 25) }
override fun partTwo() = stones.sumOf { stone -> blink(stone, cache, 75) }
companion object {
private fun blink(
stone: Long,
cache: MutableMap<Pair<Long, Int>, Long>,
count: Int,
): Long =
when {
count == 0 -> 1
stone to count in cache -> cache.getValue(stone to count)
else -> {
val result =
when {
stone == 0L -> blink(1L, cache, count - 1)
stone.hasEvenDigits() -> stone.halves().sumOf { half -> blink(half, cache, count - 1) }
else -> blink(stone * 2024, cache, count - 1)
}
cache[stone to count] = result
result
}
}
private fun Long.hasEvenDigits() = toString().length % 2 == 0
private fun Long.halves() =
listOf(toString().substring(0, toString().length / 2), toString().substring(toString().length / 2)).map(String::toLong)
}
}