From f893208f62d0f4cd9fef73f5c8e579514d6b6ae5 Mon Sep 17 00:00:00 2001 From: twentylemon Date: Sun, 15 Dec 2024 14:25:03 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scala/org/lemon/advent/year2024/Day15.scala | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/main/scala/org/lemon/advent/year2024/Day15.scala b/src/main/scala/org/lemon/advent/year2024/Day15.scala index 09dd1d0..5d0d0ea 100644 --- a/src/main/scala/org/lemon/advent/year2024/Day15.scala +++ b/src/main/scala/org/lemon/advent/year2024/Day15.scala @@ -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) @@ -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