diff --git a/src/aoc2024/Day11.kt b/src/aoc2024/Day11.kt index 683614b..ac422b1 100644 --- a/src/aoc2024/Day11.kt +++ b/src/aoc2024/Day11.kt @@ -24,26 +24,22 @@ fun main() { return -1 } - fun rec(n: Long, i: Int): Long { - //println("$i $n") - if (i == 0) return 1 - val ll = lookupTable[n to i] - if (ll != null) return ll - val result = if (n == 0L) { - rec(1L, i - 1) - } else { - val size = fastCeilLog10(n) - if (size and 1 == 0) { - val left = n / powTenTable[size / 2] - val right = n % powTenTable[size / 2] - rec(left, i - 1) + rec(right, i - 1) + fun rec(n: Long, i: Int): Long = + if (i == 0) 1 else lookupTable.getOrPut(n to i) { + if (n == 0L) { + rec(1L, i - 1) } else { - rec(n * 2024L, i - 1) + val size = fastCeilLog10(n) + if (size and 1 == 0) { + val left = n / powTenTable[size / 2] + val right = n % powTenTable[size / 2] + rec(left, i - 1) + rec(right, i - 1) + } else { + rec(n * 2024L, i - 1) + } } } - lookupTable[n to i] = result - return result - } + fun part1(input: List): Long { val numbers = input[0].splitLongs()