Skip to content

Commit

Permalink
🧪 day 15 part 2, not working
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 15, 2024
1 parent 144fdc7 commit 728b221
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 24 deletions.
75 changes: 58 additions & 17 deletions src/main/scala/org/lemon/advent/year2024/Day15.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,71 @@ import org.lemon.advent.lib.`2d`._

private object Day15:

def parseMoves(input: String): Seq[Direction] =
input.filterNot(_.isWhitespace).map(Direction.apply)

def parse(input: String): (Map[Coord, Char], Seq[Direction]) =
val Array(grid, moves) = input.split("\n\n")
(Coord.gridToMap(grid), moves.filterNot(_.isWhitespace).map(Direction.apply))
(Coord.gridToMap(grid), parseMoves(moves))

def parseWide(input: String): (Map[Coord, Char], Seq[Direction]) =
val Array(grid, moves) = input.split("\n\n")
val wide = grid.flatMap(_ match
case '#' => "##"
case 'O' => "[]"
case '.' => ".."
case '@' => "@."
case x => x.toString
)
(Coord.gridToMap(wide), parseMoves(moves))

def step(grid: Map[Coord, Char], robit: Coord, dir: Direction): Map[Coord, Char] =
val moves = Iterator.iterate(robit)(_ + dir)
.takeWhile(!grid.get(_).exists(c => c == '#' || c == '.'))
.toSeq
val dest = moves.last + dir
if grid(dest) == '#' then grid
else
val shift = moves :+ dest
shift.reverse.sliding(2).foldLeft(grid) {
case (g, Seq(to, from)) => g.updated(to, g(from))
}.updated(robit, '.')
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
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

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

def part1(input: String) =
val (grid, moves) = parse(input)
val last = moves.foldLeft(grid)((g, d) => step(g, g.find(_._2 == '@').get._1, d))
last
.filter(_._2 == 'O')
.keysIterator
.map(c => 100 * c.y + c.x)
.sum
gps(last, 'O')

def part2(input: String) =
0
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, '[')
47 changes: 40 additions & 7 deletions src/test/scala/org/lemon/advent/year2024/Day15Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Day15Test extends UnitTest:
part1(in) shouldBe 2028
}

test("part 1 example") {
test("part 1 large example") {
val in = """|##########
|#..O..O.O#
|#......O.#
Expand Down Expand Up @@ -48,12 +48,45 @@ class Day15Test extends UnitTest:
part1(read(file(2024)(15))) shouldBe 1448589
}

test("part 2 example") {
val in = """|
|""".stripMargin
part2(in) shouldBe 0
test("part 2 small example") {
val in = """|#######
|#...#.#
|#.....#
|#..OO@#
|#..O..#
|#.....#
|#######
|
|<vv<<^^<<^^""".stripMargin
part2(in) shouldBe 618
}

test("part 2") {
part2(read(file(2024)(15))) shouldBe 0
test("part 2 large example") {
val in = """|##########
|#..O..O.O#
|#......O.#
|#.OO..O.O#
|#[email protected].#
|#O#..O...#
|#O..O..O.#
|#.OO.O.OO#
|#....O...#
|##########
|
|<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
|vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
|><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
|<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
|^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
|^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
|>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
|<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
|^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
|v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^""".stripMargin
part2(in) shouldBe 9021
}

// test("part 2") {
// part2(read(file(2024)(15))) should be > 1463651
// part2(read(file(2024)(15))) shouldBe 0
// }

0 comments on commit 728b221

Please sign in to comment.