@@ -17,7 +17,15 @@ object Day15 {
17
17
18
18
trait Part {
19
19
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
+ }
21
29
22
30
def sumMovesBoxGps (input : Input ): Int = {
23
31
val initialRobot = input.grid.posOf('@' )
@@ -39,13 +47,7 @@ object Day15 {
39
47
(grid, robot)
40
48
}
41
49
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'
49
51
}
50
52
51
53
object Part2 extends Part {
@@ -60,28 +62,33 @@ object Day15 {
60
62
61
63
override def simulateMove (grid : Grid [Char ], robot : Pos , move : Pos ): (Grid [Char ], Pos ) = {
62
64
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
+ }
85
92
}
86
93
87
94
val newRobot = robot + move
@@ -91,23 +98,13 @@ object Day15 {
91
98
}
92
99
}
93
100
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 = '['
107
104
108
105
override def sumMovesBoxGps (input : Input ): Int = {
109
106
val scaledGrid = scaleGrid(input.grid)
110
- super .sumMovesBoxGps(Input (scaledGrid, input.moves ))
107
+ super .sumMovesBoxGps(input.copy(grid = scaledGrid ))
111
108
}
112
109
}
113
110
0 commit comments