Skip to content

Commit

Permalink
2024 - Day 11 - part 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 11, 2024
1 parent 32c7b1f commit 68d6f71
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
51 changes: 44 additions & 7 deletions src/main/kotlin/no/rodland/advent_2024/Day11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,60 @@ import no.rodland.advent.Day
// template generated: 11/12/2024
// Fredrik Rødland 2024

class Day11(val input: List<String>) : Day<Long, Long, List<String>> {
class Day11(val input: List<String>) : Day<Long, Long, List<Long>> {

private val parsed = input.parse()

override fun partOne(): Long {
return 2
val memoization = mutableMapOf<Pair<Long, Int>, Long>()
return parsed.map { it.numberStones(25, memoization) }.sumOf { it }
}

override fun partTwo(): Long {
return 2
val memoization = mutableMapOf<Pair<Long, Int>, Long>()
return parsed.map { it.numberStones(75, memoization) }.sumOf { it }
}

private fun Long.numberStones(i: Int, memoization: MutableMap<Pair<Long, Int>, 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<Long> {
return when {
this == 0L -> listOf(1L)
this.evenDigits() -> this.split()
else -> listOf(2024 * this)
}
}

private fun Long.split(): List<Long> = "$this".let {
val middle = "$this".length / 2
listOf(it.substring(0, middle).toLong(), it.substring(middle).toLong())
}

override fun List<String>.parse(): List<String> {
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<String>.parse(): List<Long> {
return first().split("\\s+".toRegex()).map { it.toLong() }
}

override val day = "11".toInt()
}




10 changes: 6 additions & 4 deletions src/test/kotlin/no/rodland/advent_2024/Day11Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -29,6 +29,8 @@ internal class Day11Test {
resultTwo,
{ Day11(data11) },
{ Day11(test11) },
numTestPart1 = 100,
numTestPart2 = 3
)

@Nested
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/2024/input_11_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17

0 comments on commit 68d6f71

Please sign in to comment.