2020 Day 4 Complete

This commit is contained in:
Brian Buller 2020-12-04 08:40:28 -06:00
parent 37a935d038
commit c61be8a956
3 changed files with 1275 additions and 0 deletions

1138
2020/day04/input Normal file

File diff suppressed because it is too large Load Diff

124
2020/day04/main.go Normal file
View File

@ -0,0 +1,124 @@
package main
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
var passports []*Passport
var p *Passport
p, inp = GetFirstPassport(inp)
for p != nil {
passports = append(passports, p)
p, inp = GetFirstPassport(inp)
}
var valid int
for _, t := range passports {
if t.IsValid() {
valid++
}
}
fmt.Printf("%d Valid Passports\n", valid)
}
type Passport struct {
BirthYear string
IssueYear string
Expiration string
Height string
HairColor string
EyeColor string
PassportID string
CountryID string
}
func GetFirstPassport(input []string) (*Passport, []string) {
ret := Passport{}
var k int
for k = range input {
if strings.TrimSpace(input[k]) == "" {
break
}
pts := strings.Split(input[k], " ")
for pk := range pts {
keyval := strings.Split(pts[pk], ":")
if len(keyval) == 2 {
switch keyval[0] {
case "byr":
ret.BirthYear = keyval[1]
case "iyr":
ret.IssueYear = keyval[1]
case "eyr":
ret.Expiration = keyval[1]
case "hgt":
ret.Height = keyval[1]
case "hcl":
ret.HairColor = keyval[1]
case "ecl":
ret.EyeColor = keyval[1]
case "pid":
ret.PassportID = keyval[1]
case "cid":
ret.CountryID = keyval[1]
}
}
}
}
if k == 0 {
return nil, []string{}
}
return &ret, input[k+1:]
}
func (p *Passport) ValidBirthYear() bool {
wrk, err := strconv.Atoi(p.BirthYear)
return err == nil && wrk >= 1920 && wrk <= 2002
}
func (p *Passport) ValidIssueYear() bool {
wrk, err := strconv.Atoi(p.IssueYear)
return err == nil && wrk >= 2010 && wrk <= 2020
}
func (p *Passport) ValidExpiration() bool {
wrk, err := strconv.Atoi(p.Expiration)
return err == nil && wrk >= 2020 && wrk <= 2030
}
func (p *Passport) ValidHeight() bool {
if strings.Contains(p.Height, "in") {
wrk, err := strconv.Atoi(strings.TrimSuffix(p.Height, "in"))
return err == nil && wrk >= 59 && wrk <= 76
} else if strings.Contains(p.Height, "cm") {
wrk, err := strconv.Atoi(strings.TrimSuffix(p.Height, "cm"))
return err == nil && wrk >= 150 && wrk <= 193
}
return false
}
func (p *Passport) ValidHairColor() bool {
if !strings.HasPrefix(p.HairColor, "#") {
return false
}
_, err := hex.DecodeString(p.HairColor[1:])
return err == nil
}
func (p *Passport) ValidEyeColor() bool {
return p.EyeColor == "amb" || p.EyeColor == "blu" || p.EyeColor == "brn" || p.EyeColor == "gry" || p.EyeColor == "grn" || p.EyeColor == "hzl" || p.EyeColor == "oth"
}
func (p *Passport) ValidPassportID() bool {
_, err := strconv.Atoi(p.PassportID)
return err == nil && len(p.PassportID) == 9
}
func (p *Passport) IsValid() bool {
return p.ValidBirthYear() && p.ValidIssueYear() && p.ValidExpiration() && p.ValidHeight() && p.ValidHairColor() && p.ValidEyeColor() && p.ValidPassportID()
}

13
2020/day04/testinput Normal file
View File

@ -0,0 +1,13 @@
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in