-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
329 lines (289 loc) · 6 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
package main
import (
"io"
"strings"
aoc "github.com/teivah/advent-of-code"
)
const acceptedStr = "A"
const rejectedStr = "R"
const startingWorkflow = "in"
type Result int
const (
accepted Result = iota
rejected
sent
)
type Condition int
const (
alwaysTrue Condition = iota
smaller
greater
)
type Rating map[string]int
type Step struct {
f stepFunc
res Result
condVariable string
cond Condition
condValue int
next string
}
type stepFunc func(Rating) (bool, Result, string)
func fs1(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
workflows := parseWorkflows(groups[0])
ratings := parseRatings(groups[1])
res := 0
outer:
for _, rating := range ratings {
workflow := startingWorkflow
inner:
for {
steps := workflows[workflow]
for _, step := range steps {
matching, result, next := step.f(rating)
if !matching {
continue
}
switch result {
case rejected:
continue outer
case accepted:
res += rating["x"] + rating["m"] + rating["a"] + rating["s"]
continue outer
case sent:
workflow = next
continue inner
default:
panic(result)
}
}
}
}
return res
}
func parseWorkflows(lines []string) map[string][]Step {
m := make(map[string][]Step)
for _, line := range lines {
sep := strings.Index(line, "{")
workflowName := line[:sep]
var workflows []Step
s := line[sep+1 : len(line)-1]
del := aoc.NewDelimiter(s, ",")
for _, stepName := range del.GetStrings() {
step := parseWorkflowStep(stepName)
workflows = append(workflows, step)
}
m[workflowName] = workflows
}
return m
}
func parseWorkflowStep(s string) Step {
thenIdx := strings.Index(s, ":")
var res Result
var next string
if thenIdx == -1 {
switch s {
case acceptedStr:
res = accepted
case rejectedStr:
res = rejected
default:
res = sent
next = s
}
return Step{
f: func(_ Rating) (bool, Result, string) {
return true, res, next
},
res: res,
cond: alwaysTrue,
next: next,
}
}
beforeThen := s[:thenIdx]
next = s[thenIdx+1:]
operatorIdx := 0
var isSmaller bool
if v := strings.Index(beforeThen, "<"); v != -1 {
isSmaller = true
operatorIdx = v
} else if v := strings.Index(beforeThen, ">"); v != -1 {
operatorIdx = v
} else {
panic(s)
}
variable := beforeThen[:operatorIdx]
value := aoc.StringToInt(beforeThen[operatorIdx+1:])
switch next {
case acceptedStr:
res = accepted
case rejectedStr:
res = rejected
default:
res = sent
}
if isSmaller {
return Step{
f: func(r Rating) (bool, Result, string) {
if r[variable] < value {
return true, res, next
}
return false, res, ""
},
res: res,
condVariable: variable,
cond: smaller,
condValue: value,
next: next,
}
}
return Step{
f: func(r Rating) (bool, Result, string) {
if r[variable] > value {
return true, res, next
}
return false, 0, ""
},
res: res,
condVariable: variable,
cond: greater,
condValue: value,
next: next,
}
}
func parseRatings(lines []string) []Rating {
ratings := make([]Rating, 0, len(lines))
for _, line := range lines {
// Remove { and }
line = line[1 : len(line)-1]
del := aoc.NewDelimiter(line, ",")
items := del.GetStrings()
rating := make(map[string]int, len(items))
for _, s := range items {
name, value := parseRating(s)
rating[name] = value
}
ratings = append(ratings, rating)
}
return ratings
}
func parseRating(s string) (string, int) {
del := aoc.NewDelimiter(s, "=")
return del.GetString(0), del.GetInt(1)
}
func fs2(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
workflows := parseWorkflows(groups[0])
node := createTree(workflows, startingWorkflow)
defaultRange := Range{
from: 1,
to: 4000,
}
intervals := dfs(node, RangeRating{
"x": defaultRange,
"m": defaultRange,
"a": defaultRange,
"s": defaultRange,
})
sum := 0
for _, interval := range intervals {
v := 1
for _, r := range interval {
v *= r.to - r.from + 1
}
sum += v
}
return sum
}
type Node struct {
accepted bool
rejected bool
name string
children []*Node
steps []Step
}
type RangeRating map[string]Range
func (r RangeRating) clone() RangeRating {
res := make(map[string]Range, len(r))
for k, v := range r {
res[k] = v
}
return res
}
type Range struct {
from int
to int
}
func createTree(workflows map[string][]Step, workflowName string) *Node {
var children []*Node
var steps []Step
for _, step := range workflows[workflowName] {
switch step.res {
case accepted:
children = append(children, &Node{
accepted: true,
})
steps = append(steps, step)
continue
case rejected:
children = append(children, &Node{
rejected: true,
})
steps = append(steps, step)
continue
}
n := createTree(workflows, step.next)
if n != nil {
children = append(children, n)
steps = append(steps, step)
}
}
return &Node{
name: workflowName,
children: children,
steps: steps,
}
}
func dfs(node *Node, r RangeRating) []RangeRating {
if node.rejected {
return nil
}
if node.accepted {
for _, v := range r {
if v.to < v.from {
return nil
}
}
return []RangeRating{r}
}
parent := r.clone()
var res []RangeRating
for i, child := range node.children {
cur := parent.clone()
step := node.steps[i]
switch step.cond {
case alwaysTrue:
res = append(res, dfs(child, cur)...)
case smaller:
variable := step.condVariable
rr := cur[variable]
rr.to = min(rr.to, step.condValue-1)
cur[variable] = rr
res = append(res, dfs(child, cur)...)
rr = parent[variable]
rr.from = max(rr.from, step.condValue)
parent[variable] = rr
case greater:
variable := step.condVariable
rr := cur[variable]
rr.from = max(rr.from, step.condValue+1)
cur[variable] = rr
res = append(res, dfs(child, cur)...)
rr = parent[variable]
rr.to = min(rr.to, step.condValue)
parent[variable] = rr
}
}
return res
}