diff --git a/2024/src/main/kotlin/day10.kt b/2024/src/main/kotlin/day10.kt index 5b0ee5a..1bfb32e 100644 --- a/2024/src/main/kotlin/day10.kt +++ b/2024/src/main/kotlin/day10.kt @@ -7,6 +7,13 @@ fun main() { } println("Part 1: $part1") + + val part2 = map.findCoordinatesOf(0) + .fold(0) { acc, start -> + acc + map.pathRating(start) + } + + println("Part 2: $part2") } fun Grid.findCoordinatesOf(value: T) = sequence { @@ -32,4 +39,19 @@ fun Grid.pathsToTop(coordinate: Grid.Coordinate): Set { .fold(emptySet()) { acc, next -> acc + pathsToTop(next) } +} + +fun Grid.pathRating(coordinate: Grid.Coordinate): Int { + val height = get(coordinate) + + if (height == 9) { + return 1 + } + + return Direction.entries + .map { coordinate.step(it) } + .filter { it in this && this[it] == height + 1 } + .fold(0) { acc, next -> + acc + pathRating(next) + } } \ No newline at end of file