-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (166 loc) · 3.75 KB
/
main.go
File metadata and controls
209 lines (166 loc) · 3.75 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "4", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func parse(r io.Reader) []map[string]string {
var passports []map[string]string
currentPassport := make(map[string]string)
finishPassport := func() {
passports = append(passports, currentPassport)
currentPassport = make(map[string]string)
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
row := scanner.Text()
if len(row) == 0 {
finishPassport()
continue
}
rawFields := strings.Split(row, " ")
for _, rawField := range rawFields {
rawFieldParts := strings.Split(rawField, ":")
currentPassport[rawFieldParts[0]] = rawFieldParts[1]
}
}
finishPassport()
if scanner.Err() != nil {
panic(scanner.Err())
}
return passports
}
type fieldValueValidator interface {
IsValid(value string) bool
}
type length struct {
length int
}
func (l length) IsValid(value string) bool {
return len(value) == l.length
}
type between struct {
min, max int
}
func (i between) IsValid(value string) bool {
intVal, err := strconv.Atoi(value)
if err != nil {
return false
}
return intVal >= i.min && intVal <= i.max
}
type conditional struct {
keyFunc func(value string) (string, string)
keyValidators map[string]fieldValueValidator
}
func (m conditional) IsValid(value string) bool {
key, newValue := m.keyFunc(value)
return m.keyValidators[key].IsValid(newValue)
}
type matches struct {
reg *regexp.Regexp
}
func (r matches) IsValid(value string) bool {
return r.reg.MatchString(value)
}
type oneOf struct {
values []string
}
func (o oneOf) IsValid(value string) bool {
for _, allowed := range o.values {
if allowed == value {
return true
}
}
return false
}
type alwaysValid struct{}
func (a alwaysValid) IsValid(_ string) bool {
return true
}
type both struct {
first, second fieldValueValidator
}
func (s both) IsValid(value string) bool {
return s.first.IsValid(value) && s.second.IsValid(value)
}
const (
BirthYear = "byr"
IssueYear = "iyr"
ExpirationYear = "eyr"
Height = "hgt"
HairColor = "hcl"
EyeColor = "ecl"
PassportID = "pid"
CountryID = "cid"
)
var validatorForField = map[string]fieldValueValidator{
BirthYear: both{
first: length{4},
second: between{min: 1920, max: 2002},
},
IssueYear: both{
first: length{4},
second: between{min: 2010, max: 2020},
},
ExpirationYear: both{
first: length{4},
second: between{min: 2020, max: 2030},
},
Height: both{
first: matches{regexp.MustCompile(`^\d+(cm|in)$`)},
second: conditional{
keyFunc: func(value string) (string, string) {
num, unit := value[:len(value)-2], value[len(value)-2:]
return unit, num
},
keyValidators: map[string]fieldValueValidator{
"cm": between{min: 150, max: 193},
"in": between{min: 59, max: 76},
},
},
},
HairColor: matches{regexp.MustCompile(`^#[0-9a-f]{6}$`)},
EyeColor: oneOf{[]string{"amb", "blu", "brn", "gry", "grn", "hzl", "oth"}},
PassportID: matches{regexp.MustCompile(`^\d{9}$`)},
CountryID: alwaysValid{},
}
func isValid(passport map[string]string) bool {
if !hasRequiredFields(passport) {
return false
}
for fieldName, fieldValue := range passport {
if !validatorForField[fieldName].IsValid(fieldValue) {
return false
}
}
return true
}
func hasRequiredFields(passport map[string]string) bool {
_, hasCid := passport[CountryID]
return len(passport) == 8 || (len(passport) == 7 && !hasCid)
}
func solve(passports []map[string]string) int {
valid := 0
for _, passport := range passports {
if isValid(passport) {
valid += 1
}
}
return valid
}
func main() {
fmt.Println(solve(parse(input())))
}