Skip to content

Commit

Permalink
Optimize 2024 day 20 part 2 by avoiding containsPos checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 20, 2024
1 parent 740bc00 commit 88e7648
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/scala/eu/sim642/adventofcode2024/Day20.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ object Day20 {
(cell, x) <- row.view.zipWithIndex
if cell != '#'
start = Pos(x, y)
xOffset <- -maxCheat to maxCheat
pos = start + Pos(xOffset, 0)
if grid.containsPos(pos)
xOffset <- -(maxCheat min x) to (maxCheat min (grid(0).size - 1 - x))
startCheat = xOffset.abs
maxEndCheat = maxCheat - startCheat
yOffset <- (-maxEndCheat) to maxEndCheat
end = pos + Pos(0, yOffset)
if grid.containsPos(end) && grid(end) != '#'
yOffset <- -(maxEndCheat min y) to (maxEndCheat min (grid.size - 1 - y))
end = start + Pos(xOffset, yOffset)
if grid(end) != '#'
endCheat = yOffset.abs
cheatDistance = forwardResult.distances(start) + (startCheat + endCheat) + backwardResult.distances(end)
//if cheatDistance <= noCheatDistance
Expand Down

0 comments on commit 88e7648

Please sign in to comment.