File tree Expand file tree Collapse file tree 2 files changed +13
-10
lines changed
src/main/kotlin/adventofcode/day18 Expand file tree Collapse file tree 2 files changed +13
-10
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,6 @@ Here are the solutions I have implemented along with the time it took to run eac
37
37
| [ Day 15: Warehouse Woes] ( src/test/kotlin/adventofcode/Day15Test.kt ) | 129 |
38
38
| [ Day 16: Reindeer Maze] ( src/test/kotlin/adventofcode/Day16Test.kt ) | 261 |
39
39
| [ Day 17: Chronospatial Computer] ( src/test/kotlin/adventofcode/Day17Test.kt ) | 93 |
40
- | [ Day 18: RAM Run] ( src/test/kotlin/adventofcode/Day18Test.kt ) | 5,520 |
40
+ | [ Day 18: RAM Run] ( src/test/kotlin/adventofcode/Day18Test.kt ) | 3,328 |
41
41
| [ Day 19: Linen Layout] ( src/test/kotlin/adventofcode/Day19Test.kt ) | 60 |
42
42
| [ Day 20: Race Condition] ( src/test/kotlin/adventofcode/Day20Test.kt ) | 0 |
Original file line number Diff line number Diff line change @@ -28,15 +28,18 @@ fun List<Point2D>.generateMap() = TextGrid(List(maxOf { it.y } + 1) { ".".repeat
28
28
29
29
fun String.findFirstByteThatBlocksExit (): Point2D {
30
30
val points = parsePoints()
31
- val map = points.generateMap()
32
- val start = map.coordinates().first()
33
- val end = map.coordinates().last()
34
- val vertices = map.coordinates().filter { map[it] != ' #' }.toSet()
35
- val neighbors = { p: Point2D -> p.neighbors().filter { it in map }.filter { map[it] != ' #' } }
36
- return points.find { p ->
37
- map[p] = ' #'
38
- ! Graphs .shortestPaths(start, vertices, neighbors).pathExists(end)
39
- } ? : throw IllegalStateException (" No byte blocks exit" )
31
+
32
+ val start = Point2D .origin()
33
+ val xRange = 0 .. points.maxOf { it.x }
34
+ val yRange = 0 .. points.maxOf { it.y }
35
+ val end = Point2D (xRange.last, yRange.last)
36
+ val fallen = mutableSetOf<Point2D >()
37
+ val neighbors =
38
+ { p: Point2D -> p.neighbors().filter { it.x in xRange && it.y in yRange }.filter { it !in fallen} }
39
+ return points.first { p ->
40
+ fallen.add(p)
41
+ Graphs .dfs(start, end, neighbors).isEmpty()
42
+ }
40
43
}
41
44
42
45
fun String.findShortestPathToExitAfter (nBytes : Int ): Int {
You can’t perform that action at this time.
0 commit comments