-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.go
366 lines (317 loc) · 8.57 KB
/
day15.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"errors"
"fmt"
"log"
"os"
"slices"
"strings"
)
type IntPair struct {
First int
Second int
}
type Node struct {
content []IntPair
moveable bool
left *Node
right *Node
}
func (p IntPair) Add(other IntPair) IntPair {
return IntPair{
First: p.First + other.First,
Second: p.Second + other.Second,
}
}
func (p IntPair) Sub(other IntPair) IntPair {
return IntPair{
First: p.First - other.First,
Second: p.Second - other.Second,
}
}
var dirMap = map[byte]IntPair{
'^': {0, -1},
'<': {-1, 0},
'>': {1, 0},
'v': {0, 1},
}
const (
ROBOT byte = '@'
PACKAGE = 'O'
EMPTY = '.'
WALL = '#'
)
func findIndicesInLine(array [][]byte, start IntPair, direction IntPair) []IntPair {
rows := len(array)
if rows == 0 {
return nil
}
cols := len(array[0])
result := []IntPair{}
current := start
// Traverse in the specified direction until out of bounds
for {
// Check bounds
if current.First <= 0 || current.First >= rows || current.Second <= 0 || current.Second >= cols || array[current.Second][current.First] == '#' {
break
}
// Append the current index
result = append(result, current)
// Move to the next index in the direction
current = IntPair{
First: current.First + direction.First,
Second: current.Second + direction.Second,
}
}
return result
}
func findCharIndex(data [][]byte, target byte) (IntPair, bool) {
for j, outer := range data {
for i, char := range outer {
if char == target {
return IntPair{i, j}, true
}
}
}
return IntPair{-1, -1}, false
}
func GPSLocation(data [][]byte, target byte) int64 {
acc := int64(0)
for j, outer := range data {
for i, char := range outer {
if char == target {
acc += int64(100*j + i)
}
}
}
return acc
}
func findSegment(direction byte, current IntPair, mapOfFactory [][]byte) []IntPair {
return findIndicesInLine(mapOfFactory, current, dirMap[direction])
}
func findFirstDot(segment []IntPair, mapOfFactory [][]byte) (IntPair, error) {
for _, seg := range segment {
if mapOfFactory[seg.Second][seg.First] == '.' {
return seg, nil
}
}
return IntPair{0, 0}, errors.New("no free dot found")
}
func moveRobot(current IntPair, segment []IntPair, mapOfFactory [][]byte) IntPair {
if len(segment) == 0 {
return current
}
firstPoint, err := findFirstDot(segment, mapOfFactory)
if err != nil {
return current
}
mapOfFactory[current.Second][current.First] = '.'
lastChar := byte('@')
for _, index := range segment[1:] {
temp := mapOfFactory[index.Second][index.First]
mapOfFactory[index.Second][index.First] = byte(lastChar)
lastChar = temp
if index == firstPoint {
break
}
}
return segment[1]
}
func initLeftRight(node *Node) {
if node.left == nil { // Initialize the left branch if nil
node.left = &Node{
content: make([]IntPair, 0),
moveable: false,
}
}
if node.right == nil { // Initialize the right branch if nil
node.right = &Node{
content: make([]IntPair, 0),
moveable: false,
}
}
}
func findYSegment(mapOfFactory [][]byte, current IntPair, direction IntPair, currentNode *Node) {
rows := len(mapOfFactory)
cols := len(mapOfFactory[0])
if currentNode == nil {
currentNode = &Node{
content: make([]IntPair, 0),
moveable: true,
left: nil,
right: nil,
}
}
if current.First < 0 || current.First >= cols || current.Second < 0 || current.Second >= rows {
return
}
prev := current.Sub(direction)
switch mapOfFactory[current.Second][current.First] {
case '[':
currentNode.content = append(currentNode.content, current)
if mapOfFactory[prev.Second][prev.First] == '[' {
findYSegment(mapOfFactory, current.Add(direction), direction, currentNode)
} else {
initLeftRight(currentNode)
currentNode.moveable = true
findYSegment(mapOfFactory, current.Add(direction), direction, currentNode.left)
findYSegment(mapOfFactory, current.Add(direction).Add(dirMap['>']), direction, currentNode.right)
}
case ']':
currentNode.content = append(currentNode.content, current)
currentNode.moveable = true
if mapOfFactory[prev.Second][prev.First] == ']' {
findYSegment(mapOfFactory, current.Add(direction), direction, currentNode)
} else {
initLeftRight(currentNode)
findYSegment(mapOfFactory, current.Add(direction), direction, currentNode.right)
findYSegment(mapOfFactory, current.Add(direction).Add(dirMap['<']), direction, currentNode.left)
}
case '.':
currentNode.moveable = true
currentNode.content = append(currentNode.content, current)
return
case '#':
return
}
}
func findXSegment(mapOfFactory [][]byte, start IntPair, direction IntPair, node *Node) {
rows := len(mapOfFactory)
cols := len(mapOfFactory[0])
current := start
// Traverse in the specified direction until out of bounds
for {
// Check bounds
if current.First <= 0 || current.First >= cols || current.Second <= 0 || current.Second >= rows || mapOfFactory[current.Second][current.First] == '#' {
break
}
node.content = append(node.content, current)
if mapOfFactory[current.Second][current.First] == '.' {
node.moveable = true
return
}
// Append the current index
// Move to the next index in the direction
current = current.Add(direction)
}
}
func findSegment2(direction byte, current IntPair, mapOfFactory [][]byte) Node {
currentNode := Node{
content: make([]IntPair, 0),
moveable: false,
left: nil,
right: nil,
}
if direction == '<' || direction == '>' {
findXSegment(mapOfFactory, current.Add(dirMap[direction]), dirMap[direction], ¤tNode)
} else {
findYSegment(mapOfFactory, current.Add(dirMap[direction]), dirMap[direction], ¤tNode)
}
return currentNode
}
func removeLeafs(root *Node) (*Node, []*Node) {
if root == nil {
return nil, nil
}
// Check if the current node is a leaf.
if root.left == nil && root.right == nil {
// This node is a leaf, so "remove" it by returning nil and the node itself.
return nil, []*Node{root}
}
// Recursively process left and right subtrees.
var removedLeafs []*Node
if root.left != nil {
root.left, removedLeafs = removeLeafs(root.left)
}
if root.right != nil {
var rightLeafs []*Node
root.right, rightLeafs = removeLeafs(root.right)
removedLeafs = append(removedLeafs, rightLeafs...)
}
return root, removedLeafs
}
func moveRobot2(idx IntPair, direction IntPair, segment *Node, mapOfFactory [][]byte) IntPair {
var leafes []*Node
for segment != nil || leafes != nil {
segment, leafes = removeLeafs(segment)
for _, leaf := range leafes {
if !leaf.moveable {
return idx
}
}
for _, leaf := range leafes {
slices.Reverse(leaf.content)
prev := direction
for _, position := range leaf.content {
prev = position.Sub(direction)
mapOfFactory[position.Second][position.First] = mapOfFactory[prev.Second][prev.First]
}
if len(leaf.content) != 0 {
mapOfFactory[prev.Second][prev.First] = '.'
}
}
}
return idx.Add(direction)
}
func makeBigMap(mapOfFactory [][]byte) [][]byte {
bigMap := make([][]byte, len(mapOfFactory))
for i, line := range mapOfFactory {
preallocatedSize := len(line) * 2 // Each character doubles in size
bigMap[i] = make([]byte, 0, preallocatedSize)
for _, char := range line {
switch char {
case WALL:
bigMap[i] = append(bigMap[i], '#', '#')
case ROBOT:
bigMap[i] = append(bigMap[i], '@', '.')
case EMPTY:
bigMap[i] = append(bigMap[i], '.', '.')
case PACKAGE:
bigMap[i] = append(bigMap[i], '[', ']')
}
}
}
return bigMap
}
func main() {
content, err := os.ReadFile("input15.txt")
if err != nil {
log.Fatal(err)
}
results := strings.Split(string(content), "\n\n")
mapOfFactoryString := strings.Split(results[0], "\n")
mapOfFactory := make([][]byte, len(mapOfFactoryString))
for i, s := range mapOfFactoryString {
mapOfFactory[i] = []byte(s)
}
bigMap := makeBigMap(mapOfFactory)
directions := []byte(results[1])
idx, found := findCharIndex(mapOfFactory, '@')
if !found {
log.Fatal("Robot not found")
}
for _, direction := range directions {
if !(direction == '\n') {
segment := findSegment(direction, idx, mapOfFactory)
idx = moveRobot(idx, segment, mapOfFactory)
}
}
packages := GPSLocation(mapOfFactory, 'O')
idx, found = findCharIndex(bigMap, '@')
if !found {
log.Fatal("Robot not found")
}
for _, direction := range directions {
if !(direction == '\n') {
segment := findSegment2(direction, idx, bigMap)
idx = moveRobot2(idx, dirMap[direction], &segment, bigMap)
fmt.Printf("%c \n", direction)
for _, rows := range bigMap {
fmt.Println(string(rows))
}
}
}
fmt.Println(packages)
packages = GPSLocation(bigMap, '[')
fmt.Println(packages)
}