Skip to content

Commit

Permalink
[2024/20] Race Condition (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 20, 2024
1 parent 29f0325 commit 37047c2
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 ||||||||||||||| ||| || | | | | | | 33 |
| 2024 ||||||||||||||| ||| || | | | | | | 34 |

## 🛷 How to run

Expand Down Expand Up @@ -181,6 +181,7 @@ e.g. `HandyHaversacks`)*
| | 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [[Code](src/main/kotlin/adventofcode/year2024/Day16ReindeerMaze.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day16ReindeerMazeSpec.kt)] | `98484` | |
| | 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [[Code](src/main/kotlin/adventofcode/year2024/Day17ChronospatialComputer.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day17ChronospatialComputerSpec.kt)] | `5,1,3,4,3,7,2,1,7` | `216584205979245` |
| | 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [[Code](src/main/kotlin/adventofcode/year2024/Day19LinenLayout.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day19LinenLayoutSpec.kt)] | `267` | `796449099271652` |
| | 20 | [Race Condition](https://adventofcode.com/2024/day/20) | [[Code](src/main/kotlin/adventofcode/year2024/Day20RaceCondition.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day20RaceConditionSpec.kt)] | `1426` | |

## 🕯️ Useful commands

Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/adventofcode/common/spatial/Point2d.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package adventofcode.common.spatial

import kotlin.math.absoluteValue

data class Point2d(
val x: Long,
val y: Long,
Expand Down Expand Up @@ -44,6 +46,8 @@ data class Point2d(
}
.map { direction -> this + direction }
.toSet()

infix fun distanceTo(other: Point2d): Long = (x - other.x).absoluteValue + (y - other.y).absoluteValue
}

val NORTH = Point2d(0, -1)
Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day20RaceCondition.kt
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)
}
}
}
}
Loading

0 comments on commit 37047c2

Please sign in to comment.