From c359318235fec61adc22af336b405859a41217bb Mon Sep 17 00:00:00 2001 From: Logiar Date: Wed, 11 Dec 2024 16:42:20 +0100 Subject: [PATCH] day 11 --- day11.kts | 27 ++++++++++++++++----------- day11.kts.testvalue | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 day11.kts.testvalue diff --git a/day11.kts b/day11.kts index 98278ea..a3284ab 100755 --- a/day11.kts +++ b/day11.kts @@ -26,23 +26,28 @@ class PriorityCache(private val maxSize: Int) { return cache.containsKey(key) } } -val priorityCache = PriorityCache(20000) -fun getStonesAfter(input: List,iterations: Int, cache: PriorityCache): Int { - return input.fold(0) { acc, i -> acc + getStoneNumber(i, iterations, cache) } +val priorityCache = PriorityCache(20000) +fun getStonesAfter(input: List,iterations: Int, cache: PriorityCache): Long { + var sum = 0L + for (i in input) { + sum += getStoneNumber(i, iterations, cache) + println("Done with $i") + } + return sum } -fun getStoneNumber(number: Int, iterations: Int, cache: PriorityCache): Int { +fun getStoneNumber(number: Long, iterations: Int, cache: PriorityCache): Long { if (iterations == 0) { - return 1 + return 1L } else if (cache.isCached("$iterations-$number")) { return cache.get("$iterations-$number")!! - } else if (number == 0) { - val stonesum = getStonesAfter(listOf(3), iterations - 1, cache) + } else if (number == 0L) { + val stonesum = getStoneNumber(1, iterations - 1, cache) cache.put("$iterations-0", stonesum, iterations) return stonesum } else if (number.toString().length % 2 == 0) { val mid = number.toString().length / 2 - val left = number.toString().substring(0, mid).toInt() - val right = number.toString().substring(mid).toInt() + val left = number.toString().substring(0, mid).toLong() + val right = number.toString().substring(mid).toLong() val stonesum = getStoneNumber(left, iterations - 1, cache) + getStoneNumber(right, iterations - 1, cache) cache.put("$iterations-$number", stonesum, iterations) return stonesum @@ -53,7 +58,7 @@ fun getStoneNumber(number: Int, iterations: Int, cache: PriorityCache