-
Notifications
You must be signed in to change notification settings - Fork 0
/
d16.go
150 lines (135 loc) · 2.62 KB
/
d16.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
package main
import (
"math"
"strconv"
"strings"
)
type d16grid struct {
start coords
end coords
area [][]bool
score int64
cache map[int64]int64
seats map[int64][]coords
}
func (g *d16grid) getNext(now coords, dir int) coords {
next := now
switch dir {
case 0: // N
next.y--
case 1: // E
next.x++
case 2: // S
next.y++
case 3: // W
next.x--
}
return next
}
func (g *d16grid) getIdx(c coords, dir int) int64 {
return c.y*int64(len(g.area[0])*4) + c.x*int64(4) + int64(dir)
}
func (g *d16grid) walk(now coords, dir int, score int64, path []coords) {
path = append(path, now)
g.cache[g.getIdx(now, dir)] = score
if now.x == g.end.x && now.y == g.end.y {
if score <= g.score {
g.score = score
seats := g.seats[score]
seats = append(seats, path...)
g.seats[score] = seats
}
return
}
for i := 0; i < 4; i++ {
if i == 2 {
continue
}
nd := (dir + i) % 4
n := g.getNext(now, nd)
if g.area[n.y][n.x] {
continue
}
ns := score + 1
if nd != dir {
ns += 1000
}
paid, been := g.cache[g.getIdx(n, dir)]
if been && paid < ns {
continue
}
g.walk(n, nd, ns, path)
}
}
func (*methods) D16P1(input string) string {
lines := strings.Split(input, "\n")
var start, end coords
var area [][]bool
for y, l := range lines {
var row []bool
for x, s := range l {
if s == 'S' {
start.x = int64(x)
start.y = int64(y)
row = append(row, false)
continue
}
if s == 'E' {
end.x = int64(x)
end.y = int64(y)
row = append(row, false)
continue
}
row = append(row, s == '#')
}
area = append(area, row)
}
g := &d16grid{
start: start,
end: end,
area: area,
score: int64(math.MaxInt64),
cache: make(map[int64]int64),
seats: make(map[int64][]coords),
}
g.walk(start, 1, 0, []coords{})
return strconv.FormatInt(g.score, 10)
}
func (*methods) D16P2(input string) string {
lines := strings.Split(input, "\n")
var start, end coords
var area [][]bool
for y, l := range lines {
var row []bool
for x, s := range l {
if s == 'S' {
start.x = int64(x)
start.y = int64(y)
row = append(row, false)
continue
}
if s == 'E' {
end.x = int64(x)
end.y = int64(y)
row = append(row, false)
continue
}
row = append(row, s == '#')
}
area = append(area, row)
}
g := &d16grid{
start: start,
end: end,
area: area,
score: int64(math.MaxInt64),
cache: make(map[int64]int64),
seats: make(map[int64][]coords),
}
g.walk(start, 1, 0, []coords{})
best := make(map[int64]bool)
for _, s := range g.seats[g.score] {
best[g.getIdx(s, 0)] = true
}
return strconv.FormatInt(int64(len(best)), 10)
}