Skip to content

Commit

Permalink
Clean up part 10
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Dec 10, 2024
1 parent 8f5a08b commit f187598
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions 2024/src/main/kotlin/aoc/year2024/Day10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,32 @@ object Day10 : Puzzle<Int, Int>(day = 10) {
): Int {
val mountains = input.lines().flatMapIndexed { y, line ->
line.mapIndexed { x, char ->
Point(x, y) to (char.digitToIntOrNull() ?: -1)
Point(x, y) to char.digitToIntOrNull()
}
}.toMap()
return mountains.toList()
.sumOf { (point, height) ->
if (height == 0) {
val reachedNines = mutableListOf<Point>()
fun visit(
currentHeight: Int,
point: Point,
) {
val heightAtPoint = mountains[point] ?: return
if (heightAtPoint == currentHeight + 1) {
if (heightAtPoint == 9) {
reachedNines += point
} else {
point.adjacentOrthogonal().forEach {
visit(heightAtPoint, it)
}
}
}
}
visit(-1, point)
if (countUniqueTrails) {
reachedNines.toSet().size
} else {
reachedNines.size
}

fun visit(
currentHeight: Int,
point: Point,
reachedNines: MutableList<Point>,
) {
val heightAtPoint = mountains[point] ?: return
if (heightAtPoint == currentHeight + 1) {
if (heightAtPoint == 9) {
reachedNines += point
} else {
0
point.adjacentOrthogonal().forEach { adjacent ->
visit(heightAtPoint, adjacent, reachedNines)
}
}
}
}
return mountains.toList()
.filter { (_, height) -> height == 0 }
.sumOf { (start, _) ->
val reachedNines = mutableListOf<Point>()
visit(-1, start, reachedNines)
if (countUniqueTrails) reachedNines.toSet().size else reachedNines.size
}
}
}

0 comments on commit f187598

Please sign in to comment.