diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent13.scala b/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent13.scala index 78ad4196..354e9c0c 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent13.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent13.scala @@ -16,16 +16,18 @@ object Advent13 { type Result2 = String final case class Cart( - direction: CardinalDirection2D, - coords: Coords2D, + at: CoordsAndDirection2D, turnsMade: Int, ) { override def toString: String = - s"coords = $coords, direction = $direction, turnsMade = $turnsMade" + s"at = $at, turnsMade = $turnsMade" private val rotations = List(Left90, NoRotation, Right90) - def directionSymbol: Char = direction match { + def coords: Coords2D = at.coords + def direction: CardinalDirection2D = at.direction + + def directionSymbol: Char = at.direction match { case N => '^' case E => '>' case W => '<' @@ -49,7 +51,6 @@ object Advent13 { case Direction2D.E => N case Direction2D.N => E case Direction2D.W => S - case _ => s"Unexpected direction $direction".fail } case Track.NW_SE => direction match { @@ -57,14 +58,12 @@ object Advent13 { case Direction2D.E => S case Direction2D.N => W case Direction2D.W => N - case _ => s"Unexpected direction $direction".fail } case Track.Empty => s"Unexpected situation $square".fail } Cart( - newDirection, - coords + newDirection, + at.moveInDirection(newDirection), turnsMade + (if (square == Track.Intersection) 1 else 0), ) } @@ -189,7 +188,9 @@ object Advent13 { def parse(data: String, processingStrategy: ProcessingStrategy): State = { val field: Field2D[Char] = Field2D.parseCharField(data) val carts = field.entries.flatMap { case (c, ch) => - Direction2D.parseCaretToOption(ch) map { d => Cart(d, c, 0) } + Direction2D.parseCaretToOption(ch) map { d => + Cart(CoordsAndDirection2D(c, d), 0) + } }.toSet val board: Field2D[Track] = field.map { diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent23.scala b/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent23.scala index 7c6c5cb3..b09a05c0 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent23.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2018/Advent23.scala @@ -1,13 +1,12 @@ package jurisk.adventofcode.y2018 -import jurisk.geometry.{Area3D, Coords3D} -import jurisk.optimization.ImplicitConversions.{ - RichArithExprIntSort, - RichExpr, - RichExprBoolSort, - RichExprIntSort, - RichInt, -} +import jurisk.geometry.Area3D +import jurisk.geometry.Coords3D +import jurisk.optimization.ImplicitConversions.RichArithExprIntSort +import jurisk.optimization.ImplicitConversions.RichExpr +import jurisk.optimization.ImplicitConversions.RichExprBoolSort +import jurisk.optimization.ImplicitConversions.RichExprIntSort +import jurisk.optimization.ImplicitConversions.RichInt import jurisk.optimization.Optimizer import jurisk.utils.FileInput._ import jurisk.utils.Parsing.StringOps diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2022/Advent21.scala b/scala2/src/main/scala/jurisk/adventofcode/y2022/Advent21.scala index bb7ebe8e..2bf1920f 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2022/Advent21.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2022/Advent21.scala @@ -3,14 +3,12 @@ package jurisk.adventofcode.y2022 import cats.implicits._ import jurisk.adventofcode.y2022.Advent21.Expression._ import jurisk.adventofcode.y2022.Advent21.Operation._ -import jurisk.optimization.ImplicitConversions.{ - RichArithExprIntSort, - RichBoolExpr, - RichExpr, - RichExprIntSort, - RichLong, - RichString, -} +import jurisk.optimization.ImplicitConversions.RichArithExprIntSort +import jurisk.optimization.ImplicitConversions.RichBoolExpr +import jurisk.optimization.ImplicitConversions.RichExpr +import jurisk.optimization.ImplicitConversions.RichExprIntSort +import jurisk.optimization.ImplicitConversions.RichLong +import jurisk.optimization.ImplicitConversions.RichString import jurisk.optimization.Optimizer import jurisk.utils.FileInput._ import jurisk.utils.Parsing.StringOps diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent10.scala b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent10.scala index 4e7ef358..feb074ee 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent10.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent10.scala @@ -5,13 +5,13 @@ import jurisk.adventofcode.y2023.pipe.Pipe import jurisk.adventofcode.y2023.pipe.Pipe._ import jurisk.algorithms.pathfinding.Bfs import jurisk.algorithms.pathfinding.Dijkstra -import jurisk.geometry.{Coords2D, CoordsAndDirection2D} +import jurisk.geometry.Coords2D +import jurisk.geometry.CoordsAndDirection2D import jurisk.geometry.Direction2D.E import jurisk.geometry.Direction2D.N import jurisk.geometry.Direction2D.S import jurisk.geometry.Direction2D.W import jurisk.geometry.Field2D -import jurisk.geometry.visualizeBoolean import jurisk.utils.CollectionOps.IterableOps import jurisk.utils.FileInput._ import jurisk.utils.Parsing.StringOps diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent16.scala b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent16.scala index 4829770c..c42e33a5 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent16.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent16.scala @@ -5,14 +5,14 @@ import com.microsoft.z3.BoolExpr import jurisk.adventofcode.y2023.Advent16.Square.Empty import jurisk.collections.BiMap import jurisk.collections.BiMap.BiDirectionalArrowAssociation +import jurisk.geometry.Coords2D +import jurisk.geometry.Direction2D import jurisk.geometry.Direction2D._ -import jurisk.geometry.{Coords2D, Direction2D, Field2D} -import jurisk.optimization.ImplicitConversions.{ - RichBoolExpr, - RichExpr, - RichExprBoolSort, - RichString, -} +import jurisk.geometry.Field2D +import jurisk.optimization.ImplicitConversions.RichBoolExpr +import jurisk.optimization.ImplicitConversions.RichExpr +import jurisk.optimization.ImplicitConversions.RichExprBoolSort +import jurisk.optimization.ImplicitConversions.RichString import jurisk.optimization.Optimizer import jurisk.utils.FileInput._ import jurisk.utils.Parsing.StringOps diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent18.scala b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent18.scala index 9fd0f5c3..083811dd 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent18.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent18.scala @@ -61,8 +61,10 @@ object Advent18 { } def solveFloodFill(data: List[MovementInstruction]): Long = { + val pathPoints = MovementInstruction.walkEveryPoint(data) + val field = Field2D - .fromPoints(MovementInstruction.walkEveryPoint(data)) + .fromPoints(pathPoints) .map(if (_) Square.Dug else Square.Unknown) val filled = Field2D.floodFillFromOutside[Square]( diff --git a/scala2/src/main/scala/jurisk/adventofcode/y2023/pipe/Pipe.scala b/scala2/src/main/scala/jurisk/adventofcode/y2023/pipe/Pipe.scala index 295b636f..ac3dea6c 100644 --- a/scala2/src/main/scala/jurisk/adventofcode/y2023/pipe/Pipe.scala +++ b/scala2/src/main/scala/jurisk/adventofcode/y2023/pipe/Pipe.scala @@ -1,23 +1,19 @@ package jurisk.adventofcode.y2023.pipe -import jurisk.geometry.{ - Coords2D, - CoordsAndDirection2D, - Direction2D, - Field2D, - Rotation, -} -import jurisk.geometry.Direction2D.{ - CardinalDirection2D, - E, - N, - NE, - NW, - S, - SE, - SW, - W, -} +import jurisk.geometry.Coords2D +import jurisk.geometry.CoordsAndDirection2D +import jurisk.geometry.Direction2D +import jurisk.geometry.Direction2D.CardinalDirection2D +import jurisk.geometry.Direction2D.E +import jurisk.geometry.Direction2D.N +import jurisk.geometry.Direction2D.NE +import jurisk.geometry.Direction2D.NW +import jurisk.geometry.Direction2D.S +import jurisk.geometry.Direction2D.SE +import jurisk.geometry.Direction2D.SW +import jurisk.geometry.Direction2D.W +import jurisk.geometry.Field2D +import jurisk.geometry.Rotation import jurisk.utils.CollectionOps.IterableOps sealed trait Pipe extends Product with Serializable { @@ -31,17 +27,14 @@ object Pipe { current: CoordsAndDirection2D, field: Field2D[Pipe], ): CoordsAndDirection2D = { - val nextCoords = current.nextStraight.coords - val nextSquare = field.atOrElse(nextCoords, Empty) + val next = current.nextStraight + val nextSquare = field.atOrElse(next.coords, Empty) val nextDirection = nextSquare.connections .filterNot(_ == current.direction.invert) .singleElementUnsafe - CoordsAndDirection2D( - coords = nextCoords, - direction = nextDirection, - ) + next.copy(direction = nextDirection) } // Coordinates to the right of these `coords`, if facing in `direction` diff --git a/scala2/src/main/scala/jurisk/geometry/CoordsAndDirection2D.scala b/scala2/src/main/scala/jurisk/geometry/CoordsAndDirection2D.scala index 213abe4a..02f0d93b 100644 --- a/scala2/src/main/scala/jurisk/geometry/CoordsAndDirection2D.scala +++ b/scala2/src/main/scala/jurisk/geometry/CoordsAndDirection2D.scala @@ -8,4 +8,10 @@ final case class CoordsAndDirection2D( ) { def nextStraight: CoordsAndDirection2D = CoordsAndDirection2D(coords + direction, direction) + + def moveInDirection(newDirection: CardinalDirection2D): CoordsAndDirection2D = + CoordsAndDirection2D( + coords + newDirection, + newDirection, + ) } diff --git a/scala2/src/main/scala/jurisk/geometry/Field2D.scala b/scala2/src/main/scala/jurisk/geometry/Field2D.scala index 5f8b6583..4d84b9b3 100644 --- a/scala2/src/main/scala/jurisk/geometry/Field2D.scala +++ b/scala2/src/main/scala/jurisk/geometry/Field2D.scala @@ -263,6 +263,7 @@ final case class Field2D[T] private ( def expandOneSquareInAllDirections( empty: T )(implicit classTag: ClassTag[T]): Field2D[T] = { + // Could be more efficient, but used rarely val field = Field2D .forArea[T](Area2D(topLeft, bottomRight + Direction2D.SE.diff * 2), empty) valuesAndCoords.foldLeft(field) { case (acc, (c, v)) => diff --git a/scala2/src/main/scala/jurisk/optimization/Optimizer.scala b/scala2/src/main/scala/jurisk/optimization/Optimizer.scala index 3922d9fd..ab97d74a 100644 --- a/scala2/src/main/scala/jurisk/optimization/Optimizer.scala +++ b/scala2/src/main/scala/jurisk/optimization/Optimizer.scala @@ -1,22 +1,20 @@ package jurisk.optimization import cats.implicits.catsSyntaxOptionId +import com.microsoft.z3.ArithExpr +import com.microsoft.z3.ArithSort +import com.microsoft.z3.BoolExpr +import com.microsoft.z3.BoolSort +import com.microsoft.z3.Context +import com.microsoft.z3.Expr +import com.microsoft.z3.IntExpr +import com.microsoft.z3.IntNum +import com.microsoft.z3.IntSort +import com.microsoft.z3.Model +import com.microsoft.z3.Optimize +import com.microsoft.z3.Sort +import com.microsoft.z3.Status import com.microsoft.z3.enumerations.Z3_lbool -import com.microsoft.z3.{ - ArithExpr, - ArithSort, - BoolExpr, - BoolSort, - Context, - Expr, - IntExpr, - IntNum, - IntSort, - Model, - Optimize, - Sort, - Status, -} import jurisk.utils.Parsing.StringOps trait Optimizer {