Skip to content

Commit 88e7648

Browse files
committed
Optimize 2024 day 20 part 2 by avoiding containsPos checks
1 parent 740bc00 commit 88e7648

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day20.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ object Day20 {
4141
(cell, x) <- row.view.zipWithIndex
4242
if cell != '#'
4343
start = Pos(x, y)
44-
xOffset <- -maxCheat to maxCheat
45-
pos = start + Pos(xOffset, 0)
46-
if grid.containsPos(pos)
44+
xOffset <- -(maxCheat min x) to (maxCheat min (grid(0).size - 1 - x))
4745
startCheat = xOffset.abs
4846
maxEndCheat = maxCheat - startCheat
49-
yOffset <- (-maxEndCheat) to maxEndCheat
50-
end = pos + Pos(0, yOffset)
51-
if grid.containsPos(end) && grid(end) != '#'
47+
yOffset <- -(maxEndCheat min y) to (maxEndCheat min (grid.size - 1 - y))
48+
end = start + Pos(xOffset, yOffset)
49+
if grid(end) != '#'
5250
endCheat = yOffset.abs
5351
cheatDistance = forwardResult.distances(start) + (startCheat + endCheat) + backwardResult.distances(end)
5452
//if cheatDistance <= noCheatDistance

0 commit comments

Comments
 (0)