Skip to content

Commit

Permalink
Day 10: Hoof It
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 10, 2024
1 parent a619b6d commit b364842
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Development occurs in language-specific directories:
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)|[day7.rs](rs/src/day7.rs)|
|[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.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.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.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())
}
}
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,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ val days: List<Day> = listOf(
Day(7, ::Day7, Day7::part1, Day7::part2),
Day(8, ::Day8, Day8::part1, Day8::part2),
Day(9, ::Day9, Day9::part1, Day9::part2),
Day(10, ::Day10, Day10::part1, Day10::part2),
)

data class Day(
Expand Down
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()
}
}

0 comments on commit b364842

Please sign in to comment.