@@ -57,26 +57,26 @@ object Pad {
57
57
def apply (ptype : PadType ): Pad = Pad (ptype, ptype.locate('A' ))
58
58
}
59
59
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
+ }
72
74
}
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
+ }
78
79
79
- def shortestProgram (startState : State , goal : String ): String =
80
80
case class Node (state : State = State (), program : String = " " ) extends Ordered [Node ] {
81
81
def compare (that : Node ): Int = that.program.length compare program.length // Intentionally reversed for min-heap
82
82
}
@@ -86,6 +86,7 @@ def shortestProgram(startState: State, goal: String): String =
86
86
val queue = mutable.PriorityQueue [Node ]()
87
87
val visited = mutable.HashSet [State ]()
88
88
89
+ val startState = State ()
89
90
val start = Node (startState)
90
91
queue.enqueue(start)
91
92
visited.add(startState)
@@ -106,10 +107,6 @@ def shortestProgram(startState: State, goal: String): String =
106
107
107
108
throw new RuntimeException (" No shortest program found" )
108
109
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
-
113
110
def cost (robots : Int , pos : Vec2 , action : Char ): Int = 1
114
111
115
112
def shortestProgramLength (robots : Int , goal : String ): Int =
@@ -157,7 +154,7 @@ def solve(robots: Int, goals: List[String], func: (Int, String) => Int): Int =
157
154
// println(s"Part 2: ${solve(25, goals)}")
158
155
159
156
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)}" )
161
158
162
159
// for c <- ('0' to '5') do
163
160
// println(s"$c -> ${(0 to 3).map { i => shortestProgram(makeState(i), s"$c").length }}")
0 commit comments