-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
223 lines (194 loc) · 5.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"strings"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
grid, movements, start := getGridAndMovements(input)
expandedGrid, otherStart := expandGrid(grid)
fmt.Println("answer for part 1: ", getFinalGrid(grid, movements, start))
fmt.Println("answer for part 2: ", getFinalGrid(expandedGrid, movements, otherStart))
}
func getGridAndMovements(input []string) (grid [][]string, movements string, start aoc.Coordinates) {
isMovements := false
for index, row := range input {
if len(row) == 0 {
grid = aoc.Get2DGrid(input[:index+1])
isMovements = true
}
if isMovements && len(row) != 0 {
movements = strings.Join([]string{movements, row}, "")
}
if strings.Contains(row, "@") {
start = aoc.Coordinates{strings.Index(row, "@"), index}
}
}
return
}
func expandGrid(input [][]string) (finalGrid [][]string, start aoc.Coordinates) {
for j, row := range input {
finalRow := []string{}
for i, char := range row {
switch char {
case ".":
finalRow = append(finalRow, []string{".", "."}...)
case "#":
finalRow = append(finalRow, []string{"#", "#"}...)
case "O":
finalRow = append(finalRow, []string{"[", "]"}...)
case "@":
start.X = i * 2
start.Y = j
finalRow = append(finalRow, []string{"@", "."}...)
}
}
finalGrid = append(finalGrid, finalRow)
}
return finalGrid, start
}
func processStep(start aoc.Coordinates, grid [][]string, step string) ([][]string, aoc.Coordinates) {
var direction aoc.Coordinates
switch step {
case "^":
direction = aoc.Coordinates{0, -1}
case "<":
direction = aoc.Coordinates{-1, 0}
case ">":
direction = aoc.Coordinates{1, 0}
case "v":
direction = aoc.Coordinates{0, 1}
}
newX, newY := start.X+direction.X, start.Y+direction.Y
if !isValidStep(aoc.Coordinates{newX, newY}, grid) {
return grid, start
}
switch grid[newY][newX] {
case ".":
grid[start.Y][start.X] = "."
grid[newY][newX] = "@"
return grid, aoc.Coordinates{newX, newY}
case "#":
return grid, start
case "O":
for isValidStep(aoc.Coordinates{newX, newY}, grid) {
switch grid[newY][newX] {
case ".":
grid[start.Y][start.X] = "."
grid[newY][newX] = "O"
grid[start.Y+direction.Y][start.X+direction.X] = "@"
return grid, aoc.Coordinates{start.X + direction.X, start.Y + direction.Y}
case "#":
return grid, start
}
newX += direction.X
newY += direction.Y
}
case "[", "]":
isPossible := moveBoxes(aoc.Coordinates{newX, newY}, direction, grid)
if !isPossible {
return grid, start
}
grid[newY][newX] = "@"
grid[start.Y][start.X] = "."
return grid, aoc.Coordinates{newX, newY}
}
return grid, start
}
func isValidStep(current aoc.Coordinates, input [][]string) bool {
if current.X < 0 || current.Y < 0 || current.X >= len(input[0]) || current.Y >= len(input) {
return false
}
return true
}
func getFinalGrid(grid [][]string, movements string, start aoc.Coordinates) int {
for _, char := range movements {
grid, start = processStep(start, grid, string(char))
}
count := 0
for j, row := range grid {
for i, char := range row {
switch char {
case "O", "[":
count += (i) + (j)*100
}
}
}
return count
}
func moveBoxes(start, direction aoc.Coordinates, grid [][]string) bool {
if direction.Y == 0 && (direction.X == 1 || direction.X == -1) {
return moveBoxesHorizontally(start, direction, grid)
}
if direction.X == 0 && (direction.Y == 1 || direction.Y == -1) {
return moveBoxVertically(start, direction, grid)
}
return false
}
// moveBoxesHorizontally does a DFS to look for the first empty slot where all the
// blocks can be shifted.
func moveBoxesHorizontally(start, direction aoc.Coordinates, grid [][]string) bool {
newX, newY := start.X+direction.X, start.Y+direction.Y
switch grid[newY][newX] {
case "#":
return false
case ".":
grid[newY][newX], grid[start.Y][start.X] = grid[start.Y][start.X], grid[newY][newX]
return true
case "]", "[":
isPossible := moveBoxesHorizontally(aoc.Coordinates{newX, newY}, direction, grid)
if !isPossible {
return false
}
grid[start.Y][start.X], grid[newY][newX] = grid[newY][newX], grid[start.Y][start.X]
}
return true
}
// moveBoxVertically does a BFS to check if all child nodes end with a free space
// such that boxes can be moved. If true, we move the boxes vertically in the given
// direction.
func moveBoxVertically(start, direction aoc.Coordinates, grid [][]string) bool {
next := []aoc.Coordinates{start}
if grid[start.Y][start.X] == "]" {
next = append(next, aoc.Coordinates{start.X - 1, start.Y})
} else {
next = append(next, aoc.Coordinates{start.X + 1, start.Y})
}
visited := make(map[aoc.Coordinates]struct{})
visitedSlice := []aoc.Coordinates{}
for len(next) != 0 {
process := next[0]
next = next[1:]
if _, ok := visited[process]; ok {
continue
}
visited[process] = struct{}{}
visitedSlice = append(visitedSlice, process)
newX, newY := process.X+direction.X, process.Y+direction.Y
switch grid[newY][newX] {
case ".":
continue
case "#":
return false
case "]":
next = append(next, aoc.Coordinates{newX, newY})
next = append(next, aoc.Coordinates{newX - 1, newY})
case "[":
next = append(next, aoc.Coordinates{newX, newY})
next = append(next, aoc.Coordinates{newX + 1, newY})
}
}
// When traversing the grid to perfrom BFS, we also store all the cells
// that we are processing, as these will be the same cells that will need to be moved.
// We start shifting cells from the end of the traversed path
// so that we first move the top-most/bottom-most cell first.
// Each cell is shifted in the desired direction and the current cell is marked empty.
// Moving blocks this way saves us from a lot of corner cases.
for i := len(visitedSlice) - 1; i >= 0; i-- {
x, y := visitedSlice[i].X+direction.X, visitedSlice[i].Y+direction.Y
grid[y][x] = grid[visitedSlice[i].Y][visitedSlice[i].X]
grid[visitedSlice[i].Y][visitedSlice[i].X] = "."
}
return true
}