From 68d6f71f0cfb269ab81585d28498fba3cdc4d20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Wed, 11 Dec 2024 10:39:06 +0100 Subject: [PATCH] 2024 - Day 11 - part 1 & 2 --- .../kotlin/no/rodland/advent_2024/Day11.kt | 51 ++++++++++++++++--- .../no/rodland/advent_2024/Day11Test.kt | 10 ++-- src/test/resources/2024/input_11_test.txt | 1 + 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2024/Day11.kt b/src/main/kotlin/no/rodland/advent_2024/Day11.kt index 0bbd06a8..dfbd73fd 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day11.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day11.kt @@ -5,23 +5,60 @@ import no.rodland.advent.Day // template generated: 11/12/2024 // Fredrik Rødland 2024 -class Day11(val input: List) : Day> { +class Day11(val input: List) : Day> { private val parsed = input.parse() - override fun partOne(): Long { - return 2 + val memoization = mutableMapOf, Long>() + return parsed.map { it.numberStones(25, memoization) }.sumOf { it } } override fun partTwo(): Long { - return 2 + val memoization = mutableMapOf, Long>() + return parsed.map { it.numberStones(75, memoization) }.sumOf { it } + } + + private fun Long.numberStones(i: Int, memoization: MutableMap, Long>): Long { + if (i == 0) return 1 + if (memoization.containsKey(this to i)) { + return memoization[this to i]!! + } + return this.runRules().sumOf { it.numberStones(i - 1, memoization) }.also { memoization[this to i] = it } + } + + private fun Long.runRules(): List { + return when { + this == 0L -> listOf(1L) + this.evenDigits() -> this.split() + else -> listOf(2024 * this) + } + } + + private fun Long.split(): List = "$this".let { + val middle = "$this".length / 2 + listOf(it.substring(0, middle).toLong(), it.substring(middle).toLong()) } - override fun List.parse(): List { - return map { line -> - line + private fun Long.evenDigits(): Boolean = "$this".length % 2 == 0 + + @Suppress("unused") + fun partOneBruteForce(): Long { + var l = parsed + repeat(25) { + l = l.fold(emptyList()) { acc, num -> + acc + num.runRules() + } } + return l.size.toLong() + } + + override fun List.parse(): List { + return first().split("\\s+".toRegex()).map { it.toLong() } } override val day = "11".toInt() } + + + + diff --git a/src/test/kotlin/no/rodland/advent_2024/Day11Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day11Test.kt index 6dc4b220..53ed50a1 100644 --- a/src/test/kotlin/no/rodland/advent_2024/Day11Test.kt +++ b/src/test/kotlin/no/rodland/advent_2024/Day11Test.kt @@ -15,10 +15,10 @@ internal class Day11Test { private val data11 = "2024/input_11.txt".readFile() private val test11 = "2024/input_11_test.txt".readFile() - private val resultTestOne = 2L - private val resultTestTwo = 2L - private val resultOne = 2L - private val resultTwo = 2L + private val resultTestOne = 55312L + private val resultTestTwo = 65601038650482L + private val resultOne = 172484L + private val resultTwo = 205913561055242L val test = defaultTestSuiteParseOnInit( Day11(data11), @@ -29,6 +29,8 @@ internal class Day11Test { resultTwo, { Day11(data11) }, { Day11(test11) }, + numTestPart1 = 100, + numTestPart2 = 3 ) @Nested diff --git a/src/test/resources/2024/input_11_test.txt b/src/test/resources/2024/input_11_test.txt index e69de29b..528f9d50 100644 --- a/src/test/resources/2024/input_11_test.txt +++ b/src/test/resources/2024/input_11_test.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file