Skip to content

Commit

Permalink
D20P2
Browse files Browse the repository at this point in the history
  • Loading branch information
hibob224 committed Dec 20, 2024
1 parent cf1cc99 commit ec1ac81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/main/kotlin/y2024/day20/Day20.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ object Day20 {
return biggestShortcut.filter { it.third >= 100 }.size
}

fun solvePartTwo(): Long {
return 0
fun solvePartTwo(): Int {
val start = input.entries.find { it.value == 'S' }!!.key
val target = input.entries.find { it.value == 'E' }!!.key

val (path, dist) = input.shortestPath(start, target)

val maxShortcut = 20
val biggestShortcut = path.flatMap { p ->
val pIndex = path.indexOf(p).inc()
p.neighboursInRadius(maxShortcut)
.mapNotNull {
val ind = if (it == target) path.size + 1 else path.indexOf(it).takeIf { it != -1 }?.inc() ?: return@mapNotNull null
val diff = ind - pIndex - p.manhattan(it)
Triple(p, it, diff)
}
.filterNot { it.third <= 0 }
}

return biggestShortcut.filter { it.third >= 100 }.size
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/y2024/day20/Day20Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ internal class Day20Test {

@Test
fun solvePartTwo() {
assertEquals(0, Day20.solvePartTwo())
assertEquals(971737, Day20.solvePartTwo())
}
}

0 comments on commit ec1ac81

Please sign in to comment.