-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (145 loc) · 3.15 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
const (
valueRangesSeparator = " or "
nearbyTicketHeader = "nearby tickets:"
ruleSeparator = ":"
ticketSeparator = ","
valueRangeSeparator = "-"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "16", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func parse(r io.Reader) ([]rule, [][]int) {
var rules []rule
var nearbyTickets [][]int
parsingZone := 0
scanner := bufio.NewScanner(r)
for scanner.Scan() {
row := scanner.Text()
if len(row) == 0 {
parsingZone += 1
continue
}
switch parsingZone {
case 0: // rules
rules = append(rules, newRule(row))
case 1: // my ticket
// my ticket isn't needed
case 2: // nearby tickets
// discard header
if row == nearbyTicketHeader {
continue
}
nearbyTickets = append(nearbyTickets, newTicket(row))
default:
panic(parsingZone)
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return rules, nearbyTickets
}
type rule struct {
name string
valueRanges []valueRange
}
func newRule(raw string) rule {
parts := strings.Split(raw, ruleSeparator)
if len(parts) != 2 {
panic(raw)
}
rawRanges := strings.Split(strings.TrimSpace(parts[1]), valueRangesSeparator)
valueRanges := make([]valueRange, len(rawRanges))
for i, rawRange := range rawRanges {
valueRanges[i] = newRange(rawRange)
}
return rule{
name: parts[0],
valueRanges: valueRanges,
}
}
func (r rule) matches(value int) bool {
for _, valueRange := range r.valueRanges {
if valueRange.matches(value) {
return true
}
}
return false
}
type valueRange struct {
min, max int
}
func (v valueRange) matches(value int) bool {
return value >= v.min && value <= v.max
}
func newRange(raw string) valueRange {
parts := strings.Split(raw, valueRangeSeparator)
min, err := strconv.Atoi(parts[0])
if err != nil {
panic(err)
}
max, err := strconv.Atoi(parts[1])
if err != nil {
panic(err)
}
return valueRange{
min: min,
max: max,
}
}
func newTicket(raw string) []int {
parts := strings.Split(raw, ticketSeparator)
ticket := make([]int, len(parts))
for i, part := range parts {
intVal, err := strconv.Atoi(part)
if err != nil {
panic(err)
}
ticket[i] = intVal
}
return ticket
}
func valueMatchesAnyRule(value int, rules []rule) bool {
for _, rule := range rules {
if rule.matches(value) {
return true
}
}
return false
}
func findInvalidValues(ticket []int, rules []rule) []int {
var invalidValues []int
for _, value := range ticket {
if !valueMatchesAnyRule(value, rules) {
invalidValues = append(invalidValues, value)
}
}
return invalidValues
}
func solve(rules []rule, nearbyTickets [][]int) int {
errorRate := 0
for _, ticket := range nearbyTickets {
for _, invalidValue := range findInvalidValues(ticket, rules) {
errorRate += invalidValue
}
}
return errorRate
}
func main() {
fmt.Println(solve(parse(strings.NewReader("class: 1-3 or 5-7\nrow: 6-11 or 33-44\nseat: 13-40 or 45-50\n\nyour ticket:\n7,1,14\n\nnearby tickets:\n7,3,47\n40,4,50\n55,2,20\n38,6,12"))))
fmt.Println(solve(parse(input())))
}