-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.go
298 lines (269 loc) · 6.12 KB
/
day20.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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package day20 solves AoC 2018 day 20.
package day20
import (
"fmt"
"io"
"sort"
"strings"
"github.com/fis/aoc/glue"
"github.com/fis/aoc/util"
)
func init() {
glue.RegisterSolver(2018, 20, glue.LineSolver(solve))
}
func solve(lines []string) ([]string, error) {
if len(lines) != 1 {
return nil, fmt.Errorf("expected one line, got %d", len(lines))
}
ex, err := parseDirex(lines[0])
if err != nil {
return nil, err
}
l := make(layout)
ex.trace([]util.P{{0, 0}}, l)
part1, part2 := l.radius()
return glue.Ints(part1, part2), nil
}
type direction int
const (
north direction = iota
west
south
east
)
var dirV = [...]util.P{north: {0, -1}, west: {-1, 0}, south: {0, 1}, east: {1, 0}}
func (d direction) rev() direction { return (d + 2) % 4 }
func (d direction) move(p util.P) util.P { return p.Add(dirV[d]) }
// layout representation: a point will exist in the map iff a room exists, and the value of the entry
// will be a bitmap of directions, with the bit set iff there is a door to that direction from the room.
type layout map[util.P]uint
func (l layout) String() string {
sb := strings.Builder{}
l.Write(&sb)
return sb.String()
}
func (l layout) Write(w io.Writer) {
level := l.asLevel()
min, max := level.Bounds()
level.WriteRect(w, util.P{min.X - 1, min.Y - 1}, util.P{max.X + 1, max.Y + 1})
}
func (l layout) asLevel() (level *util.Level) {
level = util.ParseLevelString("X", '#')
for p, doors := range l {
pp := p.Scale(2)
level.Set(pp.X, pp.Y, '.')
for d := north; d <= east; d++ {
if (doors & (uint(1) << d)) != 0 {
dp := d.move(pp)
level.Set(dp.X, dp.Y, "-|"[d%2])
}
}
}
level.Set(0, 0, 'X')
return level
}
func (l layout) trace(from util.P, d direction) {
l[from] |= uint(1) << d
l[d.move(from)] |= uint(1) << d.rev()
}
func (l layout) radius() (r int, far int) {
type distP struct {
p util.P
d int
}
fringe, seen := []distP{{p: util.P{0, 0}, d: 0}}, map[util.P]struct{}{{0, 0}: {}}
for len(fringe) > 0 {
at := fringe[0]
fringe = fringe[1:]
seen[at.p] = struct{}{}
if at.d > r {
r = at.d
}
if at.d >= 1000 {
far++
}
for doors, d := l[at.p], north; d <= east; d++ {
if (doors & (uint(1) << d)) != 0 {
to := d.move(at.p)
if _, ok := seen[to]; !ok {
fringe = append(fringe, distP{p: to, d: at.d + 1})
}
}
}
}
return r, far
}
type direxOp int
const (
opLit direxOp = iota
opCat
opAlt
)
type direx struct {
op direxOp
lit []direction
sub []*direx
}
func (ex *direx) trace(from []util.P, l layout) (end []util.P) {
switch ex.op {
case opLit:
for _, at := range from {
for _, d := range ex.lit {
l.trace(at, d)
at = d.move(at)
}
end = append(end, at)
}
return end
case opCat:
end = from
for _, s := range ex.sub {
end = s.trace(end, l)
}
return end
case opAlt:
for _, s := range ex.sub {
end = append(end, s.trace(from, l)...)
}
return dedup(end)
}
return nil
}
func dedup(ps []util.P) []util.P {
if len(ps) == 0 {
return nil
}
sort.Slice(ps, func(i, j int) bool {
return ps[i].Y < ps[j].Y || (ps[i].Y == ps[j].Y && ps[i].X < ps[j].X)
})
at := 0
for i, p := range ps {
if at == 0 || ps[at-1].X != p.X || ps[at-1].Y != p.Y {
if at < i {
ps[at] = p
}
at++
}
}
return ps[:at]
}
func (ex *direx) String() string {
if ex.op == opLit {
return fmt.Sprintf("lit:%v", ex.lit)
}
sub := []string(nil)
for _, s := range ex.sub {
sub = append(sub, s.String())
}
switch ex.op {
case opCat:
return fmt.Sprintf("cat:%v", sub)
case opAlt:
return fmt.Sprintf("alt:%v", sub)
default:
return "bad"
}
}
func parseDirex(in string) (*direx, error) {
if !strings.HasPrefix(in, "^") || !strings.HasSuffix(in, "$") {
return nil, fmt.Errorf("expected ^...$")
}
ex, tail, err := parseDirexAlt(in[1 : len(in)-1])
if err != nil {
return nil, err
} else if tail != "" {
return nil, fmt.Errorf("junk after expression")
}
return ex, nil
}
func parseDirexAlt(in string) (ex *direx, tail string, err error) {
ex, in, err = parseDirexCat(in)
if err != nil {
return nil, "", err
}
if len(in) == 0 || in[0] != '|' {
return ex, in, nil
}
sub := []*direx{ex}
for len(in) > 0 && in[0] == '|' {
ex, in, err = parseDirexCat(in[1:])
if err != nil {
return nil, "", err
}
sub = append(sub, ex)
}
return &direx{op: opAlt, sub: sub}, in, nil
}
func parseDirexCat(in string) (ex *direx, tail string, err error) {
sub := []*direx(nil)
for {
ex, in, err = parseDirexAtom(in)
if err != nil {
return nil, "", err
} else if ex == nil {
break
}
sub = append(sub, ex)
}
switch len(sub) {
case 0:
return &direx{op: opLit}, in, nil
case 1:
return sub[0], in, nil
default:
return &direx{op: opCat, sub: sub}, in, nil
}
}
func parseDirexAtom(in string) (ex *direx, tail string, err error) {
switch {
case len(in) == 0 || in[0] == ')' || in[0] == '|':
return nil, in, nil
case isDir(in[0]):
lit := []direction(nil)
for len(in) > 0 && isDir(in[0]) {
lit = append(lit, asDir(in[0]))
in = in[1:]
}
return &direx{op: opLit, lit: lit}, in, nil
case in[0] == '(':
ex, in, err = parseDirexAlt(in[1:])
if err != nil {
return nil, "", err
}
if len(in) == 0 || in[0] != ')' {
return nil, "", fmt.Errorf("expected ), got %q", in)
}
return ex, in[1:], nil
default:
return nil, "", fmt.Errorf("expected direction or ( or ) or |")
}
}
func isDir(b byte) bool {
return b == 'N' || b == 'W' || b == 'S' || b == 'E'
}
func asDir(b byte) direction {
switch b {
case 'N':
return north
case 'W':
return west
case 'S':
return south
case 'E':
return east
}
return direction(-1)
}