Skip to content

Commit d6e43d1

Browse files
committed
Track strings again
1 parent a505168 commit d6e43d1

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

day21/src/day21.scala

Lines changed: 8 additions & 8 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(), 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
7777
}
7878

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

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

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

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

@@ -109,7 +109,7 @@ def makeState(robots: Int) = State(makePads(robots))
109109
def solve(robots: Int, goals: List[String]): Int =
110110
val state = makeState(robots)
111111
goals.map { goal =>
112-
val shortest = shortestProgramLength(state, goal)
112+
val shortest = shortestProgram(state, goal).length
113113
shortest * goal.dropRight(1).toInt
114114
}.sum
115115

@@ -118,5 +118,5 @@ def solve(robots: Int, goals: List[String]): Int =
118118
// println(s"Part 1: ${solve(2, goals)}")
119119
// println(s"Part 2: ${solve(25, goals)}")
120120

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

Comments
 (0)