Skip to content

Commit

Permalink
Day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
hibob224 committed Dec 20, 2024
1 parent ec1ac81 commit f5f7750
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
59 changes: 24 additions & 35 deletions src/main/kotlin/y2024/day20/Day20.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package y2024.day20

import kotlinx.coroutines.*
import utils.getInputFile
import utils.neighboursInRadius
import utils.shortestPath
import utils.toPointGrid

fun main() {
println("Part one: " + Day20.solvePartOne())
println("Part two: " + Day20.solvePartTwo())
runBlocking {
println("Part one: " + Day20.solvePartOne())
println("Part two: " + Day20.solvePartTwo())
}
}

object Day20 {
Expand All @@ -16,45 +19,31 @@ object Day20 {
.readLines()
.toPointGrid()

fun solvePartOne(): 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)
suspend fun solvePartOne(): Int = shortcuts(maxShortcutLength = 2)

val maxShortcut = 2
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
}
// This is pretty slow, but we do what we need to do
suspend fun solvePartTwo(): Int = shortcuts(maxShortcutLength = 20)

fun solvePartTwo(): Int {
private suspend fun shortcuts(maxShortcutLength: Int): Int = withContext(Dispatchers.IO) {
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)
val (path, _) = input.shortestPath(start, target)

val biggestShortcut = path
.map { p ->
async {
val pIndex = path.indexOf(p).inc()
p.neighboursInRadius(maxShortcutLength)
.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 }
}
.filterNot { it.third <= 0 }
}
}.awaitAll().flatten()

return biggestShortcut.filter { it.third >= 100 }.size
return@withContext biggestShortcut.filter { it.third >= 100 }.size
}
}
9 changes: 7 additions & 2 deletions src/test/kotlin/y2024/day20/Day20Test.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package y2024.day20

import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

internal class Day20Test {
@Test
fun solvePartOne() {
assertEquals(1321, Day20.solvePartOne())
runBlocking {
assertEquals(1321, Day20.solvePartOne())
}
}

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

0 comments on commit f5f7750

Please sign in to comment.