From 644f258cda812768b4138cb45d103f9ef9d91ce4 Mon Sep 17 00:00:00 2001 From: twentylemon Date: Sun, 15 Dec 2024 13:13:32 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20day=2015=20part=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/lemon/advent/year2024/Day15.scala | 31 ++++++------------- .../org/lemon/advent/year2024/Day15Test.scala | 7 ++--- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/main/scala/org/lemon/advent/year2024/Day15.scala b/src/main/scala/org/lemon/advent/year2024/Day15.scala index 3348c7c..09dd1d0 100644 --- a/src/main/scala/org/lemon/advent/year2024/Day15.scala +++ b/src/main/scala/org/lemon/advent/year2024/Day15.scala @@ -24,14 +24,10 @@ private object Day15: (Coord.gridToMap(wide), parseMoves(moves)) def step(grid: Map[Coord, Char], robit: Coord, dir: Direction): Map[Coord, Char] = - println(s"robit = $robit moving = $dir") - val moves = Iterator.unfold(Set(robit)) { moving => - println(s"moving = $moving") if moving.isEmpty then None else val nexts = moving.map(_ + dir) - println(s"nexts = $nexts") val boxes = nexts.flatMap(p => grid(p) match case 'O' => Seq(p) @@ -39,26 +35,20 @@ private object Day15: case ']' => Seq(p.left, p) case _ => Seq() ).filterNot(moving) - println(s"boxes = $boxes") Some(moving -> boxes) }.toSeq - println(s"moves = $moves") - val ret = - if moves.isEmpty then grid + if moves.isEmpty then grid + else + val dest = moves.flatten.map(_ + dir) + if dest.exists(grid(_) == '#') then grid else - val dest = moves.last.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)) } - } - - println(Area(ret).show(ret)) - ret + 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)) } + } def gps(grid: Map[Coord, Char], box: Char) = grid.filter(_._2 == box).keysIterator.map(c => 100 * c.y + c.x).sum @@ -70,6 +60,5 @@ private object Day15: def part2(input: String) = val (grid, moves) = parseWide(input) - println(Area(grid).show(grid)) val last = moves.foldLeft(grid)((g, d) => step(g, g.find(_._2 == '@').get._1, d)) gps(last, '[') diff --git a/src/test/scala/org/lemon/advent/year2024/Day15Test.scala b/src/test/scala/org/lemon/advent/year2024/Day15Test.scala index a9998a9..15c8012 100644 --- a/src/test/scala/org/lemon/advent/year2024/Day15Test.scala +++ b/src/test/scala/org/lemon/advent/year2024/Day15Test.scala @@ -86,7 +86,6 @@ class Day15Test extends UnitTest: part2(in) shouldBe 9021 } -// test("part 2") { -// part2(read(file(2024)(15))) should be > 1463651 -// part2(read(file(2024)(15))) shouldBe 0 -// } + test("part 2") { + part2(read(file(2024)(15))) shouldBe 1472235 + }