From fb553ee5d23012875eb6139221b6ecb0e99eba99 Mon Sep 17 00:00:00 2001 From: Juris Date: Sun, 17 Dec 2023 08:04:03 +0200 Subject: [PATCH] 2023-17 - move to A* --- .../jurisk/adventofcode/y2023/Advent17.scala | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent17.scala b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent17.scala index 3edf4299..924d65df 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent17.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent17.scala @@ -1,10 +1,16 @@ package jurisk.adventofcode.y2023 -import cats.implicits.{catsSyntaxOptionId, none} -import jurisk.algorithms.pathfinding.Dijkstra -import jurisk.geometry.Direction2D.{CardinalDirection2D, E, S} -import jurisk.geometry.Rotation.{Left90, NoRotation, Right90} -import jurisk.geometry.{Coords2D, Field2D} +import cats.implicits.catsSyntaxOptionId +import cats.implicits.none +import jurisk.algorithms.pathfinding.AStar +import jurisk.geometry.Coords2D +import jurisk.geometry.Direction2D.CardinalDirection2D +import jurisk.geometry.Direction2D.E +import jurisk.geometry.Direction2D.S +import jurisk.geometry.Field2D +import jurisk.geometry.Rotation.Left90 +import jurisk.geometry.Rotation.NoRotation +import jurisk.geometry.Rotation.Right90 import jurisk.utils.FileInput._ import jurisk.utils.Parsing.StringOps @@ -73,11 +79,16 @@ object Advent17 { minimumBeforeStoppingOrTurning, ) _ - val result = Dijkstra.dijkstra[State, Int]( - State(coords = data.topLeft, none, singleDirectionCounter = 0), + val startState = + State(coords = data.topLeft, none, singleDirectionCounter = 0) + val goal = data.bottomRight + + val result = AStar.aStar[State, Int]( + startState, calculateSuccessors, + _.coords manhattanDistance goal, s => - s.coords == data.bottomRight && s.singleDirectionCounter >= minimumBeforeStoppingOrTurning, + s.coords == goal && s.singleDirectionCounter >= minimumBeforeStoppingOrTurning, ) result match {