diff --git a/src/main/kotlin/no/rodland/advent_2024/Day10.kt b/src/main/kotlin/no/rodland/advent_2024/Day10.kt index af2737e4..6c431bea 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day10.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day10.kt @@ -18,26 +18,25 @@ class Day10(val input: List) : Day> { return heads.sumOf { it.rating(emptyList(), mutableSetOf()) } } - private fun Pos.rating(path: List, visited: MutableSet>): Int { - val newPath = path + this - visited.add(newPath) - val c = grid[this]!! - return if (c == 9) 1 - else neighbourCellsUDLR().filterNot { (newPath + it) in visited } - .filter { it in grid } - .filter { grid[it]!! == c + 1 } - .sumOf { it.rating(newPath, visited) } + private fun Pos.score(visited: MutableSet): Int { + return grid[this]?.let { c -> + visited.add(this) + if (c == 9) 1 + else neighbourCellsUDLR().filterNot { it in visited } + .filter { grid[it] == c + 1 } + .sumOf { it.score(visited) } + } ?: 0 } - - private fun Pos.score(visited: MutableSet): Int { - visited.add(this) - val c = grid[this]!! - return if (c == 9) 1 - else neighbourCellsUDLR().filterNot { it in visited } - .filter { it in grid } - .filter { grid[it] == c + 1 } - .sumOf { it.score(visited) } + private fun Pos.rating(path: List, visited: MutableSet>): Int { + return grid[this]?.let { c -> + val newPath = path + this + visited.add(newPath) + if (c == 9) 1 + else neighbourCellsUDLR().filterNot { (newPath + it) in visited } + .filter { grid[it] == c + 1 } + .sumOf { it.rating(newPath, visited) } + } ?: 0 } override fun List.parse(): Map {