-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (150 loc) · 3.65 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
package main
import (
"fmt"
"github.com/davidporos92/aoc-2020/utils"
"log"
"regexp"
"strconv"
"strings"
)
type Passport struct {
byr int
iyr int
eyr int
hgt string
hcl string
ecl string
pid string
cid int
}
func NewPassport(passport string) *Passport {
p := &Passport{}
byr := regexp.MustCompile(`byr:([^\s]+)`).FindStringSubmatch(passport)
if len(byr) > 1 {
intVal, err := strconv.Atoi(byr[1])
if err != nil {
log.Fatalf("Cannot convert byr to int: %v", err)
}
p.byr = intVal
}
iyr := regexp.MustCompile(`iyr:([^\s]+)`).FindStringSubmatch(passport)
if len(iyr) > 1 {
intVal, err := strconv.Atoi(iyr[1])
if err != nil {
log.Fatalf("Cannot convert iyr to int: %v", err)
}
p.iyr = intVal
}
eyr := regexp.MustCompile(`eyr:([^\s]+)`).FindStringSubmatch(passport)
if len(eyr) > 1 {
intVal, err := strconv.Atoi(eyr[1])
if err != nil {
log.Fatalf("Cannot convert eyr to int: %v", err)
}
p.eyr = intVal
}
hgt := regexp.MustCompile(`hgt:([^\s]+)`).FindStringSubmatch(passport)
if len(hgt) > 1 {
p.hgt = hgt[1]
}
hcl := regexp.MustCompile(`hcl:([^\s]+)`).FindStringSubmatch(passport)
if len(hcl) > 1 {
p.hcl = hcl[1]
}
ecl := regexp.MustCompile(`ecl:([^\s]+)`).FindStringSubmatch(passport)
if len(ecl) > 1 {
p.ecl = ecl[1]
}
pid := regexp.MustCompile(`pid:([^\s]+)`).FindStringSubmatch(passport)
if len(pid) > 1 {
p.pid = pid[1]
}
cid := regexp.MustCompile(`cid:([^\s]+)`).FindStringSubmatch(passport)
if len(cid) > 1 {
intVal, err := strconv.Atoi(cid[1])
if err != nil {
log.Fatalf("Cannot convert cid to int: %v", err)
}
p.cid = intVal
}
return p
}
func (p *Passport) IsValidSolution1() bool {
return p.byr != 0 &&
p.iyr != 0 &&
p.eyr != 0 &&
p.hgt != "" &&
p.hcl != "" &&
p.ecl != "" &&
p.pid != ""
}
func (p *Passport) IsValidSolution2() bool {
return p.IsValidSolution1() && p.IsByrValid() &&
p.IsIyrValid() &&
p.IsEyrValid() &&
p.IsHgtValid() &&
p.IsHclValid() &&
p.IsEclValid() &&
p.IsPidValid()
}
func (p *Passport) IsByrValid() bool {
return p.byr >= 1920 && p.byr <= 2002
}
func (p *Passport) IsIyrValid() bool {
return p.iyr >= 2010 && p.iyr <= 2020
}
func (p *Passport) IsEyrValid() bool {
return p.eyr >= 2020 && p.eyr <= 2030
}
func (p *Passport) IsHgtValid() bool {
height, suffix := p.ParseHeight()
if suffix == "cm" {
return height >= 150 && height <= 193
}
return height >= 59 && height <= 76
}
func (p *Passport) IsHclValid() bool {
return len(p.hcl) == 7 && regexp.MustCompile(`(?i)#[0-9-a-f]{6}`).MatchString(p.hcl)
}
func (p *Passport) IsEclValid() bool {
switch p.ecl {
case "amb", "blu", "brn", "gry", "grn", "hzl", "oth":
return true
default:
return false
}
}
func (p *Passport) IsPidValid() bool {
return len(p.pid) == 9 && regexp.MustCompile(`(?i)[0-9]{9}`).MatchString(p.pid)
}
func (p *Passport) ParseHeight() (int, string) {
var suffix string
if strings.Contains(p.hgt, "cm") {
suffix = "cm"
} else {
suffix = "in"
}
intValue, err := strconv.Atoi(strings.TrimRight(p.hgt, suffix))
if err != nil {
log.Fatalf("Cannot convert hgt [%s] to int: %v", p.hgt, err)
}
return intValue, suffix
}
func main() {
var validCountSolution1 int
var validCountSolution2 int
passports := make([]*Passport, 0)
batches := utils.NewReader("./input-1.dat").MustReadStringBatchesFromFile(utils.BatchSeparatorBlankLine)
for _, batch := range batches {
passport := NewPassport(batch)
passports = append(passports, passport)
if passport.IsValidSolution1() {
validCountSolution1++
}
if passport.IsValidSolution2() {
validCountSolution2++
}
}
fmt.Printf("Solution 1: %d\n", validCountSolution1)
fmt.Printf("Solution 2: %d\n", validCountSolution2)
}