-
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 #77 from ephemient/kt/day10
- Loading branch information
Showing
5 changed files
with
104 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/Day10Bench.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.Day10 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day10Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(10) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day10(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day10(input).part2() | ||
|
||
@Benchmark | ||
fun solve(bh: Blackhole) { | ||
val day10 = Day10(input) | ||
bh.consume(day10.part1()) | ||
bh.consume(day10.part2()) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.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,41 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day10(input: String) { | ||
private val levels: List<Set<IntPair>> | ||
init { | ||
val levels = List(10) { mutableSetOf<IntPair>() } | ||
for ((y, line) in input.lineSequence().withIndex()) { | ||
for ((x, char) in line.withIndex()) { | ||
if (char.isDigit()) levels[char.digitToInt()].add(y to x) | ||
} | ||
} | ||
this.levels = levels | ||
} | ||
|
||
private inline fun <T> bfs(start: (IntPair) -> T, plus: (T, T) -> T): Map<IntPair, T> = | ||
levels.subList(1, 10).fold(levels[0].associateWith(start)) { acc, points -> | ||
buildMap { | ||
for ((key, value) in acc) { | ||
for (point in key.adj) { | ||
if (point in points) { | ||
put(point, if (contains(point)) plus(value, getValue(point)) else value) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun part1() = bfs(::setOf, Set<IntPair>::plus).values.sumOf { it.size } | ||
|
||
fun part2() = bfs({ 1 }, Int::plus).values.sum() | ||
|
||
companion object { | ||
private val IntPair.adj: List<IntPair> | ||
get() = listOf( | ||
first - 1 to second, | ||
first to second - 1, | ||
first to second + 1, | ||
first + 1 to second, | ||
) | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day10Test.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,30 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day10Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(36, Day10(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(81, Day10(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|89010123 | ||
|78121874 | ||
|87430965 | ||
|96549874 | ||
|45678903 | ||
|32019012 | ||
|01329801 | ||
|10456732 | ||
|""".trimMargin() | ||
} | ||
} |