From 43c0e21657c63445f247ca9f3f71d039d5a8c546 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Thu, 21 Dec 2023 08:09:39 +0200 Subject: [PATCH] Add naive solution to 2023 day 21 part 2 --- .../eu/sim642/adventofcode2023/Day21.scala | 27 +++++++++++++++++++ .../sim642/adventofcode2023/Day21Test.scala | 14 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala index 450c70ce..637156e3 100644 --- a/src/main/scala/eu/sim642/adventofcode2023/Day21.scala +++ b/src/main/scala/eu/sim642/adventofcode2023/Day21.scala @@ -4,6 +4,7 @@ import eu.sim642.adventofcodelib.Grid import eu.sim642.adventofcodelib.GridImplicits.* import eu.sim642.adventofcodelib.graph.{GraphSearch, SimultaneousBFS, UnitNeighbors} import eu.sim642.adventofcodelib.pos.Pos +import eu.sim642.adventofcodelib.IntegralImplicits._ object Day21 { @@ -27,11 +28,37 @@ object Day21 { SimultaneousBFS.search(graphSearch).distances.count(_._2 % 2 == steps % 2) } + // copied from 2021 day 25 + extension (pos: Pos) { + def %+(other: Pos): Pos = Pos(pos.x %+ other.x, pos.y %+ other.y) + } + + def countReachableExactlyInfinite(grid: Grid[Char], steps: Int = 26501365): Int = { + val gridSize = Pos(grid(0).size, grid.size) + + 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))) } } diff --git a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala index 410a5046..9aba7e07 100644 --- a/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala +++ b/src/test/scala/eu/sim642/adventofcode2023/Day21Test.scala @@ -25,4 +25,18 @@ class Day21Test extends AnyFunSuite { test("Part 1 input answer") { assert(countReachableExactly(parseGrid(input)) == 3687) } + + 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), 1000) == 668697) + //assert(countReachableExactlyInfinite(parseGrid(exampleInput), 5000) == 16733044) + } + + test("Part 2 input answer") { + //assert(countReachableExactlyInfinite(parseGrid(input)) == 3687) + } }