Skip to content

Commit 32a8290

Browse files
committed
Fixing graphs algorithm and updating readme with timings from Maven build run (on my mac)
1 parent 6868331 commit 32a8290

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ The project is structured as follows:
2020
Here are the solutions I have implemented along with the time it took to run each one:
2121
| Solution | Timing (ms) |
2222
| :--- | ---: |
23-
| [Day 1: Historian Hysteria](src/test/kotlin/adventofcode/Day01Test.kt) | 49 |
24-
| [Day 2: Red-Nosed Reports](src/test/kotlin/adventofcode/Day02Test.kt) | 45 |
25-
| [Day 3: Mull It Over](src/test/kotlin/adventofcode/Day03Test.kt) | 39 |
26-
| [Day 4: Ceres Search](src/test/kotlin/adventofcode/Day04Test.kt) | 120 |
27-
| [Day 5: Print Queue](src/test/kotlin/adventofcode/Day05Test.kt) | 50 |
28-
| [Day 6: Guard Gallivant](src/test/kotlin/adventofcode/Day06Test.kt) | 778 |
29-
| [Day 7: Bridge Repair](src/test/kotlin/adventofcode/Day07Test.kt) | 69 |
30-
| [Day 8: Resonant Collinearity](src/test/kotlin/adventofcode/Day08Test.kt) | 77 |
31-
| [Day 9: Disk Fragmenter](src/test/kotlin/adventofcode/Day09Test.kt) | 1,012 |
32-
| [Day 10: Hoof It](src/test/kotlin/adventofcode/Day10Test.kt) | 583 |
33-
| [Day 11: Plutonian Pebbles](src/test/kotlin/adventofcode/Day11Test.kt) | 119 |
34-
| [Day 12: Garden Groups](src/test/kotlin/adventofcode/Day12Test.kt) | 221 |
35-
| [Day 13: Claw Contraption](src/test/kotlin/adventofcode/Day13Test.kt) | 54 |
36-
| [Day 14: Restroom Redoubt](src/test/kotlin/adventofcode/Day14Test.kt) | 474 |
37-
| [Day 15: Warehouse Woes](src/test/kotlin/adventofcode/Day15Test.kt) | 129 |
38-
| [Day 16: Reindeer Maze](src/test/kotlin/adventofcode/Day16Test.kt) | 261 |
39-
| [Day 17: Chronospatial Computer](src/test/kotlin/adventofcode/Day17Test.kt) | 93 |
40-
| [Day 18: RAM Run](src/test/kotlin/adventofcode/Day18Test.kt) | 476 |
41-
| [Day 19: Linen Layout](src/test/kotlin/adventofcode/Day19Test.kt) | 60 |
42-
| [Day 20: Race Condition](src/test/kotlin/adventofcode/Day20Test.kt) | 474 |
23+
| [Day 1: Historian Hysteria](src/test/kotlin/adventofcode/Day01Test.kt) | 7 |
24+
| [Day 2: Red-Nosed Reports](src/test/kotlin/adventofcode/Day02Test.kt) | 8 |
25+
| [Day 3: Mull It Over](src/test/kotlin/adventofcode/Day03Test.kt) | 7 |
26+
| [Day 4: Ceres Search](src/test/kotlin/adventofcode/Day04Test.kt) | 67 |
27+
| [Day 5: Print Queue](src/test/kotlin/adventofcode/Day05Test.kt) | 8 |
28+
| [Day 6: Guard Gallivant](src/test/kotlin/adventofcode/Day06Test.kt) | 661 |
29+
| [Day 7: Bridge Repair](src/test/kotlin/adventofcode/Day07Test.kt) | 24 |
30+
| [Day 8: Resonant Collinearity](src/test/kotlin/adventofcode/Day08Test.kt) | 5 |
31+
| [Day 9: Disk Fragmenter](src/test/kotlin/adventofcode/Day09Test.kt) | 1,007 |
32+
| [Day 10: Hoof It](src/test/kotlin/adventofcode/Day10Test.kt) | 474 |
33+
| [Day 11: Plutonian Pebbles](src/test/kotlin/adventofcode/Day11Test.kt) | 96 |
34+
| [Day 12: Garden Groups](src/test/kotlin/adventofcode/Day12Test.kt) | 119 |
35+
| [Day 13: Claw Contraption](src/test/kotlin/adventofcode/Day13Test.kt) | 11 |
36+
| [Day 14: Restroom Redoubt](src/test/kotlin/adventofcode/Day14Test.kt) | 452 |
37+
| [Day 15: Warehouse Woes](src/test/kotlin/adventofcode/Day15Test.kt) | 52 |
38+
| [Day 16: Reindeer Maze](src/test/kotlin/adventofcode/Day16Test.kt) | 134 |
39+
| [Day 17: Chronospatial Computer](src/test/kotlin/adventofcode/Day17Test.kt) | 55 |
40+
| [Day 18: RAM Run](src/test/kotlin/adventofcode/Day18Test.kt) | 248 |
41+
| [Day 19: Linen Layout](src/test/kotlin/adventofcode/Day19Test.kt) | 28 |
42+
| [Day 20: Race Condition](src/test/kotlin/adventofcode/Day20Test.kt) | 515 |

src/main/kotlin/adventofcode/util/graph/Graphs.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,17 @@ object Graphs {
5959
return ShortestPaths(start, dist, pred)
6060
}
6161

62-
private data class Reachable<V>(val steps: Int, val vertex: V)
63-
6462
fun <V> reachable(start: V, maxSteps: Int = Int.MAX_VALUE, neighbors: (V) -> List<V>): Set<V> {
6563
val visited = mutableSetOf<V>()
66-
val stack = mutableListOf(Pair(start, 0))
67-
while (stack.isNotEmpty()) {
68-
val (current, steps) = stack.removeFirst()
69-
if (steps > maxSteps) continue
70-
if (current !in visited) {
71-
visited.add(current)
72-
neighbors(current)
73-
.filter { it !in visited }
74-
.forEach { stack.add(Pair(it, steps + 1)) }
64+
val queue = mutableListOf(Pair(start, 0))
65+
while (queue.isNotEmpty()) {
66+
val (current, steps) = queue.removeFirst()
67+
if (visited.add(current) && steps < maxSteps) {
68+
queue.addAll(
69+
neighbors(current)
70+
.filter { n -> n !in visited }
71+
.map { n -> Pair(n, steps + 1) }
72+
)
7573
}
7674
}
7775
return visited

0 commit comments

Comments
 (0)