-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
499 lines (441 loc) · 9.92 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"io"
"strings"
"unicode"
pq "github.com/emirpasic/gods/queues/priorityqueue"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
board := toBoard(lib.ReaderToStrings(input))
return board.bfs()
}
type Key struct {
visited int
position lib.Position
}
func toKey(position lib.Position, remainingKeys map[rune]bool) Key {
v := 0
for r := range remainingKeys {
v += 1 << int(r-'a')
}
return Key{
visited: v,
position: position,
}
}
type Key4 struct {
visited int
position0 lib.Position
position1 lib.Position
position2 lib.Position
position3 lib.Position
}
func toKeys(positions []lib.Position, remainingKeys map[rune]bool) Key4 {
v := 0
for r := range remainingKeys {
v += 1 << int(r-'a')
}
return Key4{
visited: v,
position0: positions[0],
position1: positions[1],
position2: positions[2],
position3: positions[3],
}
}
func (b *Board) bfs() int {
remainingKeys := make(map[rune]bool)
for _, r := range b.keys {
remainingKeys[r] = true
}
closedDoors := make(map[rune]bool)
for r := range b.doors {
closedDoors[r] = true
}
q := []Entry{
{
position: b.position,
remainingKeys: remainingKeys,
closedDoors: closedDoors,
visited: map[lib.Position]bool{b.position: true},
},
}
global := make(map[Key]int)
for len(q) != 0 {
e := q[0]
q = q[1:]
k := toKey(e.position, e.remainingKeys)
if distance, exists := global[k]; exists {
if distance <= e.distance {
continue
}
}
global[k] = e.distance
r := b.grid[e.position]
if isKey(r) {
if e.remainingKeys[r] {
delete(e.remainingKeys, r)
if len(e.remainingKeys) == 0 {
return e.distance
}
delete(e.closedDoors, unicode.ToUpper(r))
e.visited = map[lib.Position]bool{e.position: true}
}
} else if isDoor(r) {
if e.closedDoors[r] {
continue
}
}
pos := e.position.Delta(-1, 0)
if b.exists(pos) && !e.visited[pos] {
e := e.new(pos)
e.moveUntilIntersection(b, lib.Up)
q = append(q, e)
}
pos = e.position.Delta(1, 0)
if b.exists(pos) && !e.visited[pos] {
e := e.new(pos)
e.moveUntilIntersection(b, lib.Down)
q = append(q, e)
}
pos = e.position.Delta(0, -1)
if b.exists(pos) && !e.visited[pos] {
e := e.new(pos)
e.moveUntilIntersection(b, lib.Left)
q = append(q, e)
}
pos = e.position.Delta(0, 1)
if b.exists(pos) && !e.visited[pos] {
e := e.new(pos)
e.moveUntilIntersection(b, lib.Right)
q = append(q, e)
}
}
return -1
}
func (b *Board) best() int {
remainingKeys := make(map[rune]bool)
for _, r := range b.keys {
remainingKeys[r] = true
}
closedDoors := make(map[rune]bool)
for r := range b.doors {
closedDoors[r] = true
}
q := pq.NewWith(func(a, b interface{}) int {
priorityA := a.(Entry4).distance
priorityB := b.(Entry4).distance
return priorityA - priorityB
})
q.Enqueue(Entry4{
positions: b.positions,
remainingKeys: remainingKeys,
closedDoors: closedDoors,
})
global := make(map[Key4]int)
found := false
best := lib.NewMiner()
for !q.Empty() {
x, _ := q.Dequeue()
e := x.(Entry4)
if e.distance >= best.Get() {
continue
}
k := toKeys(e.positions, e.remainingKeys)
if distance, exists := global[k]; exists {
if distance <= e.distance {
continue
}
}
global[k] = e.distance
for i := 0; i < len(b.positions); i++ {
keys := b.accessibleKeys(e.closedDoors, e.positions[i])
for r, keyloc := range keys {
if !e.remainingKeys[r] {
continue
}
e := e.new(i, keyloc.pos, keyloc.distance)
delete(e.remainingKeys, r)
if len(e.remainingKeys) == 0 {
// As we use a priority queue, we shouldn't return directly but instead exhaust the queue
best.Add(e.distance)
found = true
continue
}
delete(e.closedDoors, unicode.ToUpper(r))
q.Enqueue(e)
}
}
}
if found {
return best.Get()
}
return -1
}
type KeyLocation struct {
distance int
pos lib.Position
}
func (b *Board) accessibleKeys(closedDoor map[rune]bool, position lib.Position) map[rune]KeyLocation {
type State struct {
distance int
position lib.Position
}
q := []State{{position: position}}
visited := make(map[lib.Position]int)
keys := make(map[rune]KeyLocation)
for len(q) != 0 {
s := q[0]
q = q[1:]
if v, exists := visited[s.position]; exists {
if v <= s.distance {
continue
}
}
visited[s.position] = s.distance
r := b.grid[s.position]
if isKey(r) {
keys[r] = KeyLocation{s.distance, s.position}
}
pos := s.position.Delta(-1, 0)
if r, exists := b.grid[pos]; exists {
if !isDoor(r) {
q = append(q, State{s.distance + 1, pos})
} else if door, exists := b.doorsPosition[pos]; exists {
if !closedDoor[door] {
q = append(q, State{s.distance + 1, pos})
}
}
}
pos = s.position.Delta(1, 0)
if r, exists := b.grid[pos]; exists {
if !isDoor(r) {
q = append(q, State{s.distance + 1, pos})
} else if door, exists := b.doorsPosition[pos]; exists {
if !closedDoor[door] {
q = append(q, State{s.distance + 1, pos})
}
}
}
pos = s.position.Delta(0, -1)
if r, exists := b.grid[pos]; exists {
if !isDoor(r) {
q = append(q, State{s.distance + 1, pos})
} else if door, exists := b.doorsPosition[pos]; exists {
if !closedDoor[door] {
q = append(q, State{s.distance + 1, pos})
}
}
}
pos = s.position.Delta(0, 1)
if r, exists := b.grid[pos]; exists {
if !isDoor(r) {
q = append(q, State{s.distance + 1, pos})
} else if door, exists := b.doorsPosition[pos]; exists {
if !closedDoor[door] {
q = append(q, State{s.distance + 1, pos})
}
}
}
}
return keys
}
func (b *Board) exists(pos lib.Position) bool {
_, exists := b.grid[pos]
return exists
}
type Entry struct {
position lib.Position
remainingKeys map[rune]bool
closedDoors map[rune]bool
distance int
visited map[lib.Position]bool
}
type Entry4 struct {
positions []lib.Position
remainingKeys map[rune]bool
closedDoors map[rune]bool
distance int
}
func (e *Entry) moveUntilIntersection(b *Board, dir lib.Direction) {
for {
if e.visited[e.position] {
break
}
e.visited[e.position] = true
if b.isIntersection(e.position) {
break
}
r := b.grid[e.position]
if isKey(r) || isDoor(r) {
break
}
pos := e.position.Move(dir, 1)
_, exists := b.grid[pos]
if !exists {
break
}
e.position = pos
e.distance++
}
}
func (b *Board) isIntersection(pos lib.Position) bool {
sum := 0
if b.exists(pos.Delta(-1, 0)) {
sum++
}
if b.exists(pos.Delta(1, 0)) {
sum++
}
if b.exists(pos.Delta(0, -1)) {
sum++
}
if b.exists(pos.Delta(0, 1)) {
sum++
}
return sum > 2
}
func (e Entry4) new(i int, pos lib.Position, distance int) Entry4 {
remainingKeys := make(map[rune]bool, len(e.remainingKeys))
closedDoors := make(map[rune]bool, len(e.closedDoors))
for k, v := range e.remainingKeys {
remainingKeys[k] = v
}
for k, v := range e.closedDoors {
closedDoors[k] = v
}
positions := make([]lib.Position, len(e.positions))
for i, pos := range e.positions {
positions[i] = pos
}
positions[i] = pos
return Entry4{
positions: positions,
remainingKeys: remainingKeys,
closedDoors: closedDoors,
distance: e.distance + distance,
}
}
func (e Entry) new(pos lib.Position) Entry {
remainingKeys := make(map[rune]bool, len(e.remainingKeys))
closedDoors := make(map[rune]bool, len(e.closedDoors))
visited := make(map[lib.Position]bool, len(e.visited))
for k, v := range e.remainingKeys {
remainingKeys[k] = v
}
for k, v := range e.closedDoors {
closedDoors[k] = v
}
for k, v := range e.visited {
visited[k] = v
}
return Entry{
position: pos,
remainingKeys: remainingKeys,
closedDoors: closedDoors,
visited: visited,
distance: e.distance + 1,
}
}
type Board struct {
grid map[lib.Position]rune
doors map[rune]lib.Position
doorsPosition map[lib.Position]rune
keys map[lib.Position]rune
position lib.Position
positions []lib.Position
maxKey int
}
func toBoard(lines []string) *Board {
grid := make(map[lib.Position]rune)
doors := make(map[rune]lib.Position)
keys := make(map[lib.Position]rune)
row := -1
board := &Board{}
for _, line := range lines {
line = strings.TrimSpace(line)
row++
for col := 0; col < len(line); col++ {
r := rune(line[col])
pos := lib.Position{row, col}
if r == '#' {
continue
} else if r == '.' {
grid[pos] = empty
} else if r == '@' {
board.position = pos
grid[pos] = empty
} else if isKey(r) {
grid[pos] = r
keys[pos] = r
board.maxKey = lib.Max(board.maxKey, int(r-'a'))
} else if isDoor(r) {
grid[pos] = r
doors[r] = pos
} else {
panic(r)
}
}
}
board.grid = grid
board.doors = doors
board.keys = keys
return board
}
func toBoard4(lines []string) *Board {
grid := make(map[lib.Position]rune)
doors := make(map[rune]lib.Position)
doorsPosition := make(map[lib.Position]rune)
keys := make(map[lib.Position]rune)
row := -1
board := &Board{
positions: make([]lib.Position, 4),
}
posID := 0
for _, line := range lines {
line = strings.TrimSpace(line)
row++
for col := 0; col < len(line); col++ {
r := rune(line[col])
pos := lib.Position{row, col}
if r == '#' {
continue
} else if r == '.' {
grid[pos] = empty
} else if r == '@' {
board.positions[posID] = pos
posID++
grid[pos] = empty
} else if isKey(r) {
grid[pos] = r
keys[pos] = r
board.maxKey = lib.Max(board.maxKey, int(r-'a'))
} else if isDoor(r) {
grid[pos] = r
doors[r] = pos
doorsPosition[pos] = r
} else {
panic(r)
}
}
}
board.grid = grid
board.doors = doors
board.keys = keys
board.doorsPosition = doorsPosition
return board
}
func isKey(r rune) bool {
return r >= 'a' && r <= 'z'
}
func isDoor(r rune) bool {
return r >= 'A' && r <= 'Z'
}
const (
empty rune = '.'
)
func fs2(input io.Reader) int {
board := toBoard4(lib.ReaderToStrings(input))
return board.best()
}