Skip to content

Commit

Permalink
[2024/18] RAM Run (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 21, 2024
1 parent f85b242 commit a9873fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions 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 ||||||||||||||| ||| ||| | | | | | 36 |
| 2024 ||||||||||||||| ||| ||| | | | | | 37 |

## 🛷 How to run

Expand Down Expand Up @@ -180,7 +180,7 @@ e.g. `HandyHaversacks`)*
| | 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [[Code](src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt)] | `224357412` | `7083` |
| | 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` |
| | 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [[Code](src/main/kotlin/adventofcode/year2024/Day18RamRun.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day18RamRunSpec.kt)] | `302` | |
| | 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [[Code](src/main/kotlin/adventofcode/year2024/Day18RamRun.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day18RamRunSpec.kt)] | `302` | `24,32` |
| | 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` | `1000697` |

Expand Down
33 changes: 23 additions & 10 deletions src/main/kotlin/adventofcode/year2024/Day18RamRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,57 @@ import adventofcode.common.spatial.Point2d
import adventofcode.common.spatial.Point2d.Companion.Origin

class Day18RamRun(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override val name = "RAM Run"

private val corruptedBytes by lazy {
input.lines().map { line ->
val (x, y) = line.split(",").map(String::toLong)
Point2d(x, y)
}
}

override val name = "RAM Run"
override fun partOne() = shortestPath(Origin, corruptedBytes.take(KILOBYTE_IN_BYTES))!!

override fun partOne() = shortestPath(Origin, Point2d(GRID_SIZE, GRID_SIZE), corruptedBytes.take(1024).toSet())
override fun partTwo(): String {
val (x, y) =
generateSequence(KILOBYTE_IN_BYTES + 1, Int::inc)
.first { bytes -> shortestPath(Origin, corruptedBytes.take(bytes)) == null }
.let { byte -> corruptedBytes[byte - 1] }

return "$x,$y"
}

companion object {
private const val GRID_SIZE = 70
private const val KILOBYTE_IN_BYTES = 1024
private val end = Point2d(GRID_SIZE, GRID_SIZE)

private fun shortestPath(
start: Point2d,
end: Point2d,
corruptedBytes: Set<Point2d>,
): Int {
val visited = mutableSetOf<Pair<Point2d, Int>>()
corruptedBytes: List<Point2d>,
): Int? {
val visited = mutableSetOf<Point2d>()
val queue = ArrayDeque(setOf(start to 0))

while (queue.isNotEmpty()) {
val (position, cost) = queue.removeFirst()

if (position == end) {
return cost
}

val neighbors =
position
.neighbors()
.filter { (x, y) -> x in 0..end.x && y in 0..end.y }
.filterNot { neighbor -> neighbor in visited.map { it.first } }
.filterNot { neighbor -> neighbor in visited }
.filterNot { neighbor -> neighbor in corruptedBytes }
.map { neighbor -> neighbor to cost + 1 }

visited.addAll(neighbors)
queue.addAll(neighbors)
queue.addAll(neighbors.map { neighbor -> neighbor to cost + 1 })
}

return visited.first { (position) -> position == end }.second
return null
}
}
}

0 comments on commit a9873fe

Please sign in to comment.