generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
191 lines (160 loc) · 3.15 KB
/
solution.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
package day19p1
import (
"fmt"
"io"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
workflows := make(workflowMap)
partList := []part{}
startParts := false
for _, ln := range lines {
if ln == "" {
startParts = true
continue
}
if startParts {
p := parsePart(ln)
partList = append(partList, p)
} else {
name, instructions := parseWorkflow(ln)
workflows[name] = instructions
}
}
var sum int64
for _, p := range partList {
accepted := workflows.accept(p, "in")
if utils.Verbose {
fmt.Printf("%#v %t\n", p, accepted)
}
if accepted {
sum += p.x + p.m + p.a + p.s
}
}
return sum
}
type operation int
const (
none operation = iota
lessthan
greaterthan
)
type part struct {
x int64
m int64
a int64
s int64
}
func (p part) getAttribute(a string) int64 {
switch a {
case "x":
return p.x
case "m":
return p.m
case "a":
return p.a
case "s":
return p.s
default:
return 0
}
}
type rule struct {
op operation
attribute string
argument int64
result string
}
func (r rule) evaluate(p part) string {
input := p.getAttribute(r.attribute)
switch r.op {
case none:
return r.result
case lessthan:
if input < r.argument {
return r.result
}
case greaterthan:
if input > r.argument {
return r.result
}
default:
panic("unknown operation")
}
return ""
}
type workflowMap map[string][]rule
// Check if part is accepted starting with workflow start
func (w workflowMap) accept(p part, start string) bool {
instructions := w[start]
for _, i := range instructions {
result := i.evaluate(p)
switch result {
case "A":
return true
case "R":
return false
case "":
// do nothing and move on
default:
return w.accept(p, result)
}
}
panic("Finished workflow with no result")
}
func parseWorkflow(ln string) (string, []rule) {
rules := []rule{}
ln = strings.TrimSuffix(ln, "}")
parts := strings.Split(ln, "{")
name := parts[0]
ruleStatements := strings.Split(parts[1], ",")
for _, s := range ruleStatements {
ruleComponents := strings.Split(s, ":")
if len(ruleComponents) == 1 {
// This is a non rule
r := rule{none, "", 0, ruleComponents[0]}
rules = append(rules, r)
} else {
opRune := ruleComponents[0][1]
var op operation
switch opRune {
case '>':
op = greaterthan
case '<':
op = lessthan
default:
panic("unknown operation in parse")
}
arg, err := strconv.ParseInt(ruleComponents[0][2:], 10, 64)
utils.Check(err, "Unable to parse %s to integer", ruleComponents[0][2:])
r := rule{op, ruleComponents[0][:1], arg, ruleComponents[1]}
rules = append(rules, r)
}
}
return name, rules
}
func parsePart(ln string) part {
ln = strings.TrimSuffix(ln, "}")
ln = strings.TrimPrefix(ln, "{")
parts := strings.Split(ln, ",")
ret := part{}
for _, p := range parts {
sides := strings.Split(p, "=")
value, err := strconv.ParseInt(sides[1], 10, 64)
utils.Check(err, "unable to parse %s to integer", sides[1])
switch sides[0] {
case "x":
ret.x = value
case "m":
ret.m = value
case "a":
ret.a = value
case "s":
ret.s = value
}
}
return ret
}