Skip to content

Commit 26ff864

Browse files
committed
Move State into shortestProgram
1 parent 7c7712a commit 26ff864

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

day21/src/day21.scala

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,26 @@ object Pad {
5757
def apply(ptype: PadType): Pad = Pad(ptype, ptype.locate('A'))
5858
}
5959

60-
case class State(pads: List[Pad] = List(), output: String = "") {
61-
def perform(action: Char) =
62-
for
63-
(newPads, outAction) <- pads.foldLeft[Option[(List[Pad], Option[Char])]](Some((List(), Some(action)))) { (acc, pad) =>
64-
acc.flatMap { case (pads, action) =>
65-
action match
66-
case Some(action) =>
67-
for
68-
(newAction, newPad) <- Some(pad.perform(action))
69-
if newPad.isValid
70-
yield (pads :+ newPad, newAction)
71-
case None => Some((pads :+ pad, None))
60+
def shortestProgram(robots: Int, goal: String): String =
61+
case class State(pads: List[Pad] = List.fill(robots)(Pad(PadType.Dir)) :+ Pad(PadType.Num), output: String = "") {
62+
def perform(action: Char) =
63+
for
64+
(newPads, outAction) <- pads.foldLeft[Option[(List[Pad], Option[Char])]](Some((List(), Some(action)))) { (acc, pad) =>
65+
acc.flatMap { case (pads, action) =>
66+
action match
67+
case Some(action) =>
68+
for
69+
(newAction, newPad) <- Some(pad.perform(action))
70+
if newPad.isValid
71+
yield (pads :+ newPad, newAction)
72+
case None => Some((pads :+ pad, None))
73+
}
7274
}
73-
}
74-
yield
75-
val newOutput = outAction.map(output.appended(_)).getOrElse(output)
76-
State(newPads, newOutput)
77-
}
75+
yield
76+
val newOutput = outAction.map(output.appended(_)).getOrElse(output)
77+
State(newPads, newOutput)
78+
}
7879

79-
def shortestProgram(startState: State, goal: String): String =
8080
case class Node(state: State = State(), program: String = "") extends Ordered[Node] {
8181
def compare(that: Node): Int = that.program.length compare program.length // Intentionally reversed for min-heap
8282
}
@@ -86,6 +86,7 @@ def shortestProgram(startState: State, goal: String): String =
8686
val queue = mutable.PriorityQueue[Node]()
8787
val visited = mutable.HashSet[State]()
8888

89+
val startState = State()
8990
val start = Node(startState)
9091
queue.enqueue(start)
9192
visited.add(startState)
@@ -106,10 +107,6 @@ def shortestProgram(startState: State, goal: String): String =
106107

107108
throw new RuntimeException("No shortest program found")
108109

109-
def makePads(robots: Int): List[Pad] = List.fill(robots)(Pad(PadType.Dir)) :+ Pad(PadType.Num)
110-
111-
def makeState(robots: Int) = State(makePads(robots))
112-
113110
def cost(robots: Int, pos: Vec2, action: Char): Int = 1
114111

115112
def shortestProgramLength(robots: Int, goal: String): Int =
@@ -157,7 +154,7 @@ def solve(robots: Int, goals: List[String], func: (Int, String) => Int): Int =
157154
// println(s"Part 2: ${solve(25, goals)}")
158155

159156
for i <- (0 to 3) do
160-
println(s"${solve(i, goals, { (r, g) => shortestProgram(makeState(r), g).length })} vs ${solve(i, goals, shortestProgramLength)}")
157+
println(s"${solve(i, goals, { (r, g) => shortestProgram(r, g).length })} vs ${solve(i, goals, shortestProgramLength)}")
161158

162159
// for c <- ('0' to '5') do
163160
// println(s"$c -> ${(0 to 3).map { i => shortestProgram(makeState(i), s"$c").length }}")

0 commit comments

Comments
 (0)