Skip to content

Commit

Permalink
Optimize 2023 day 21 part 2 by searching once
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 21, 2023
1 parent eaa8065 commit 026dc57
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/scala/eu/sim642/adventofcode2023/Day21.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ object Day21 {
def countReachableExactlyInfinite(grid: Grid[Char], steps: Int = 26501365): Long
}

object NaivePart2Solution extends Part2Solution {

// copied from 2021 day 25
extension (pos: Pos) {
def %+(other: Pos): Pos = Pos(pos.x %+ other.x, pos.y %+ other.y)
}
// copied from 2021 day 25
extension (pos: Pos) {
def %+(other: Pos): Pos = Pos(pos.x %+ other.x, pos.y %+ other.y)
}

object NaivePart2Solution extends Part2Solution {
override def countReachableExactlyInfinite(grid: Grid[Char], steps: Int = 26501365): Long = {
val gridSize = Pos(grid(0).size, grid.size)
val graphSearch = gardenSearch(grid.posOf('S'), pos => grid(pos %+ gridSize) != '#', steps)
Expand All @@ -50,11 +49,22 @@ object Day21 {
}

object QuadraticPart2Solution extends Part2Solution {

override def countReachableExactlyInfinite(grid: Grid[Char], steps: Int = 26501365): Long = {
val gridSize = Pos(grid(0).size, grid.size)
assert(gridSize.x == 131 && gridSize.y == 131)

val (q, r) = steps /% 131
//assert(r == 65) // TODO: need this?

def maxDist(x: Int) = r + x * 131

val graphSearch = gardenSearch(grid.posOf('S'), pos => grid(pos %+ gridSize) != '#', maxDist(2))
val distances = SimultaneousBFS.search(graphSearch).distances

def p(x: Int): Pos = Pos(x, NaivePart2Solution.countReachableExactlyInfinite(grid, r + x * 131).toInt)
def p(x: Int): Pos = Pos(x, distances.count({ case (_, dist) =>
val steps = maxDist(x)
dist <= steps && dist % 2 == steps % 2
}))

Polynomial.fitQuadratic(p(0), p(1), p(2))(q)
}
Expand Down

0 comments on commit 026dc57

Please sign in to comment.