@@ -35,28 +35,32 @@ extension (ptype: PadType) {
35
35
def locate (c : Char ) = ptype.layout.find(_._2 == c).get._1
36
36
37
37
def shortestPath (startPos : Vec2 , endPos : Vec2 ): String =
38
- case class Node (pos : Vec2 , program : String = " " ) extends Ordered [Node ] {
39
- def cost = program.length - " ^^+|vv+|<<+|>>+" .r.findAllMatchIn(program).length
38
+ case class Node (pos : Vec2 , index : Int , program : String = " " ) extends Ordered [Node ] {
39
+ def cost = program.length * 1_000_000 + turns * 1_000 + index
40
+
41
+ def turns = program.zip(program.tail).count { case (c1, c2) => c1 != c2 }
40
42
41
43
def compare (that : Node ): Int = that.cost compare cost // Intentionally reversed for min-heap
42
44
}
43
45
44
46
val queue = mutable.PriorityQueue [Node ]()
45
47
val visited = mutable.Set [Vec2 ]()
48
+ var index = 0
46
49
47
- queue.enqueue(Node (startPos))
48
- visited.add(startPos)
50
+ queue.enqueue(Node (startPos, index))
49
51
50
52
while ! queue.isEmpty do
51
53
val node = queue.dequeue()
52
54
if node.pos == endPos then
53
55
return node.program.appended('A' )
54
56
55
- for (action, dir) <- DIRECTIONS do
57
+ for action <- List ('<' , '^' , 'v' , '>' ) do
58
+ val dir = DIRECTIONS (action)
56
59
val neigh = node.pos + dir
57
60
if layout.contains(neigh) && ! visited.contains(neigh) then
58
61
visited.add(neigh)
59
- queue.enqueue(Node (neigh, node.program.appended(action)))
62
+ queue.enqueue(Node (neigh, index, node.program.appended(action)))
63
+ index += 1
60
64
61
65
throw RuntimeException (" No shortest program found" )
62
66
@@ -220,7 +224,8 @@ def moveCount(robots: Int, current: Char, next: Char): Int =
220
224
// println(s"Part 1: ${solve(2, goals)}")
221
225
// println(s"Part 2: ${solve(25, goals)}")
222
226
223
- println(SHORTEST_PATHS )
227
+ for p <- SHORTEST_PATHS .toList.sorted do
228
+ println(p)
224
229
225
230
for i <- (0 to 3 ) do
226
231
println(s " ${solve(i, goals, { (r, g) => shortestProgram(r, g).length })} vs ${solve(i, goals, shortestProgramLength)} vs ${solve(i, goals, shortestProgramLengthClever)}" )
0 commit comments