Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 19, 2023
1 parent 530aeae commit 6b9df2a
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 74 deletions.
19 changes: 10 additions & 9 deletions scala2/src/main/scala/jurisk/adventofcode/y2018/Advent13.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 => '<'
Expand All @@ -49,22 +51,19 @@ 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 {
case Direction2D.S => E
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),
)
}
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 7 additions & 8 deletions scala2/src/main/scala/jurisk/adventofcode/y2018/Advent23.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 6 additions & 8 deletions scala2/src/main/scala/jurisk/adventofcode/y2022/Advent21.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent16.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand Down
41 changes: 17 additions & 24 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/pipe/Pipe.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ final case class CoordsAndDirection2D(
) {
def nextStraight: CoordsAndDirection2D =
CoordsAndDirection2D(coords + direction, direction)

def moveInDirection(newDirection: CardinalDirection2D): CoordsAndDirection2D =
CoordsAndDirection2D(
coords + newDirection,
newDirection,
)
}
1 change: 1 addition & 0 deletions scala2/src/main/scala/jurisk/geometry/Field2D.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)) =>
Expand Down
28 changes: 13 additions & 15 deletions scala2/src/main/scala/jurisk/optimization/Optimizer.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 6b9df2a

Please sign in to comment.