Skip to content

Commit 393aac4

Browse files
committed
Try searching a simple configuration of pads
1 parent c6d03ed commit 393aac4

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

day21/src/day21.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ case class Pad(ptype: PadType, pos: Vec2) {
4747
case _ => (None, Pad(ptype, pos + DIRECTIONS(action)))
4848
}
4949

50-
case class State(pads: List[Pad], output: String) {
50+
object Pad {
51+
def apply(ptype: PadType): Pad = Pad(ptype, PAD_LAYOUTS(ptype).find(_._2 == 'A').get._1)
52+
}
53+
54+
case class State(pads: List[Pad] = List(), output: String = "") {
5155
def perform(action: Char) =
5256
val (newPads, outAction) = pads.foldLeft[(List[Pad], Option[Char])]((List(), Some(action))) { case ((pads, action), pad) =>
5357
action match
@@ -60,22 +64,23 @@ case class State(pads: List[Pad], output: String) {
6064
State(newPads, newOutput)
6165
}
6266

63-
case class Node(state: State, program: String) extends Ordered[Node] {
67+
case class Node(state: State = State(), program: String = "") extends Ordered[Node] {
6468
def compare(that: Node): Int = program.length() compare that.program.length()
6569
}
6670

67-
def shortestProgram(goal: String): String =
71+
def shortestProgram(startState: State, goal: String): String =
6872
// Your run-of-the-mill Dijkstra implementation
6973

7074
val queue = mutable.PriorityQueue[Node]()
7175
val visited = mutable.HashSet[State]()
7276

73-
val start = Node(State(List(), ""), "")
77+
val start = Node(startState)
7478
queue.enqueue(start)
75-
visited.add(start.state)
79+
visited.add(startState)
7680

7781
while (!queue.isEmpty) do
7882
val node = queue.dequeue()
83+
println(node)
7984
if (node.state.output == goal) then
8085
return node.program
8186

@@ -89,4 +94,9 @@ def shortestProgram(goal: String): String =
8994

9095
@main def main(path: String) =
9196
val input = Source.fromFile(path).getLines.toList
92-
println(s"$input")
97+
98+
for line <- input do
99+
// val pads = List(Pad(PadType.Dir), Pad(PadType.Dir), Pad(PadType.Dir), Pad(PadType.Num))
100+
val pads = List(Pad(PadType.Num))
101+
val shortest = shortestProgram(State(pads), line)
102+
println(s"$shortest")

0 commit comments

Comments
 (0)