Skip to content

Commit

Permalink
Merge pull request #84 from ephemient/kt/day11
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 11, 2024
2 parents 8c3d142 + 8b4384d commit 3b1b716
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|||
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -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 <K> MutableMap<K, Long>.incBy(key: K, value: Long) =
put(key, getOrElse(key) { 0 } + value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ val days: List<Day> = 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 3b1b716

Please sign in to comment.