-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (282 loc) · 6.71 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
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
package main
import (
"fmt"
"math"
"strings"
"github.com/jdrst/adventofgo/util"
)
func main() {
fmt.Printf("First part: %v\n", partOne(util.ReadFile("input.txt")))
fmt.Printf("Second part: %v\n", partTwo(util.ReadFile("input.txt")))
}
type amphipod struct {
tpe rune
energyPerStep, destination int
x, y int
hasMoved bool
}
type burrow struct {
depth int
usedEnergy int
pods []amphipod
}
type cache struct {
currentBest *int
results map[string]int
}
func partOne(file util.File) int {
lines := file.AsLines()
max := math.MaxInt
c := cache{currentBest: &max, results: map[string]int{}}
res := moveUntilDone(makeBurrow(lines), &c)
return res
}
func makeBurrow(lines util.Lines) burrow {
pods := lines[2 : len(lines)-1]
return burrow{usedEnergy: 0, pods: parseAmphipods(pods), depth: len(pods)}
}
func moveUntilDone(b burrow, c *cache) int {
// fmt.Println(b)
for _, p := range b.pods {
if p.x != p.destination {
goto notDone
}
}
return b.usedEnergy
notDone:
if v, exists := c.results[fmt.Sprint(b)]; exists {
return v
}
if b.usedEnergy > *c.currentBest {
return math.MaxInt
}
res := math.MaxInt
for _, p := range b.pods {
for _, next := range p.move(b) {
energyUsed := moveUntilDone(next, c)
res = min(res, energyUsed)
}
}
if res < *c.currentBest {
c.currentBest = &res
}
c.results[fmt.Sprint(b)] = res
return res
}
func (a amphipod) move(b burrow) (possibilities []burrow) {
energy := b.usedEnergy
//remove self
currentPods := make([]amphipod, len(b.pods))
copy(currentPods, b.pods)
currentPods = deletePod(a.x, a.y, currentPods)
//move out of side room
if a.y > 0 {
if a.hasMoved {
return
}
//won't move if in correct destination
if a.y < 4 && a.x == a.destination {
for y := 4; y >= a.y; y-- {
if p, exists := exists(a.x, y, b.pods); exists {
if p.tpe != a.tpe {
goto notDone
}
}
}
return
}
notDone:
//can't move at all if entry is blocked
for y := a.y - 1; y >= 0; y-- {
if _, exists := exists(a.x, y, b.pods); exists {
return
}
}
_, blockedLeft := exists(a.x-1, 0, b.pods)
_, blockedRight := exists(a.x+1, 0, b.pods)
if blockedLeft && blockedRight {
return
}
//move upwards
newY := a.y
for newY > 0 {
newY--
energy += a.energyPerStep
}
//left possibilities
var newEnergy int
if !blockedLeft {
newEnergy = energy
for x := a.x - 1; x >= 0; x-- {
newEnergy += a.energyPerStep
if x == 2 || x == 4 || x == 6 || x == 8 {
continue
}
if _, exists := exists(x, 0, b.pods); exists {
break
}
tempPods := make([]amphipod, len(currentPods))
copy(tempPods, currentPods)
tempPods = append(tempPods, amphipod{tpe: a.tpe, energyPerStep: a.energyPerStep, x: x, y: newY, destination: a.destination, hasMoved: true})
possibilities = append(possibilities, burrow{usedEnergy: newEnergy, pods: tempPods, depth: b.depth})
}
}
//right possibilities
if !blockedRight {
newEnergy = energy
for x := a.x + 1; x < 11; x++ {
newEnergy += a.energyPerStep
if x == 2 || x == 4 || x == 6 || x == 8 {
continue
}
if _, exists := exists(x, 0, b.pods); exists {
break
}
tempPods := make([]amphipod, len(currentPods))
copy(tempPods, currentPods)
tempPods = append(tempPods, amphipod{tpe: a.tpe, energyPerStep: a.energyPerStep, x: x, y: newY, destination: a.destination, hasMoved: true})
possibilities = append(possibilities, burrow{usedEnergy: newEnergy, pods: tempPods, depth: b.depth})
}
}
}
//move into side room
if a.y == 0 {
//can't move if side room occupated
if _, exists := exists(a.destination, 1, b.pods); exists {
return
}
//don't move in if occupied with wrong type
for y := b.depth; y > 0; y-- {
if p, exists := exists(a.destination, y, b.pods); exists {
if p.tpe != a.tpe {
return
}
}
}
//can't move if pod is in the way
if a.x > a.destination {
for x := a.x - 1; x >= a.destination; x-- {
if _, exists := exists(x, 0, b.pods); exists {
return
}
}
}
if a.x < a.destination {
for x := a.x + 1; x <= a.destination; x++ {
if _, exists := exists(x, 0, b.pods); exists {
return
}
}
}
//move into target
newY := b.depth
for newY > 0 {
if _, exists := exists(a.destination, newY, b.pods); !exists {
break
}
newY--
}
newPods := make([]amphipod, len(currentPods))
copy(newPods, currentPods)
newEnergy := energy + util.Delta(a.x, a.destination)*a.energyPerStep + newY*a.energyPerStep
newPods = append(newPods, amphipod{tpe: a.tpe, energyPerStep: a.energyPerStep, x: a.destination, y: newY, destination: a.destination, hasMoved: true})
possibilities = append(possibilities, burrow{usedEnergy: newEnergy, pods: newPods, depth: b.depth})
}
return possibilities
}
func parseAmphipods(lines util.Lines) []amphipod {
pods := []amphipod{}
for i, l := range lines {
for j, c := range string(l) {
var ePs, dest int
switch c {
case 'A':
ePs = 1
dest = 2
case 'B':
ePs = 10
dest = 4
case 'C':
ePs = 100
dest = 6
case 'D':
ePs = 1000
dest = 8
default:
continue
}
pods = append(pods, amphipod{tpe: c, energyPerStep: ePs, destination: dest, x: j - 1, y: i + 1})
}
}
return pods
}
func (b burrow) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("used energy: %v\n", b.usedEnergy))
sb.WriteString("#############\n")
sb.WriteRune('#')
for x, y := 0, 0; x < 11; x++ {
if p, exists := exists(x, y, b.pods); exists {
sb.WriteRune(p.tpe)
} else {
sb.WriteRune('.')
}
}
sb.WriteString("#\n")
for y := 1; y <= b.depth; y++ {
if y == 1 {
sb.WriteString("##")
} else {
sb.WriteString(" ")
}
for x := 1; x < 10; x++ {
if x%2 == 1 {
sb.WriteRune('#')
continue
}
if p, exists := exists(x, y, b.pods); exists {
sb.WriteRune(p.tpe)
} else {
sb.WriteRune('.')
}
}
if y == 1 {
sb.WriteString("##")
}
sb.WriteString("\n")
}
sb.WriteString(" #########")
return sb.String()
}
func partTwo(file util.File) int {
lines := file.AsLines()
lines = append(lines[:5], lines[3:]...)
lines[3] = " #D#C#B#A#"
lines[4] = " #D#B#A#C#"
max := math.MaxInt
c := cache{currentBest: &max, results: map[string]int{}}
res := moveUntilDone(makeBurrow(lines), &c)
return res
}
func exists(x, y int, pods []amphipod) (*amphipod, bool) {
for i, p := range pods {
if p.x == x && p.y == y {
return &pods[i], true
}
}
return nil, false
}
func deletePod(x, y int, pods []amphipod) []amphipod {
for i, p := range pods {
if p.x == x && p.y == y {
return append(pods[:i], pods[i+1:]...)
}
}
return pods
}
func min(a, b int) int {
if a > b {
return b
}
return a
}