-
Notifications
You must be signed in to change notification settings - Fork 0
/
19a.go
90 lines (80 loc) · 2.14 KB
/
19a.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
// go run 19a.go
package main
import (
"fmt"
"strconv"
"strings"
)
type Rule struct {
category string
checker func(int) bool
verdict string
}
func parseRule(text string) Rule {
colon := strings.Index(text, ":")
if colon == -1 {
return Rule{"s", func(int) bool { return true }, text}
}
category := text[0:1]
verdict := text[colon + 1:]
bound, _ := strconv.Atoi(text[2:colon])
if text[1] == '>' {
return Rule{category, func(value int) bool { return value > bound }, verdict}
} else {
return Rule{category, func(value int) bool { return value < bound }, verdict}
}
}
func solve(data []string, queries []string) int {
workflows := map[string][]Rule{}
for _, line := range data {
brace := strings.Index(line, "{")
rules := []Rule{}
for _, text := range strings.Split(line[brace + 1 : (len(line) - 1)], ",") {
rules = append(rules, parseRule(text))
}
workflows[line[0:brace]] = rules
}
result := 0
for _, query := range queries {
part := map[string]int{}
for _, rating := range strings.Split(query[1 : len(query) - 1], ",") {
number, _ := strconv.Atoi(rating[2:])
part[rating[0:1]] = number
}
cur := "in"
for {
for _, rule := range workflows[cur] {
if rule.checker(part[rule.category]) {
cur = rule.verdict
break
}
}
if cur == "A" {
sum := 0
for _, value := range part {
sum += value
}
result += sum
break
}
if cur == "R" { break }
}
}
return result
}
func main() {
var data = []string{}
var queries = []string{}
var line string
for {
_, err := fmt.Scanln(&line)
if err != nil { break }
data = append(data, line)
}
for {
_, err := fmt.Scanln(&line)
if err != nil { break }
queries = append(queries, line)
}
fmt.Println(solve(data, queries))
}