-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from ephemient/kt/day11
- Loading branch information
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day11Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day11Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |