@@ -6,10 +6,10 @@ import eu.sim642.adventofcodelib.pos.Pos
6
6
7
7
object Day18 {
8
8
9
- def exitSteps (bytes : Seq [Pos ], max : Pos = Pos (70 , 70 ), after : Int = 1024 ): Int = {
9
+ def bytesGraphSearch (bytes : Seq [Pos ], max : Pos = Pos (70 , 70 ), after : Int = 1024 ): GraphSearch [ Pos ] & UnitNeighbors [ Pos ] & TargetNode [ Pos ] = {
10
10
val fallenBytes = bytes.take(after).toSet
11
11
12
- val graphSearch = new GraphSearch [Pos ] with UnitNeighbors [Pos ] with TargetNode [Pos ] {
12
+ new GraphSearch [Pos ] with UnitNeighbors [Pos ] with TargetNode [Pos ] {
13
13
override val startNode : Pos = Pos .zero
14
14
15
15
override def unitNeighbors (pos : Pos ): IterableOnce [Pos ] = {
@@ -23,37 +23,23 @@ object Day18 {
23
23
24
24
override val targetNode : Pos = max
25
25
}
26
+ }
26
27
28
+ def exitSteps (bytes : Seq [Pos ], max : Pos = Pos (70 , 70 ), after : Int = 1024 ): Int = {
29
+ val graphSearch = bytesGraphSearch(bytes, max, after)
27
30
BFS .search(graphSearch).target.get._2
28
31
}
29
32
30
- // TODO: deduplicate
31
- def exitSteps2 (bytes : Seq [Pos ], max : Pos = Pos (70 , 70 ), after : Int = 1024 ): Boolean = {
32
- val fallenBytes = bytes.take(after).toSet
33
-
34
- val graphSearch = new GraphSearch [Pos ] with UnitNeighbors [Pos ] with TargetNode [Pos ] {
35
- override val startNode : Pos = Pos .zero
36
-
37
- override def unitNeighbors (pos : Pos ): IterableOnce [Pos ] = {
38
- for {
39
- offset <- Pos .axisOffsets
40
- newPos = pos + offset
41
- if Pos .zero <= newPos && newPos <= max
42
- if ! fallenBytes(newPos)
43
- } yield newPos
44
- }
45
-
46
- override val targetNode : Pos = max
47
- }
48
-
33
+ def exitReachable (bytes : Seq [Pos ], max : Pos , after : Int ): Boolean = {
34
+ val graphSearch = bytesGraphSearch(bytes, max, after)
49
35
BFS .search(graphSearch).target.isDefined
50
36
}
51
37
52
38
def findBlockingByte (bytes : Seq [Pos ], max : Pos = Pos (70 , 70 )): String = {
53
- def f (after : Int ): Boolean = ! exitSteps2 (bytes, max, after)
54
- val after = OrderedSearch .binaryLower(f, 0 , bytes.size + 1 )(true )
55
- val afterPos = bytes(after - 1 )
56
- s " ${afterPos .x}, ${afterPos .y}"
39
+ def f (after : Int ): Boolean = ! exitReachable (bytes, max, after)
40
+ val blockingAfter = OrderedSearch .binaryLower(f, 0 , bytes.size + 1 )(true )
41
+ val blockingByte = bytes(blockingAfter - 1 )
42
+ s " ${blockingByte .x}, ${blockingByte .y}"
57
43
}
58
44
59
45
def parseByte (s : String ): Pos = s match {
0 commit comments