-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19-1.go
178 lines (161 loc) · 3.17 KB
/
day19-1.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// x, m, a, s is 0, 1, 2, 3.
type day19Part []int
type day19Cond int
const (
day19CondPass = iota
day19CondLess
day19CondMore
)
type day19Rule struct {
// Result workflow.
out string
cond day19Cond
// 0–3.
part int
// Comparison condition.
cmp int
}
type day19RuleSet map[string][]day19Rule
type day19System struct {
rules day19RuleSet
parts []day19Part
}
func day19part1(filename string) (string, error) {
f, err := day19ReadPlan(filename)
if err != nil {
return "", err
}
var acc []day19Part
for _, p := range f.parts {
if out := f.process(p); out == "A" {
acc = append(acc, p)
}
}
var total int
for _, v := range acc {
for _, vv := range v {
total += vv
}
}
return fmt.Sprint(total), nil
}
func (s day19System) process(p day19Part) string {
next := "in"
for next != "A" && next != "R" {
for _, r := range s.rules[next] {
if out, pass := r.result(p); pass {
next = out
break
}
}
}
return next
}
// Returns the result workflow and if the rule succeeded.
func (r day19Rule) result(p day19Part) (string, bool) {
res := false
switch r.cond {
case day19CondPass:
res = true
case day19CondLess:
res = p[r.part] < r.cmp
case day19CondMore:
res = p[r.part] > r.cmp
}
return r.out, res
}
var (
day19RuleRegex = regexp.MustCompile(`([[:alpha:]]+){(.*)}`)
day19OneRuleRegex = regexp.MustCompile(`([xmas])([<>])(\d+):([[:alpha:]]+)`)
day19PartRegex = regexp.MustCompile(`{x=(\d+),m=(\d+),a=(\d+),s=(\d+)}`)
)
func day19ReadPlan(filename string) (day19System, error) {
var rulesDone bool
res := day19System{
rules: day19RuleSet{},
parts: []day19Part{},
}
if err := forLineError(filename, func(line string) error {
if len(line) == 0 {
rulesDone = true
return nil
}
if !rulesDone {
m := day19RuleRegex.FindStringSubmatch(line)
rules, err := day19ReadRules(m[2])
if err != nil {
return err
}
res.rules[m[1]] = rules
return nil
}
p, err := day19ReadPart(line)
if err != nil {
return err
}
res.parts = append(res.parts, p)
return nil
}); err != nil {
return res, err
}
return res, nil
}
func day19ReadPart(s string) (day19Part, error) {
var p day19Part
m := day19PartRegex.FindStringSubmatch(s)
if len(m) != 5 {
return p, fmt.Errorf("bad part: %s", s)
}
for i := 1; i < 5; i++ {
num, err := strconv.Atoi(m[i])
if err != nil {
return p, err
}
p = append(p, num)
}
return p, nil
}
func day19ReadRules(r string) ([]day19Rule, error) {
var res []day19Rule
for _, v := range strings.Split(r, ",") {
rule, err := day19MakeRule(v)
if err != nil {
return nil, err
}
res = append(res, rule)
}
return res, nil
}
var day19PartMap = map[string]int{
"x": 0,
"m": 1,
"a": 2,
"s": 3,
}
var day19CondMap = map[string]day19Cond{
"<": day19CondLess,
">": day19CondMore,
}
func day19MakeRule(s string) (day19Rule, error) {
var r day19Rule
if spl := day19OneRuleRegex.FindStringSubmatch(s); len(spl) == 5 {
r.out = spl[4]
r.cond = day19CondMap[spl[2]]
r.part = day19PartMap[spl[1]]
n, err := strconv.Atoi(spl[3])
if err != nil {
return r, err
}
r.cmp = n
} else {
r.out = s
}
return r, nil
}