Skip to content

Commit 89aff9d

Browse files
committed
Only track program length on day 21
1 parent a7dbcfd commit 89aff9d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

day21/src/day21.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ case class State(pads: List[Pad] = List(), output: String = "") {
7272
State(newPads, newOutput)
7373
}
7474

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
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
7777
}
7878

79-
def shortestProgram(startState: State, goal: String): String =
79+
def shortestProgramLength(startState: State, goal: String): Int =
8080
// Your run-of-the-mill Dijkstra implementation
8181

8282
val queue = mutable.PriorityQueue[Node]()
@@ -89,7 +89,7 @@ def shortestProgram(startState: State, goal: String): String =
8989
while !queue.isEmpty do
9090
val node = queue.dequeue()
9191
if node.state.output == goal then
92-
return node.program
92+
return node.programLength
9393

9494
if node.state.output.length < goal.length then
9595
for
@@ -98,15 +98,15 @@ def shortestProgram(startState: State, goal: String): String =
9898
do
9999
if !visited.contains(newState) then
100100
visited.add(newState)
101-
queue.enqueue(Node(newState, node.program.appended(action)))
101+
queue.enqueue(Node(newState, node.programLength + 1))
102102

103103
throw new RuntimeException("No shortest program found")
104104

105105
def solve(robots: Int, goals: List[String]): Int =
106106
val pads = List.fill(robots)(Pad(PadType.Dir)) :+ Pad(PadType.Num)
107107
goals.map { goal =>
108-
val shortest = shortestProgram(State(pads), goal)
109-
shortest.length * goal.dropRight(1).toInt
108+
val shortest = shortestProgramLength(State(pads), goal)
109+
shortest * goal.dropRight(1).toInt
110110
}.sum
111111

112112
@main def main(path: String) =

0 commit comments

Comments
 (0)