8000 Clean up 2024 day 15 · sim642/adventofcode@e6f4e3b · GitHub
[go: up one dir, main page]

Skip to content

Commit e6f4e3b

Browse files
committed
Clean up 2024 day 15
1 parent 4dd8a4f commit e6f4e3b

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day15.scala

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ object Day15 {
1717

1818
trait Part {
1919
def simulateMove(grid: Grid[Char], robot: Pos, move: Pos): (Grid[Char], Pos)
20-
def sumBoxGps(grid: Grid[Char]): Int
20+
val boxGpsChar: Char
21+
22+
def sumBoxGps(grid: Grid[Char]): Int = {
23+
(for {
24+
(row, y) <- grid.view.zipWithIndex
25+
(cell, x) <- row.view.zipWithIndex
26+
if cell == boxGpsChar
27+
} yield 100 * y + x).sum
28+
}
2129

2230
def sumMovesBoxGps(input: Input): Int = {
2331
val initialRobot = input.grid.posOf('@')
@@ -39,13 +47,7 @@ object Day15 {
3947
(grid, robot)
4048
}
4149

42-
override def sumBoxGps(grid: Grid[Char]): Int = {
43-
(for {
44-
(row, y) <- grid.view.zipWithIndex
45-
(cell, x) <- row.view.zipWithIndex
46-
if cell == 'O'
47-
} yield 100 * y + x).sum
48-
}
50+
override val boxGpsChar: Char = 'O'
4951
}
5052

5153
object Part2 extends Part {
@@ -60,28 +62,33 @@ object Day15 {
6062

6163
override def simulateMove(grid: Grid[Char], robot: Pos, move: Pos): (Grid[Char], Pos) = {
6264

63-
def helper(grid: Grid[Char], pos: Pos): Option[Grid[Char]] = grid(pos) match {
64-
case '.' => Some(grid)
65-
case '#' => None
66-
case '[' if move.x == 0 => // vertical
67-
for {
68-
newGrid <- helper(grid, pos + move)
69-
newGrid2 <- helper(newGrid, pos + Pos(1, 0) + move)
70-
} yield newGrid2.updatedGrid(pos + move, '[').updatedGrid(pos + Pos(1, 0) + move, ']').updatedGrid(pos, '.').updatedGrid(pos + Pos(1, 0), '.')
71-
case ']' if move.x == 0 => // vertical
72-
for {
73-
newGrid <- helper(grid, pos + move)
74-
newGrid2 <- helper(newGrid, pos - Pos(1, 0) + move)
75-
} yield newGrid2.updatedGrid(pos + move, ']').updatedGrid(pos - Pos(1, 0) + move, '[').updatedGrid(pos, '.').updatedGrid(pos - Pos(1, 0), '.')
76-
case '[' if move.y == 0 => // horizontal
77-
for {
78-
newGrid <- helper(grid, pos + move)
79-
} yield newGrid.updatedGrid(pos + move, '[').updatedGrid(pos, '.')
80-
case ']' if move.y == 0 => // horizontal
81-
for {
82-
newGrid <- helper(grid, pos + move)
83-
} yield newGrid.updatedGrid(pos + move, ']').updatedGrid(pos, '.')
84-
case _ => ???
65+
def helper(grid: Grid[Char], pos: Pos): Option[Grid[Char]] = {
66+
grid(pos) match {
67+
case '.' => Some(grid)
68+
case '#' => None
69+
case '[' if move.x == 0 => // vertical
70+
val newPos = pos + move
71+
for {
72+
newGrid <- helper(grid, newPos)
73+
newGrid2 <- helper(newGrid, newPos + Pos(1, 0))
74+
} yield
75+
newGrid2
76+
.updatedGrid(newPos, '[')
77+
.updatedGrid(newPos + Pos(1, 0), ']')
78+
.updatedGrid(pos, '.')
79+
.updatedGrid(pos + Pos(1, 0), '.')
80+
case ']' if move.x == 0 => // vertical
81+
helper(grid, pos - Pos(1, 0)) // handle [ instead
82+
case cell@('[' | ']') if move.y == 0 => // horizontal
83+
val newPos = pos + move
84+
for {
85+
newGrid <- helper(grid, newPos)
86+
} yield
87+
newGrid
88+
.updatedGrid(newPos, cell)
89+
.updatedGrid(pos, '.')
90+
case _ => throw new IllegalArgumentException("illegal grid cell")
91+
}
8592
}
8693

8794
val newRobot = robot + move
@@ -91,23 +98,13 @@ object Day15 {
9198
}
9299
}
93100

94-
override def sumBoxGps(grid: Grid[Char]): Int = {
95-
printGrid(grid)
96-
(for {
97-
(row, y) <- grid.view.zipWithIndex
98-
(cell, x) <- row.view.zipWithIndex
99-
if cell == '['
100-
//gpsX = x min (row.size - 1 - x)
101-
//gpsY = y min (grid.size - 1 - y)
102-
gpsX = x
103-
gpsY = y
104-
() = println((gpsX, gpsY))
105-
} yield 100 * gpsY + gpsX).sum
106-
}
101+
// The text is being unnecessarily confusing/misleading here, referring to "closest",
102+
// when it still means left and top...
103+
override val boxGpsChar: Char = '['
107104

108105
override def sumMovesBoxGps(input: Input): Int = {
109106
val scaledGrid = scaleGrid(input.grid)
110-
super.sumMovesBoxGps(Input(scaledGrid, input.moves))
107+
super.sumMovesBoxGps(input.copy(grid = scaledGrid))
111108
}
112109
}
113110

src/test/scala/eu/sim642/adventofcode2024/Day15Test.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Day15Test extends AnyFunSuite {
5454
test("Part 1 examples") {
5555
assert(Part1.sumMovesBoxGps(parseInput(exampleInput2)) == 2028)
5656
assert(Part1.sumMovesBoxGps(parseInput(exampleInput)) == 10092)
57+
assert(Part1.sumMovesBoxGps(parseInput(exampleInput3)) == 908) // from glguy
5758
}
5859

5960
test("Part 1 input answer") {

0 commit comments

Comments
 (0)
0