Day 10 part 2

This commit is contained in:
Sven Weidauer 2024-12-11 20:45:49 +01:00
parent 87dfaa03e7
commit fb7549498e

View file

@ -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 <T> Grid<T>.findCoordinatesOf(value: T) = sequence {
@ -32,4 +39,19 @@ fun Grid<Int>.pathsToTop(coordinate: Grid.Coordinate): Set<Grid.Coordinate> {
.fold(emptySet()) { acc, next ->
acc + pathsToTop(next)
}
}
fun Grid<Int>.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)
}
}