Day 10 part 1
This commit is contained in:
parent
d9172dabbf
commit
87dfaa03e7
3 changed files with 91 additions and 0 deletions
35
2024/src/main/kotlin/day10.kt
Normal file
35
2024/src/main/kotlin/day10.kt
Normal file
|
@ -0,0 +1,35 @@
|
|||
fun main() {
|
||||
val map = CharGrid.read("day10.txt").map { it.digitToInt() }
|
||||
|
||||
val part1 = map.findCoordinatesOf(0)
|
||||
.fold(0) { acc, start ->
|
||||
acc + map.pathsToTop(start).size
|
||||
}
|
||||
|
||||
println("Part 1: $part1")
|
||||
}
|
||||
|
||||
fun <T> Grid<T>.findCoordinatesOf(value: T) = sequence {
|
||||
for (y in 0..<height) {
|
||||
for (x in 0..<width) {
|
||||
if (get(x, y) == value) {
|
||||
yield(Grid.Coordinate(x, y))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Grid<Int>.pathsToTop(coordinate: Grid.Coordinate): Set<Grid.Coordinate> {
|
||||
val height = get(coordinate)
|
||||
|
||||
if (height == 9) {
|
||||
return setOf(coordinate)
|
||||
}
|
||||
|
||||
return Direction.entries
|
||||
.map { coordinate.step(it) }
|
||||
.filter { it in this && this[it] == height + 1 }
|
||||
.fold(emptySet()) { acc, next ->
|
||||
acc + pathsToTop(next)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue