Skip to content

Commit ba027c9

Browse files
committed
Give up and just use the hardcoded map
1 parent 6b75db0 commit ba027c9

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

day21/src/day21.scala

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,19 @@ def shortestProgramLength(robots: Int, goal: String): Int =
208208

209209
throw RuntimeException("No shortest program found")
210210

211-
def solve(robots: Int, goals: List[String], func: (Int, String) => Int): Int =
211+
def solve(robots: Int, goals: List[String], func: (Int, String) => Long): Long =
212212
goals.map { goal =>
213213
val shortest = func(robots, goal)
214-
shortest * goal.dropRight(1).toInt
214+
shortest * goal.dropRight(1).toLong
215215
}.sum
216216

217-
// Algorithm/approach inspired by https://www.reddit.com/r/adventofcode/comments/1hj2odw/comment/m36j01x
217+
// Algorithm/approach is effectively a Scala port of
218+
// https://www.reddit.com/r/adventofcode/comments/1hj2odw/comment/m36j01x For
219+
// some reason my Dijkstra-based shortest-path constructions didn't yield the
220+
// specific/right ordering, so I eventually just ended up using the hardcoded
221+
// map too. Most of the experiments can be found in earlier commits.
218222

219-
val SHORTEST_PATHS = PadType.Num.shortestPaths ++ PadType.Dir.shortestPaths
220-
221-
val SHORTEST_PATHS_2 = Map(
223+
val SHORTEST_PATHS = Map(
222224
(('A', '0'), "<A"),
223225
(('0', 'A'), ">A"),
224226
(('A', '1'), "^<<A"),
@@ -351,19 +353,26 @@ val SHORTEST_PATHS_2 = Map(
351353
(('A', '>'), "vA"),
352354
)
353355

354-
def shortestProgramLengthClever(robots: Int, goal: String): Int =
355-
// TODO: Memoize
356-
if robots < 0 then
357-
goal.length
356+
val memo = mutable.Map[(Int, String), Long]()
357+
358+
def shortestProgramLengthClever(robots: Int, goal: String): Long =
359+
val key = (robots, goal)
360+
if memo.contains(key) then
361+
return memo(key)
362+
363+
val result = if robots < 0 then
364+
goal.length.toLong
358365
else
359366
var current = 'A'
360-
var length = 0
367+
var length = 0L
361368
for next <- goal do
362369
length += moveCount(robots, current, next)
363370
current = next
364371
length
372+
memo(key) = result
373+
result
365374

366-
def moveCount(robots: Int, current: Char, next: Char): Int =
375+
def moveCount(robots: Int, current: Char, next: Char): Long =
367376
if current == next then
368377
1
369378
else
@@ -374,12 +383,15 @@ def moveCount(robots: Int, current: Char, next: Char): Int =
374383
// println(s"Part 1: ${solve(2, goals)}")
375384
// println(s"Part 2: ${solve(25, goals)}")
376385

377-
for (k, p) <- SHORTEST_PATHS.toList.sorted do
378-
if k._1 != k._2 && p != SHORTEST_PATHS_2(k) then
379-
println(s"(\"$p\", \"${SHORTEST_PATHS_2(k)}\")")
386+
// for (k, p) <- SHORTEST_PATHS.toList.sorted do
387+
// if k._1 != k._2 && p != SHORTEST_PATHS_2(k) then
388+
// println(s"(\"$p\", \"${SHORTEST_PATHS_2(k)}\")")
389+
390+
println(s"Part 1: ${solve(2, goals, shortestProgramLengthClever)}")
391+
println(s"Part 2: ${solve(25, goals, shortestProgramLengthClever)}")
380392

381-
for i <- (0 to 3) do
382-
println(s"${solve(i, goals, { (r, g) => shortestProgram(r, g).length })} vs ${solve(i, goals, shortestProgramLength)} vs ${solve(i, goals, shortestProgramLengthClever)}")
393+
// for i <- (0 to 3) do
394+
// println(s"${solve(i, goals, { (r, g) => shortestProgram(r, g).length })} vs ${solve(i, goals, shortestProgramLength)} vs ${solve(i, goals, shortestProgramLengthClever)}")
383395

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

0 commit comments

Comments
 (0)