Skip to content

Commit f963c8c

Browse files
committed
Try getting my priorities straight
1 parent 6849ece commit f963c8c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

day21/src/day21.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,32 @@ extension (ptype: PadType) {
3535
def locate(c: Char) = ptype.layout.find(_._2 == c).get._1
3636

3737
def shortestPath(startPos: Vec2, endPos: Vec2): String =
38-
case class Node(pos: Vec2, program: String = "") extends Ordered[Node] {
39-
def cost = program.length - "^^+|vv+|<<+|>>+".r.findAllMatchIn(program).length
38+
case class Node(pos: Vec2, index: Int, program: String = "") extends Ordered[Node] {
39+
def cost = program.length * 1_000_000 + turns * 1_000 + index
40+
41+
def turns = program.zip(program.tail).count { case (c1, c2) => c1 != c2 }
4042

4143
def compare(that: Node): Int = that.cost compare cost // Intentionally reversed for min-heap
4244
}
4345

4446
val queue = mutable.PriorityQueue[Node]()
4547
val visited = mutable.Set[Vec2]()
48+
var index = 0
4649

47-
queue.enqueue(Node(startPos))
48-
visited.add(startPos)
50+
queue.enqueue(Node(startPos, index))
4951

5052
while !queue.isEmpty do
5153
val node = queue.dequeue()
5254
if node.pos == endPos then
5355
return node.program.appended('A')
5456

55-
for (action, dir) <- DIRECTIONS do
57+
for action <- List('<', '^', 'v', '>') do
58+
val dir = DIRECTIONS(action)
5659
val neigh = node.pos + dir
5760
if layout.contains(neigh) && !visited.contains(neigh) then
5861
visited.add(neigh)
59-
queue.enqueue(Node(neigh, node.program.appended(action)))
62+
queue.enqueue(Node(neigh, index, node.program.appended(action)))
63+
index += 1
6064

6165
throw RuntimeException("No shortest program found")
6266

@@ -220,7 +224,8 @@ def moveCount(robots: Int, current: Char, next: Char): Int =
220224
// println(s"Part 1: ${solve(2, goals)}")
221225
// println(s"Part 2: ${solve(25, goals)}")
222226

223-
println(SHORTEST_PATHS)
227+
for p <- SHORTEST_PATHS.toList.sorted do
228+
println(p)
224229

225230
for i <- (0 to 3) do
226231
println(s"${solve(i, goals, { (r, g) => shortestProgram(r, g).length })} vs ${solve(i, goals, shortestProgramLength)} vs ${solve(i, goals, shortestProgramLengthClever)}")

0 commit comments

Comments
 (0)