@@ -47,7 +47,11 @@ case class Pad(ptype: PadType, pos: Vec2) {
47
47
case _ => (None , Pad (ptype, pos + DIRECTIONS (action)))
48
48
}
49
49
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 = " " ) {
51
55
def perform (action : Char ) =
52
56
val (newPads, outAction) = pads.foldLeft[(List [Pad ], Option [Char ])]((List (), Some (action))) { case ((pads, action), pad) =>
53
57
action match
@@ -60,22 +64,23 @@ case class State(pads: List[Pad], output: String) {
60
64
State (newPads, newOutput)
61
65
}
62
66
63
- case class Node (state : State , program : String ) extends Ordered [Node ] {
67
+ case class Node (state : State = State () , program : String = " " ) extends Ordered [Node ] {
64
68
def compare (that : Node ): Int = program.length() compare that.program.length()
65
69
}
66
70
67
- def shortestProgram (goal : String ): String =
71
+ def shortestProgram (startState : State , goal : String ): String =
68
72
// Your run-of-the-mill Dijkstra implementation
69
73
70
74
val queue = mutable.PriorityQueue [Node ]()
71
75
val visited = mutable.HashSet [State ]()
72
76
73
- val start = Node (State ( List (), " " ), " " )
77
+ val start = Node (startState )
74
78
queue.enqueue(start)
75
- visited.add(start.state )
79
+ visited.add(startState )
76
80
77
81
while (! queue.isEmpty) do
78
82
val node = queue.dequeue()
83
+ println(node)
79
84
if (node.state.output == goal) then
80
85
return node.program
81
86
@@ -89,4 +94,9 @@ def shortestProgram(goal: String): String =
89
94
90
95
@ main def main (path : String ) =
91
96
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