-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15_2.go
186 lines (170 loc) · 4.7 KB
/
day15_2.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
package day15
import (
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
type TileEx struct {
Kind ExtendedDefineTile
Row int
Col int
}
type ExtendedDefineTile int
const (
WallEx ExtendedDefineTile = iota
EmptyEx
RobotEx
BoxLeft
BoxRight
NoneEx
)
func canMove(currentparents, parentsToCheck []TileEx, themap [][]TileEx, r, c int, trylevel bool) []TileEx {
if !trylevel {
return []TileEx{}
}
newparentsToCheck := make([]TileEx, 0)
for _, t := range parentsToCheck {
tmp := themap[t.Row+r][t.Col+c]
if tmp.Kind == BoxRight {
newparentsToCheck = append(newparentsToCheck, themap[tmp.Row][tmp.Col-1])
newparentsToCheck = append(newparentsToCheck, tmp)
} else if tmp.Kind == BoxLeft {
newparentsToCheck = append(newparentsToCheck, tmp)
newparentsToCheck = append(newparentsToCheck, themap[tmp.Row][tmp.Col+1])
} else {
newparentsToCheck = append(newparentsToCheck, tmp)
}
}
allareempty := true
notwallfound := true
filteredparents := make([]TileEx, 0)
for _, p := range newparentsToCheck {
if p.Kind != EmptyEx {
allareempty = false
filteredparents = append(filteredparents, p)
}
if p.Kind == WallEx {
notwallfound = false
}
}
if allareempty {
return append(append(currentparents, parentsToCheck...), newparentsToCheck...)
}
return canMove(append(currentparents, parentsToCheck...), filteredparents, themap, r, c, notwallfound)
}
func take(from []TileEx, themap [][]TileEx, mov DefinedDir) []TileEx {
row, col := directionOfMov(mov)
return canMove(make([]TileEx, 0), from, themap, row, col, true)
}
func findBoxes2(from TileEx, themap [][]TileEx, mov DefinedDir) []TileEx {
result := make([]TileEx, 0)
r, c := directionOfMov(mov)
cantake := true
emptyfound := false
current := themap[from.Row][from.Col]
for cantake {
current = themap[current.Row+r][current.Col+c]
switch current.Kind {
case BoxRight:
if mov == UP || mov == DOWN {
partnerleft := themap[current.Row][current.Col-1]
tosend := make([]TileEx, 0)
tosend = append(tosend, partnerleft, current)
availableboxes := take(tosend, themap, mov)
result = append(result, availableboxes...)
cantake = false
emptyfound = len(availableboxes) > 0
} else {
if mov == LEFT || mov == RIGHT {
result = append(result, current)
}
}
case BoxLeft:
if mov == UP || mov == DOWN {
partnerright := themap[current.Row][current.Col+1]
tosend := make([]TileEx, 0)
tosend = append(tosend, current, partnerright)
availableboxes := take(tosend, themap, mov)
result = append(result, availableboxes...)
cantake = false
emptyfound = len(availableboxes) > 0
} else {
if mov == LEFT || mov == RIGHT {
result = append(result, current)
}
}
case EmptyEx:
if mov == LEFT || mov == RIGHT {
result = append(result, current)
emptyfound = true
cantake = false
}
result = append(result, current)
case WallEx:
cantake = false
}
}
if emptyfound {
utilities.ReverseArray(result)
return result
} else {
return []TileEx{}
}
}
func move2(robot TileEx, themap *[][]TileEx, movs []DefinedDir) {
if len(movs) > 0 {
mov := movs[0]
r, c := directionOfMov(mov)
nextPos := (*themap)[robot.Row+r][robot.Col+c]
switch nextPos.Kind {
case WallEx:
move2(robot, themap, movs[1:])
case EmptyEx:
(*themap)[robot.Row][robot.Col].Kind = EmptyEx
(*themap)[nextPos.Row][nextPos.Col].Kind = RobotEx
robot.Row, robot.Col = nextPos.Row, nextPos.Col
move2(robot, themap, movs[1:])
default:
tilesToMov := findBoxes2(robot, *themap, mov)
if len(tilesToMov) > 0 {
(*themap)[robot.Row][robot.Col].Kind = EmptyEx
for _, t := range tilesToMov {
possiblereplacement := (*themap)[t.Row-r][t.Col-c]
isinpossible := false
for _, tt := range tilesToMov {
if tt.Row == possiblereplacement.Row &&
tt.Col == possiblereplacement.Col {
isinpossible = true
break
}
}
replacementkind := EmptyEx
if isinpossible {
replacementkind = possiblereplacement.Kind
}
if t.Kind != EmptyEx {
(*themap)[t.Row+r][t.Col+c].Kind = t.Kind
}
(*themap)[t.Row][t.Col].Kind = replacementkind
}
(*themap)[nextPos.Row][nextPos.Col].Kind = RobotEx
robot.Row, robot.Col = nextPos.Row, nextPos.Col
move2(robot, themap, movs[1:])
} else {
move2(robot, themap, movs[1:])
}
}
}
}
func Executepart2() int {
var result int = 0
var fileName string = "./day15/day15.txt"
if fileContent, err := utilities.ReadFileAsText(fileName); err == nil {
robotinit, themap, movements := parseContent2(fileContent)
move2(robotinit, &themap, movements)
for _, v := range themap {
for _, t := range v {
result += calculateGPS2(t)
}
}
}
return result
}