-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.go
108 lines (86 loc) · 2.37 KB
/
day19.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
package day19
import (
"log"
"strconv"
"strings"
)
type Day struct{}
type ParsedInput struct {
Workflows map[string]Workflow
Parts []Part
}
func (d Day) GetInput(lines []string) interface{} {
workflows := make(map[string]Workflow)
parts := make([]Part, 0)
parsingParts := false
for _, line := range lines {
if !parsingParts {
if line == "" {
parsingParts = true
continue
}
lineParts := strings.Split(strings.Replace(line, "}", "", 1), "{")
rules := strings.Split(lineParts[1], ",")
workflow := Workflow{
Name: lineParts[0],
Rules: make([]Rule, len(rules)-1),
}
for i, sRule := range rules {
if strings.ContainsAny(sRule, "<>") {
lastParts := strings.Split(sRule[2:], ":")
sValue := lastParts[0]
value, err := strconv.Atoi(sValue)
if err != nil {
log.Fatalf("Error while parsing the integer '%s'", sValue)
}
workflow.Rules[i] = Rule{
Cat: Category(sRule[0]),
Op: Operation(sRule[1]),
Val: value,
Destination: lastParts[1],
}
} else { // Is the exception
workflow.Exception = sRule
}
}
workflows[workflow.Name] = workflow
} else {
categoryAndValues := strings.Split(line[1:len(line)-1], ",")
part := Part{Values: make(map[Category]int, 4)}
for _, catAndVal := range categoryAndValues {
sParts := strings.Split(catAndVal, "=")
sValue := sParts[1]
value, err := strconv.Atoi(sValue)
if err != nil {
log.Fatalf("Error while parsing the integer '%s'", sValue)
}
part.Values[Category(sParts[0][0])] = value
}
parts = append(parts, part)
}
}
workflows["A"] = Workflow{Name: "A"}
workflows["R"] = Workflow{Name: "R"}
return ParsedInput{Workflows: workflows, Parts: parts}
}
func (d Day) SolvePart1(parsedInputI interface{}) (finalSum int) {
parsedInput := parsedInputI.(ParsedInput)
workflows := parsedInput.Workflows
for _, part := range parsedInput.Parts {
if workflows["in"].parsePart(workflows, part) {
finalSum += part.sum()
}
}
return
}
func (d Day) SolvePart2(parsedInputI interface{}) int {
parsedInput := parsedInputI.(ParsedInput)
workflows := parsedInput.Workflows
ranges := map[Category]Range{
'x': {Min: 1, Max: 4000},
'm': {Min: 1, Max: 4000},
'a': {Min: 1, Max: 4000},
's': {Min: 1, Max: 4000},
}
return workflows["in"].getCombinations(workflows, ranges)
}