-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.go
273 lines (213 loc) · 5.27 KB
/
day23.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
package main
import (
"math"
"os"
"panic"
"readers"
"strconv"
)
var costs = []int64{1, 10, 100, 1000}
func getCost(unit, from, to int) int64 {
if from > to {
tmp := from
from = to
to = tmp
}
depth := int64((to - 3) / 4)
targetHall := ((to+1)%4)*2 + 3
discount := int64(0)
if from == 0 || from == 6 {
discount++
}
distance := int64(math.Abs(float64(2*from-targetHall))) + depth - discount
return costs[getType(unit)] * distance
}
type burrowState struct {
cost int64
positions []int
}
func (b *burrowState) moveUnit(unit, position int, newCost int64) burrowState {
newPositions := make([]int, len(b.positions))
copy(newPositions, b.positions)
newPositions[unit] = position
return burrowState{
positions: newPositions,
cost: b.cost + newCost,
}
}
func (b *burrowState) isDone() bool {
for i := 0; i < len(b.positions); i++ {
if b.positions[i] < 7 || (b.positions[i]+1)%4 != getType(i) {
return false
}
}
return true
}
func (b *burrowState) toStateString() string {
grid := make([]int, totalAmphipods()+7)
for i := 0; i < len(grid); i++ {
grid[i] = -1
}
for i := 0; i < totalAmphipods(); i++ {
grid[b.positions[i]] = i
}
output := ""
for i := 0; i < len(grid); i++ {
unitType := getType(grid[i])
if unitType == -1 {
output += "-"
} else {
output += strconv.Itoa(unitType)
}
}
return output
}
var columnSize = 0
func totalAmphipods() int {
return columnSize * 4
}
func getType(unit int) int {
if unit == -1 {
return -1
}
return unit / columnSize
}
func findValidPosition(positions []int, unit int) []bool {
if positions[unit] < 7 {
return findValidRoomPositions(positions, unit)
}
return findValidHallwayPositions(positions, unit)
}
func findValidHallwayPositions(position []int, unit int) []bool {
grid := make([]int, totalAmphipods()+7)
for i := 0; i < len(grid); i++ {
grid[i] = -1
}
for i := 0; i < totalAmphipods(); i++ {
grid[position[i]] = i
}
validPositions := make([]bool, 7)
currentPosition := position[unit]
for i := currentPosition - 4; i > 6; i -= 4 {
if grid[i] != -1 {
return validPositions
}
}
currentType := getType(unit)
if (currentPosition+1)%4 == currentType {
hasToMove := false
for i := currentPosition + 4; i < totalAmphipods()+7; i += 4 {
if getType(grid[i]) != currentType {
hasToMove = true
break
}
}
if !hasToMove {
return validPositions
}
}
newPosition := currentPosition
for newPosition > 10 {
newPosition -= 4
}
for i := 0; i < 7; i++ {
validPositions[i] = grid[i] == -1 && checkHallwayToRoom(i, newPosition, grid)
}
return validPositions
}
func findValidRoomPositions(position []int, unit int) []bool {
grid := make([]int, totalAmphipods()+7)
for i := 0; i < len(grid); i++ {
grid[i] = -1
}
for i := 0; i < totalAmphipods(); i++ {
grid[position[i]] = i
}
validPositions := make([]bool, totalAmphipods()+7)
currentPosition := position[unit]
currentType := getType(unit)
roomForType := currentType + 7
if !checkHallwayToRoom(currentPosition, roomForType, grid) {
return validPositions
}
target := roomForType
for i := 0; i < columnSize; i++ {
if grid[roomForType+4*i] == -1 {
target = roomForType + 4*i
} else if getType(grid[roomForType+4*i]) != currentType {
return validPositions
}
}
validPositions[target] = true
return validPositions
}
func checkHallwayToRoom(hallwayPosition int, roomPosition int, grid []int) bool {
min := int(math.Min(float64(hallwayPosition+1), float64(roomPosition-5)))
max := int(math.Max(float64(hallwayPosition-1), float64(roomPosition-6)))
for i := min; i <= max; i++ {
if grid[i] != -1 {
return false
}
}
return true
}
func solve(lines []string) int64 {
columnSize = len(lines) - 3
positions := make([]int, totalAmphipods())
for i := 0; i < columnSize; i++ {
line := lines[i+2]
for j := 0; j < 4; j++ {
unit := int(line[2*j+3]-'A') * columnSize
for positions[unit] != 0 {
unit++
}
positions[unit] = 4*i + j + 7
}
}
stateQueue := make([]burrowState, 1)
stateQueue[0] = burrowState{positions: positions, cost: 0}
cache := make(map[string]int64)
bestCost := int64(math.MaxInt64)
for len(stateQueue) > 0 {
state := stateQueue[0]
stateQueue = stateQueue[1:]
if state.cost >= bestCost {
continue // break if priority queue, as only more expensive can come
}
for unit := 0; unit < totalAmphipods(); unit++ {
for i, validPosition := range findValidPosition(state.positions, unit) {
if !validPosition {
continue
}
newCost := getCost(unit, state.positions[unit], i)
newState := state.moveUnit(unit, i, newCost)
if newState.isDone() {
if newState.cost < bestCost {
bestCost = newState.cost
}
} else {
cacheKey := newState.toStateString()
if cacheValue, exists := cache[cacheKey]; !exists || newState.cost < cacheValue {
cache[cacheKey] = newState.cost
stateQueue = append(stateQueue, newState)
}
}
}
}
}
return bestCost
}
func Day23Part1() int64 {
file, err := os.Open("assets/day23.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
return solve(lines)
}
func Day23Part2() int64 {
file, err := os.Open("assets/day23part2.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
return solve(lines)
}