Skip to content

Commit a505168

Browse files
committed
Abstract out some helpers and output some lengths
1 parent 89aff9d commit a505168

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

day21/src/day21.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,21 @@ def shortestProgramLength(startState: State, goal: String): Int =
102102

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

105+
def makePads(robots: Int): List[Pad] = List.fill(robots)(Pad(PadType.Dir)) :+ Pad(PadType.Num)
106+
107+
def makeState(robots: Int) = State(makePads(robots))
108+
105109
def solve(robots: Int, goals: List[String]): Int =
106-
val pads = List.fill(robots)(Pad(PadType.Dir)) :+ Pad(PadType.Num)
110+
val state = makeState(robots)
107111
goals.map { goal =>
108-
val shortest = shortestProgramLength(State(pads), goal)
112+
val shortest = shortestProgramLength(state, goal)
109113
shortest * goal.dropRight(1).toInt
110114
}.sum
111115

112116
@main def main(path: String) =
113117
val goals = Source.fromFile(path).getLines.toList
114-
println(s"Part 1: ${solve(2, goals)}")
115-
println(s"Part 2: ${solve(25, goals)}")
118+
// println(s"Part 1: ${solve(2, goals)}")
119+
// println(s"Part 2: ${solve(25, goals)}")
120+
121+
for c <- ('0' to '9') do
122+
println(s"$c -> ${(0 to 8).map { i => shortestProgramLength(makeState(i), s"$c") }}")

0 commit comments

Comments
 (0)