@@ -108,13 +108,13 @@ def shortestProgram(robots: Int, goal: String): String =
108
108
throw new RuntimeException (" No shortest program found" )
109
109
110
110
def shortestProgramLength (robots : Int , goal : String ): Int =
111
- case class State (pos : Vec2 = PadType .Num .locate('A' ), output : String = " " )
111
+ case class State (pos : Vec2 = PadType .Num .locate('A' ), dPos : Vec2 = PadType . Dir .locate( 'A' ), output : String = " " )
112
112
113
113
case class Node (state : State , total : Int = 0 ) extends Ordered [Node ] {
114
114
def compare (that : Node ): Int = that.total compare total // Intentionally reversed for min-heap
115
115
}
116
116
117
- def cost (state : State , newState : State , action : Char ): Int = 1
117
+ def cost (robots : Int , pos : Vec2 , dPos : Vec2 , action : Char ): ( Int , Vec2 ) = ( 1 , dPos)
118
118
119
119
// Your run-of-the-mill Dijkstra implementation (this time on the numpad)
120
120
@@ -137,11 +137,12 @@ def shortestProgramLength(robots: Int, goal: String): Int =
137
137
do
138
138
val newPos = node.state.pos + DIRECTIONS .get(action).getOrElse(Vec2 (0 , 0 ))
139
139
if PAD_LAYOUTS (PadType .Num ).contains(newPos) then
140
+ val (c, newDPos) = cost(robots, node.state.pos, node.state.dPos, action)
140
141
val newOutput = if action == 'A' then node.state.output.appended(PAD_LAYOUTS (PadType .Num )(node.state.pos)) else node.state.output
141
- val newState = State (newPos, newOutput)
142
+ val newState = State (newPos, newDPos, newOutput)
142
143
if ! visited.contains(newState) then
143
144
visited.add(newState)
144
- queue.enqueue(Node (newState, node.total + cost(node.state, newState, action) ))
145
+ queue.enqueue(Node (newState, node.total + c ))
145
146
146
147
throw new RuntimeException (" No shortest program found" )
147
148
0 commit comments