-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
214 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/adventofcode/year2024/Day20RaceCondition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package adventofcode.year2024 | ||
|
||
import adventofcode.Puzzle | ||
import adventofcode.PuzzleInput | ||
import adventofcode.common.spatial.Grid2d | ||
import adventofcode.common.spatial.Point2d | ||
|
||
class Day20RaceCondition(customInput: PuzzleInput? = null) : Puzzle(customInput) { | ||
private val racetrack by lazy { input.racketrack() } | ||
|
||
override fun partOne() = racetrack.findCheats(2) | ||
|
||
companion object { | ||
private const val MINIMUM_SAVINGS = 100 | ||
|
||
private fun String.racketrack(): List<Point2d> { | ||
val grid = Grid2d(this) | ||
val path = mutableListOf(grid['S']) | ||
|
||
while (path.last() != grid['E']) { | ||
val next = | ||
grid.neighborsOf(path.last()) | ||
.filterNot { neighbor -> neighbor == path.getOrNull(path.lastIndex - 1) } | ||
.filterNot { neighbor -> grid[neighbor] == '#' } | ||
.first() | ||
|
||
path.add(next) | ||
} | ||
|
||
return path.toList() | ||
} | ||
|
||
private fun List<Point2d>.findCheats(time: Int) = | ||
indices.sumOf { start -> | ||
(start + MINIMUM_SAVINGS..lastIndex).count { end -> | ||
val distance = this[start] distanceTo this[end] | ||
(distance <= time) && (distance <= end - start - MINIMUM_SAVINGS) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.