Day 10 part 2
This commit is contained in:
parent
87dfaa03e7
commit
fb7549498e
1 changed files with 22 additions and 0 deletions
|
@ -7,6 +7,13 @@ fun main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Part 1: $part1")
|
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 {
|
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 ->
|
.fold(emptySet()) { acc, next ->
|
||||||
acc + pathsToTop(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)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue