diff --git a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala index 637156e3..1afb34e7 100644 --- a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala +++ b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala @@ -5,6 +5,7 @@ import eu.sim642.adventofcodelib.GridImplicits.* import eu.sim642.adventofcodelib.graph.{GraphSearch, SimultaneousBFS, UnitNeighbors} import eu.sim642.adventofcodelib.pos.Pos import eu.sim642.adventofcodelib.IntegralImplicits._ +import Integral.Implicits._ object Day21 { @@ -53,12 +54,51 @@ object Day21 { SimultaneousBFS.search(graphSearch).distances.count(_._2 % 2 == steps % 2) } + def countReachableExactlyInfinite2(grid: Grid[Char], steps: Int = 26501365): Long = { + val (q, r) = steps /% 131 + //println((q, r)) + + val x1 = 0 + val y1 = countReachableExactlyInfinite(grid, r) + val x2 = 1 + val y2 = countReachableExactlyInfinite(grid, r + 131) + val x3 = 2 + val y3 = countReachableExactlyInfinite(grid, r + 2 * 131) + + def f(x: Long): Long = { + y1 * (x - x2) * (x - x3) / (x1 - x2) / (x1 - x3) + + y2 * (x - x1) * (x - x3) / (x2 - x1) / (x2 - x3) + + y3 * (x - x1) * (x - x2) / (x3 - x1) / (x3 - x2) + } + + f(q) + + //val gridSize = Pos(grid(0).size, grid.size) + //print(gridSize) + // + //val graphSearch = new GraphSearch[Pos] with UnitNeighbors[Pos] { + // override val startNode: Pos = grid.posOf('S') + // + // override def unitNeighbors(pos: Pos): IterableOnce[Pos] = { + // for { + // offset <- Pos.axisOffsets + // newPos = pos + offset + // if grid(newPos %+ gridSize) != '#' + // } yield newPos + // } + // + // override def isTargetNode(pos: Pos, dist: Int): Boolean = dist == steps + //} + // + //SimultaneousBFS.search(graphSearch).distances.count(_._2 % 2 == steps % 2) + } + def parseGrid(input: String): Grid[Char] = input.linesIterator.map(_.toVector).toVector lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day21.txt")).mkString.trim def main(args: Array[String]): Unit = { println(countReachableExactly(parseGrid(input))) - println(countReachableExactlyInfinite(parseGrid(input))) + println(countReachableExactlyInfinite2(parseGrid(input))) } } diff --git a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala index 9aba7e07..c15754e9 100644 --- a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala +++ b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala @@ -27,16 +27,36 @@ class Day21Test extends AnyFunSuite { } test("Part 2 examples") { - assert(countReachableExactlyInfinite(parseGrid(exampleInput), 6) == 16) - assert(countReachableExactlyInfinite(parseGrid(exampleInput), 10) == 50) - assert(countReachableExactlyInfinite(parseGrid(exampleInput), 50) == 1594) - assert(countReachableExactlyInfinite(parseGrid(exampleInput), 100) == 6536) - assert(countReachableExactlyInfinite(parseGrid(exampleInput), 500) == 167004) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 6) == 16) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 10) == 50) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 50) == 1594) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 100) == 6536) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 500) == 167004) //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 1000) == 668697) //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 5000) == 16733044) } test("Part 2 input answer") { - //assert(countReachableExactlyInfinite(parseGrid(input)) == 3687) + //assert(countReachableExactlyInfinite(parseGrid(input), 64) == 3687) + //assert(countReachableExactlyInfinite(parseGrid(input), 128) == 14452) + //assert(countReachableExactlyInfinite(parseGrid(input), 256) == 57375) + //assert(countReachableExactlyInfinite(parseGrid(input), 512) == 228690) + //assert(countReachableExactlyInfinite(parseGrid(input), 1024) == 912913) + //println(Day9.Part1.extrapolate(Seq(15130, 60085, 134866, 239473))) + + //assert(countReachableExactlyInfinite(parseGrid(input), 131) == 15130) + //assert(countReachableExactlyInfinite(parseGrid(input), 2 * 131) == 60085) + //assert(countReachableExactlyInfinite(parseGrid(input), 3 * 131) == 134866) + //assert(countReachableExactlyInfinite(parseGrid(input), 4 * 131) == 239473) + //assert(countReachableExactlyInfinite(parseGrid(input), 5 * 131) == 373906) + + + //assert(countReachableExactlyInfinite2(parseGrid(input), 64) == 3687) + //assert(countReachableExactlyInfinite2(parseGrid(input), 128) == 14452) + //assert(countReachableExactlyInfinite2(parseGrid(input), 256) == 57375) + //assert(countReachableExactlyInfinite2(parseGrid(input), 512) == 228690) + //assert(countReachableExactlyInfinite2(parseGrid(input), 1024) == 912913) + + assert(countReachableExactlyInfinite2(parseGrid(input)) == 610321885082978L) } }