-
Notifications
You must be signed in to change notification settings - Fork 0
/
d20.go
216 lines (197 loc) · 3.99 KB
/
d20.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
package main
import (
"math"
"strconv"
"strings"
)
type d20grid struct {
end coords
area [][]bool
score int64
cache map[int64]int64
seats map[int64][]coords
path map[int64]int64
}
func (g *d20grid) 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 *d20grid) getIdx(c coords, dir int) int64 {
return c.y*int64(len(g.area[0])*4) + c.x*int64(4) + int64(dir)
}
func (g *d20grid) getIdxObs(c coords, dir, obs int) int64 {
return c.y*int64(len(g.area[0])*4*20) + c.x*int64(4*20) + int64(dir*20) + int64(obs)
}
func (g *d20grid) walk(now coords, dir int, score int64, path []coords) {
path = append(path, now)
g.cache[g.getIdx(now, 0)] = 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 && score > 0 {
continue
}
nd := (dir + i) % 4
n := g.getNext(now, nd)
if g.area[n.y][n.x] {
continue
}
ns := score + 1
paid, been := g.cache[g.getIdx(n, 0)]
if been && paid <= ns {
continue
}
g.walk(n, nd, ns, path)
}
}
func (g *d20grid) cheat(path []coords, skip int) (int, int64) {
for i, cur := range path {
if i == len(path)-1 {
continue
}
for d := 0; d < 4; d++ {
idx := i*4*2 + d*2 + 0
if idx > skip {
could := g.getNext(cur, d)
score, been := g.path[g.getIdx(could, 0)]
if been {
save := score - int64(i) - 1
if save > 0 {
return i*4*2 + d*2 + 0, save
}
}
}
idx++
if idx <= skip {
continue
}
could := g.getNext(g.getNext(cur, d), d)
score, been := g.path[g.getIdx(could, 0)]
if been {
save := score - int64(i) - 2
if save > 0 {
return i*4*2 + d*2 + 1, save
}
}
}
}
return -1, -1
}
func (*methods) D20P1(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 := &d20grid{
end: end,
area: area,
score: int64(math.MaxInt64),
cache: make(map[int64]int64),
seats: make(map[int64][]coords),
path: make(map[int64]int64),
}
g.walk(start, 0, 0, []coords{})
path := g.seats[g.score]
for i, c := range path {
g.path[g.getIdx(c, 0)] = int64(i)
}
skip := -1
var cnt int64
for {
pos, save := g.cheat(path, skip)
if pos < 0 {
break
}
skip = pos
if save >= 100 {
cnt++
}
}
return strconv.FormatInt(cnt, 10)
}
func (*methods) D20P2(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 := &d20grid{
end: end,
area: area,
score: int64(math.MaxInt64),
cache: make(map[int64]int64),
seats: make(map[int64][]coords),
}
g.walk(start, 0, 0, []coords{})
pt := g.seats[g.score]
var total int64
for i := 0; i < len(pt)-20; i++ {
for j := i + 20; j < len(pt); j++ {
start := pt[i]
end := pt[j]
// manhattan
dist := int(math.Abs(float64(end.x-start.x)) + math.Abs(float64(end.y-start.y)))
if dist <= 20 {
// as j and i are indexes of the points on the original path,
// they represent the original score at a point in time
savings := j - i - dist
if savings >= 100 {
total++
}
}
}
}
return strconv.FormatInt(total, 10)
}