Skip to content

Commit

Permalink
2023-18
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 18, 2023
1 parent 94e5d04 commit 021cbbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
27 changes: 8 additions & 19 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent18.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jurisk.adventofcode.y2023

import cats.implicits.catsSyntaxUnorderedFoldableOps
import jurisk.algorithms.pathfinding.Bfs
import cats.implicits._
import jurisk.geometry._
import jurisk.utils.FileInput._
import jurisk.utils.Parsing.StringOps
Expand Down Expand Up @@ -62,27 +61,17 @@ object Advent18 {
}

def solveFloodFill(data: List[MovementInstruction]): Long = {
var field = Field2D
.fromPoints(
MovementInstruction.walkEveryPoint(data),
{
case true => Square.Dug
case false => Square.Unknown
},
)
val field = Field2D
.fromPoints(MovementInstruction.walkEveryPoint(data))
.map(if (_) Square.Dug else Square.Unknown)

field = field.expandOneSquareInAllDirections(Square.Unknown)

// TODO: Field2D has Field2D.floodFillCoordinates that you could also have floodFillFromOutside with just f: T => Boolean ?

val next = Field2D.floodFillField[Square](
val filled = Field2D.floodFillFromOutside[Square](
field,
field.topLeft,
(_, to) => to == Square.Unknown,
Square.Outside,
outside = Square.Unknown,
mark = Square.Outside,
)

next.count(x => x == Square.Dug || x == Square.Unknown)
filled.count(_ != Square.Outside)
}

def solvePicksShoelace(data: List[MovementInstruction]): Long = {
Expand Down
29 changes: 23 additions & 6 deletions scala2/src/main/scala/jurisk/geometry/Field2D.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ final case class Field2D[T] private (
acc.updatedAtUnsafe(c + Direction2D.SE, v)
}
}

def contractOneSquareInAllDirections: Field2D[T] =
Field2D(data.tail.init.map(_.tail.init))
}

object Field2D {
Expand Down Expand Up @@ -298,15 +301,14 @@ object Field2D {
topLeft,
)

def fromPoints[T: ClassTag](
points: Seq[Coords2D],
f: Boolean => T,
): Field2D[T] = {
def fromPoints(
points: Seq[Coords2D]
): Field2D[Boolean] = {
val boundingBox = Area2D.boundingBoxInclusive(points)
val field = Field2D.forArea(boundingBox, f(false))
val field = Field2D.forArea(boundingBox, false)

points.foldLeft(field) { case (acc, c) =>
acc.updatedAtUnsafe(c, f(true))
acc.updatedAtUnsafe(c, true)
}
}

Expand All @@ -330,6 +332,21 @@ object Field2D {
},
)

def floodFillFromOutside[T: ClassTag](
field: Field2D[T],
outside: T,
mark: T,
): Field2D[T] = {
val expanded = field.expandOneSquareInAllDirections(outside)
val filled = floodFillField[T](
expanded,
expanded.topLeft,
(_, to) => to == outside,
mark,
)
filled.contractOneSquareInAllDirections
}

def floodFillField[T](
field: Field2D[T],
seed: Coords2D,
Expand Down

0 comments on commit 021cbbf

Please sign in to comment.