Skip to content

Commit 378d44e

Browse files
committed
Add dPos to state
1 parent 4a0cf5c commit 378d44e

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

day21/src/day21.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ def shortestProgram(robots: Int, goal: String): String =
108108
throw new RuntimeException("No shortest program found")
109109

110110
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 = "")
112112

113113
case class Node(state: State, total: Int = 0) extends Ordered[Node] {
114114
def compare(that: Node): Int = that.total compare total // Intentionally reversed for min-heap
115115
}
116116

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)
118118

119119
// Your run-of-the-mill Dijkstra implementation (this time on the numpad)
120120

@@ -137,11 +137,12 @@ def shortestProgramLength(robots: Int, goal: String): Int =
137137
do
138138
val newPos = node.state.pos + DIRECTIONS.get(action).getOrElse(Vec2(0, 0))
139139
if PAD_LAYOUTS(PadType.Num).contains(newPos) then
140+
val (c, newDPos) = cost(robots, node.state.pos, node.state.dPos, action)
140141
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)
142143
if !visited.contains(newState) then
143144
visited.add(newState)
144-
queue.enqueue(Node(newState, node.total + cost(node.state, newState, action)))
145+
queue.enqueue(Node(newState, node.total + c))
145146

146147
throw new RuntimeException("No shortest program found")
147148

0 commit comments

Comments
 (0)