Skip to content

Commit 96f1a5a

Browse files
committed
Add solution for Y2024D11.
1 parent 4c52726 commit 96f1a5a

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _"Anything that's worth doing, is worth overdoing."_
1515
Table of contents to jump straight to the problem you're looking for.
1616

1717
<details open>
18-
<summary>2024 (20x⭐)</summary>
18+
<summary>2024 (22x⭐)</summary>
1919

2020
| Day | Title | Stars |
2121
|:---:|:-----------------------------------------------------------|:-----:|
@@ -29,6 +29,7 @@ Table of contents to jump straight to the problem you're looking for.
2929
| 08 | [Resonant Collinearity](solutions/aockt/y2024/Y2024D08.kt) | ⭐⭐ |
3030
| 09 | [Disk Fragmenter](solutions/aockt/y2024/Y2024D09.kt) | ⭐⭐ |
3131
| 10 | [Hoof It](solutions/aockt/y2024/Y2024D10.kt) | ⭐⭐ |
32+
| 11 | [Plutonian Pebbles](solutions/aockt/y2024/Y2024D11.kt) | ⭐⭐ |
3233

3334
</details>
3435

Diff for: inputs/aockt/y2024/d11/input.txt

59 Bytes
Binary file not shown.

Diff for: inputs/aockt/y2024/d11/solution_part1.txt

29 Bytes
Binary file not shown.

Diff for: inputs/aockt/y2024/d11/solution_part2.txt

38 Bytes
Binary file not shown.

Diff for: solutions/aockt/y2024/Y2024D11.kt

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package aockt.y2024
2+
3+
import aockt.util.parse
4+
import aockt.y2024.Y2024D11.Stone
5+
import io.github.jadarma.aockt.core.Solution
6+
import kotlin.math.floor
7+
import kotlin.math.log10
8+
import kotlin.math.pow
9+
10+
object Y2024D11 : Solution {
11+
12+
/** A strange stone from Pluto, wih a [number] engraved on it. */
13+
@JvmInline
14+
private value class Stone(val number: Long) {
15+
16+
/** Returns the stone(s) that will appear after this one is blinked at. */
17+
fun blink(): List<Stone> = buildList {
18+
if (number == 0L) { add(Stone(1L)); return@buildList }
19+
20+
val digitCount = floor(log10(number.toDouble())).toInt() + 1
21+
if (digitCount % 2 == 1) { add(Stone(number * 2024L)); return@buildList }
22+
23+
val split = 10.0.pow(digitCount / 2).toLong()
24+
add(Stone(number / split))
25+
add(Stone(number % split))
26+
}
27+
}
28+
29+
/** Calculates how many stones will appear after a number of [blinks] from the starting [stones]. */
30+
private fun solve(stones: List<Stone>, blinks: Int): Long {
31+
val cache = mutableMapOf<Pair<Stone, Int>, Long>()
32+
33+
fun recurse(stone: Stone, times: Int): Long = when (times) {
34+
0 -> 1
35+
else -> cache.getOrPut(stone to times) {
36+
stone.blink().sumOf { recurse(it, times - 1) }
37+
}
38+
}
39+
40+
return stones.sumOf { stone -> recurse(stone, blinks) }
41+
}
42+
43+
/** Parse the [input] and return the initial rows of [Stone]s. */
44+
private fun parseInput(input: String): List<Stone> = parse { input.split(' ').map { Stone(it.toLong()) } }
45+
46+
override fun partOne(input: String): Long = solve(stones = parseInput(input), blinks = 25)
47+
override fun partTwo(input: String): Long = solve(stones = parseInput(input), blinks = 75)
48+
}

Diff for: tests/aockt/y2024/Y2024D11Test.kt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package aockt.y2024
2+
3+
import io.github.jadarma.aockt.test.AdventDay
4+
import io.github.jadarma.aockt.test.AdventSpec
5+
6+
@AdventDay(2024, 11, "Plutonian Pebbles")
7+
class Y2024D11Test : AdventSpec<Y2024D11>({
8+
val exampleInput = "125 17"
9+
partOne { exampleInput shouldOutput 55_312 }
10+
partTwo { exampleInput shouldOutput 65_601_038_650_482 }
11+
})

0 commit comments

Comments
 (0)