Skip to content

Commit

Permalink
🎨 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 15, 2024
1 parent 644f258 commit f893208
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/main/scala/org/lemon/advent/year2024/Day15.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private object Day15:
(Coord.gridToMap(wide), parseMoves(moves))

def step(grid: Map[Coord, Char], robit: Coord, dir: Direction): Map[Coord, Char] =
val moves = Iterator.unfold(Set(robit)) { moving =>
val moves = Seq.unfold(Set(robit)) { moving =>
if moving.isEmpty then None
else
val nexts = moving.map(_ + dir)
Expand All @@ -36,19 +36,12 @@ private object Day15:
case _ => Seq()
).filterNot(moving)
Some(moving -> boxes)
}.toSeq
}.flatten

if moves.isEmpty then grid
if moves.exists(c => grid(c + dir) == '#') then grid
else
val dest = moves.flatten.map(_ + dir)
if dest.exists(grid(_) == '#') then grid
else
val gapsFilled = moves.foldLeft(grid) { (g, moving) =>
moving.foldLeft(g) { (g, coord) => g.updated(coord, '.') }
}
moves.foldLeft(gapsFilled) { (g, moving) =>
moving.foldLeft(g) { (g, coord) => g.updated(coord + dir, grid(coord)) }
}
val gapsFilled = moves.foldLeft(grid) { (g, coord) => g.updated(coord, '.') }
moves.foldLeft(gapsFilled) { (g, coord) => g.updated(coord + dir, grid(coord)) }

def gps(grid: Map[Coord, Char], box: Char) =
grid.filter(_._2 == box).keysIterator.map(c => 100 * c.y + c.x).sum
Expand Down

0 comments on commit f893208

Please sign in to comment.