From ec1ac81afeb77ae469340e88c13c4499de3d150d Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Fri, 20 Dec 2024 16:38:14 +0000 Subject: [PATCH] D20P2 --- src/main/kotlin/y2024/day20/Day20.kt | 21 +++++++++++++++++++-- src/test/kotlin/y2024/day20/Day20Test.kt | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/y2024/day20/Day20.kt b/src/main/kotlin/y2024/day20/Day20.kt index e74e6cd..278ecf3 100644 --- a/src/main/kotlin/y2024/day20/Day20.kt +++ b/src/main/kotlin/y2024/day20/Day20.kt @@ -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 } } diff --git a/src/test/kotlin/y2024/day20/Day20Test.kt b/src/test/kotlin/y2024/day20/Day20Test.kt index b436747..8dcc3b7 100644 --- a/src/test/kotlin/y2024/day20/Day20Test.kt +++ b/src/test/kotlin/y2024/day20/Day20Test.kt @@ -11,6 +11,6 @@ internal class Day20Test { @Test fun solvePartTwo() { - assertEquals(0, Day20.solvePartTwo()) + assertEquals(971737, Day20.solvePartTwo()) } }