Skip to content

Commit 577d5d0

Browse files
committed
Implement 1-robot case using cost function
1 parent 378d44e commit 577d5d0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

day21/src/day21.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ case class Vec2(x: Int, y: Int) {
99
}
1010

1111
def +(that: Vec2) = Vec2(x + that.x, y + that.y)
12+
13+
def manhattanDist(that: Vec2) = (x - that.x).abs + (y - that.y).abs
1214
}
1315

1416
enum PadType:
@@ -114,7 +116,15 @@ def shortestProgramLength(robots: Int, goal: String): Int =
114116
def compare(that: Node): Int = that.total compare total // Intentionally reversed for min-heap
115117
}
116118

117-
def cost(robots: Int, pos: Vec2, dPos: Vec2, action: Char): (Int, Vec2) = (1, dPos)
119+
def cost(robots: Int, pos: Vec2, dPos: Vec2, action: Char): (Int, Vec2) =
120+
if robots <= 0 then
121+
(1, dPos)
122+
else
123+
// Only considering robots = 1 for now
124+
val targetDPos = PadType.Dir.locate(action)
125+
val steps = dPos.manhattanDist(targetDPos) + 1 // needs 'A' press
126+
// println(s"$dPos -> $targetDPos ('${PAD_LAYOUTS(PadType.Dir)(dPos)}' ${steps} -> '${PAD_LAYOUTS(PadType.Dir)(targetDPos)}')")
127+
(steps, targetDPos)
118128

119129
// Your run-of-the-mill Dijkstra implementation (this time on the numpad)
120130

0 commit comments

Comments
 (0)