Skip to content

Commit

Permalink
2024 - Day 10 - refactored after talking with chriswk.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 10, 2024
1 parent c8a7322 commit 0a24a78
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
30 changes: 10 additions & 20 deletions src/main/kotlin/no/rodland/advent_2024/Day10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,23 @@ class Day10(val input: List<String>) : Day<Int, Int, Map<Pos, Int>> {
private val heads = grid.filterValues { it == 0 }.keys

override fun partOne(): Int {
return heads.sumOf { it.score(mutableSetOf()) }
return heads.sumOf {
it.score().toSet().size
}
}

override fun partTwo(): Int {
return heads.sumOf { it.rating(emptyList(), mutableSetOf()) }
return heads.sumOf {
it.score().size
}
}

// part 1
private fun Pos.score(visited: MutableSet<Pos>): Int {
private fun Pos.score(): List<Pos> {
val c = grid[this]!!
visited.add(this)
return if (c == 9) 1
else neighbourCellsUDLR().filterNot { it in visited }
return if (c == 9) listOf(this)
else neighbourCellsUDLR()
.filter { grid[it] == c + 1 }
.sumOf { it.score(visited) }
}

// part 2
private fun Pos.rating(path: List<Pos>, visited: MutableSet<List<Pos>>): Int {
val c = grid[this]!!
val newPath = path + this
visited.add(newPath)
return if (c == 9) 1
else neighbourCellsUDLR().filterNot { (newPath + it) in visited }
.filter { grid[it] == c + 1 }
.sumOf { it.rating(newPath, visited) }
.flatMap { it.score() }
}

override fun List<String>.parse(): Map<Pos, Int> {
Expand All @@ -48,7 +39,6 @@ class Day10(val input: List<String>) : Day<Int, Int, Map<Pos, Int>> {
}

override val day = "10".toInt()

}


Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/rodland/advent_2024/Day10Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class Day10Test {
{ Day10(data10) },
{ Day10(test10) },
numTestPart1 = 200,
numTestPart2 = 50,
numTestPart2 = 200,
)

@Nested
Expand Down

0 comments on commit 0a24a78

Please sign in to comment.