From 8b4384d5337e93741ca3a05aab95950533375333 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Wed, 11 Dec 2024 01:42:01 -0500 Subject: [PATCH] Day 11: Plutonian Pebbles --- README.md | 2 +- .../ephemient/aoc2024/exe/Day11Bench.kt | 31 ++++++++++++++ .../com/github/ephemient/aoc2024/Day11.kt | 42 +++++++++++++++++++ .../com/github/ephemient/aoc2024/Days.kt | 1 + .../com/github/ephemient/aoc2024/Day11Test.kt | 24 +++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day11Bench.kt create mode 100644 kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt create mode 100644 kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day11Test.kt diff --git a/README.md b/README.md index 15f9a82..4ef7297 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ Development occurs in language-specific directories: |[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt)|[day8.py](py/aoc2024/day8.py)|[day8.rs](rs/src/day8.rs)| |[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.kt)|[day9.py](py/aoc2024/day9.py)|[day9.rs](rs/src/day9.rs)| |[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)|[day10.rs](rs/src/day10.rs)| -|[Day11.hs](hs/src/Day11.hs)|||| +|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)||| diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day11Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day11Bench.kt new file mode 100644 index 0000000..67c6bfa --- /dev/null +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day11Bench.kt @@ -0,0 +1,31 @@ +package com.github.ephemient.aoc2024.exe + +import com.github.ephemient.aoc2024.Day11 +import kotlinx.benchmark.Benchmark +import kotlinx.benchmark.Blackhole +import kotlinx.benchmark.Scope +import kotlinx.benchmark.Setup +import kotlinx.benchmark.State + +@State(Scope.Benchmark) +class Day11Bench { + private lateinit var input: String + + @Setup + fun setup() { + input = getDayInput(11) + } + + @Benchmark + fun part1() = Day11(input).part1() + + @Benchmark + fun part2() = Day11(input).part2() + + @Benchmark + fun solve(bh: Blackhole) { + val day11 = Day11(input) + bh.consume(day11.part1()) + bh.consume(day11.part2()) + } +} \ No newline at end of file diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt new file mode 100644 index 0000000..57115e6 --- /dev/null +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt @@ -0,0 +1,42 @@ +package com.github.ephemient.aoc2024 + +class Day11(input: String) { + private val input = buildMap { + for (word in input.splitToSequence(WHITESPACE)) { + incBy(word.toLongOrNull() ?: continue, 1) + } + } + + fun part1() = solve(25) + + fun part2() = solve(75) + + internal fun solve(n: Int): Long { + var counts = input + repeat(n) { + counts = buildMap { + for ((num, count) in counts) { + if (num == 0L) { + incBy(1, count) + } else { + val string = num.toString() + if (string.length % 2 == 0) { + incBy(string.take(string.length / 2).toLong(), count) + incBy(string.drop(string.length / 2).toLong(), count) + } else { + incBy(2024 * num, count) + } + } + } + } + } + return counts.values.sum() + } + + companion object { + private val WHITESPACE = """\s+""".toRegex() + + private fun MutableMap.incBy(key: K, value: Long) = + put(key, getOrElse(key) { 0 } + value) + } +} diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Days.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Days.kt index 7c172ee..7db5877 100644 --- a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Days.kt +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Days.kt @@ -11,6 +11,7 @@ val days: List = listOf( Day(8, ::Day8, Day8::part1, Day8::part2), Day(9, ::Day9, Day9::part1, Day9::part2), Day(10, ::Day10, Day10::part1, Day10::part2), + Day(11, ::Day11, Day11::part1, Day11::part2), ) data class Day( diff --git a/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day11Test.kt b/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day11Test.kt new file mode 100644 index 0000000..9ddf2e7 --- /dev/null +++ b/kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day11Test.kt @@ -0,0 +1,24 @@ +package com.github.ephemient.aoc2024 + +import kotlin.test.Test +import kotlin.test.assertEquals + +class Day11Test { + @Test + fun part1() { + assertEquals(7, Day11(example1).solve(1)) + assertEquals(22, Day11(example2).solve(6)) + assertEquals(55312, Day11(example2).solve(25)) + } + + companion object { + private val example1 = + """ + |0 1 10 99 999 + |""".trimMargin() + private val example2 = + """ + |125 17 + |""".trimMargin() + } +}