@@ -72,11 +72,11 @@ case class State(pads: List[Pad] = List(), output: String = "") {
72
72
State (newPads, newOutput)
73
73
}
74
74
75
- case class Node (state : State = State (), programLength : Int = 0 ) extends Ordered [Node ] {
76
- def compare (that : Node ): Int = that.programLength compare programLength // Intentionally reversed for min-heap
75
+ case class Node (state : State = State (), program : String = " " ) extends Ordered [Node ] {
76
+ def compare (that : Node ): Int = that.program.length compare program.length // Intentionally reversed for min-heap
77
77
}
78
78
79
- def shortestProgramLength (startState : State , goal : String ): Int =
79
+ def shortestProgram (startState : State , goal : String ): String =
80
80
// Your run-of-the-mill Dijkstra implementation
81
81
82
82
val queue = mutable.PriorityQueue [Node ]()
@@ -89,7 +89,7 @@ def shortestProgramLength(startState: State, goal: String): Int =
89
89
while ! queue.isEmpty do
90
90
val node = queue.dequeue()
91
91
if node.state.output == goal then
92
- return node.programLength
92
+ return node.program
93
93
94
94
if node.state.output.length < goal.length then
95
95
for
@@ -98,7 +98,7 @@ def shortestProgramLength(startState: State, goal: String): Int =
98
98
do
99
99
if ! visited.contains(newState) then
100
100
visited.add(newState)
101
- queue.enqueue(Node (newState, node.programLength + 1 ))
101
+ queue.enqueue(Node (newState, node.program.appended(action) ))
102
102
103
103
throw new RuntimeException (" No shortest program found" )
104
104
@@ -109,7 +109,7 @@ def makeState(robots: Int) = State(makePads(robots))
109
109
def solve (robots : Int , goals : List [String ]): Int =
110
110
val state = makeState(robots)
111
111
goals.map { goal =>
112
- val shortest = shortestProgramLength (state, goal)
112
+ val shortest = shortestProgram (state, goal).length
113
113
shortest * goal.dropRight(1 ).toInt
114
114
}.sum
115
115
@@ -118,5 +118,5 @@ def solve(robots: Int, goals: List[String]): Int =
118
118
// println(s"Part 1: ${solve(2, goals)}")
119
119
// println(s"Part 2: ${solve(25, goals)}")
120
120
121
- for c <- ('0' to '9 ' ) do
122
- println(s " $c -> ${(0 to 8 ).map { i => shortestProgramLength (makeState(i), s " $c" ) }}" )
121
+ for c <- ('0' to '5 ' ) do
122
+ println(s " $c -> ${(0 to 3 ).map { i => shortestProgram (makeState(i), s " $c" ).length }}" )
0 commit comments