From 778b38b3a4e5930a82fd00c9aab2d41051176f70 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Wed, 11 Dec 2024 07:54:26 +0100 Subject: [PATCH] Minor kotlinization :) --- src/aoc2024/Day11.kt | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) 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()