day 11
All checks were successful
Solve / example-action (push) Successful in 6m6s

This commit is contained in:
Logiar 2024-12-11 16:42:20 +01:00
parent 162b48d5cb
commit c359318235
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
2 changed files with 34 additions and 11 deletions

View File

@ -26,23 +26,28 @@ class PriorityCache<K, V>(private val maxSize: Int) {
return cache.containsKey(key) return cache.containsKey(key)
} }
} }
val priorityCache = PriorityCache<String, Int>(20000) val priorityCache = PriorityCache<String, Long>(20000)
fun getStonesAfter(input: List<Int>,iterations: Int, cache: PriorityCache<String, Int>): Int { fun getStonesAfter(input: List<Long>,iterations: Int, cache: PriorityCache<String, Long>): Long {
return input.fold(0) { acc, i -> acc + getStoneNumber(i, iterations, cache) } var sum = 0L
for (i in input) {
sum += getStoneNumber(i, iterations, cache)
println("Done with $i")
} }
fun getStoneNumber(number: Int, iterations: Int, cache: PriorityCache<String, Int>): Int { return sum
}
fun getStoneNumber(number: Long, iterations: Int, cache: PriorityCache<String, Long>): Long {
if (iterations == 0) { if (iterations == 0) {
return 1 return 1L
} else if (cache.isCached("$iterations-$number")) { } else if (cache.isCached("$iterations-$number")) {
return cache.get("$iterations-$number")!! return cache.get("$iterations-$number")!!
} else if (number == 0) { } else if (number == 0L) {
val stonesum = getStonesAfter(listOf(3), iterations - 1, cache) val stonesum = getStoneNumber(1, iterations - 1, cache)
cache.put("$iterations-0", stonesum, iterations) cache.put("$iterations-0", stonesum, iterations)
return stonesum return stonesum
} else if (number.toString().length % 2 == 0) { } else if (number.toString().length % 2 == 0) {
val mid = number.toString().length / 2 val mid = number.toString().length / 2
val left = number.toString().substring(0, mid).toInt() val left = number.toString().substring(0, mid).toLong()
val right = number.toString().substring(mid).toInt() val right = number.toString().substring(mid).toLong()
val stonesum = getStoneNumber(left, iterations - 1, cache) + getStoneNumber(right, iterations - 1, cache) val stonesum = getStoneNumber(left, iterations - 1, cache) + getStoneNumber(right, iterations - 1, cache)
cache.put("$iterations-$number", stonesum, iterations) cache.put("$iterations-$number", stonesum, iterations)
return stonesum return stonesum
@ -53,7 +58,7 @@ fun getStoneNumber(number: Int, iterations: Int, cache: PriorityCache<String, In
return stonesum return stonesum
} }
} }
val input = scanner.nextLine().split(" ").map { it.toInt() } val input = scanner.nextLine().split(" ").map { it.toLong() }
println("First part with 25 iterations: ${getStonesAfter(input, 25, priorityCache)}") println("First part with 25 iterations: ${getStonesAfter(input, 25, priorityCache)}")
println("Second part with 75 iterations: ${getStonesAfter(input, 75, priorityCache)}")

18
day11.kts.testvalue Normal file
View File

@ -0,0 +1,18 @@
Done with 2
Done with 54
Done with 992917
Done with 5270417
Done with 2514
Done with 28561
Done with 0
Done with 990
First part with 25 iterations: 222461
Done with 2
Done with 54
Done with 992917
Done with 5270417
Done with 2514
Done with 28561
Done with 0
Done with 990
Second part with 75 iterations: 264350935776416