Skip to content

Commit

Permalink
2024 - Day 10 - cleaned up a bit - no need to !! the gets from the ma…
Browse files Browse the repository at this point in the history
…p - just appriciate them being null (reduces the number of filterings)
  • Loading branch information
fmmr committed Dec 10, 2024
1 parent 52fd811 commit c81e732
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/main/kotlin/no/rodland/advent_2024/Day10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ class Day10(val input: List<String>) : Day<Int, Int, Map<Pos, Int>> {
return heads.sumOf { it.rating(emptyList(), mutableSetOf()) }
}

private fun Pos.rating(path: List<Pos>, visited: MutableSet<List<Pos>>): 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<Pos>): 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<Pos>): 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<Pos>, visited: MutableSet<List<Pos>>): 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<String>.parse(): Map<Pos, Int> {
Expand Down

0 comments on commit c81e732

Please sign in to comment.