Skip to content

Commit

Permalink
✨ day 15 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 15, 2024
1 parent 728b221 commit 644f258
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
31 changes: 10 additions & 21 deletions src/main/scala/org/lemon/advent/year2024/Day15.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,31 @@ 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)
case '[' => Seq(p, p.right)
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
Expand All @@ -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, '[')
7 changes: 3 additions & 4 deletions src/test/scala/org/lemon/advent/year2024/Day15Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 644f258

Please sign in to comment.