Last active
January 24, 2021 15:30
-
-
Save ohbarye/a9dbe35b086f49abd86884d198661c04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"strings" | |
) | |
func main() { | |
bytes, _ := ioutil.ReadFile("./input.txt") | |
input := strings.TrimRight(string(bytes), "\n") | |
m := map[string]bool{ | |
"byr": false, | |
"iyr": false, | |
"eyr": false, | |
"hgt": false, | |
"hcl": false, | |
"ecl": false, | |
"pid": false, | |
"cid": false, | |
} | |
validCount := 0 | |
for _, row := range strings.Split(input, "\n\n") { | |
for _, f := range strings.Fields(row) { | |
a := strings.Split(f, ":")[0] | |
m[a] = true | |
} | |
allTrue := true | |
for k, v := range m { | |
if !v && k != "cid" { | |
allTrue = false | |
} | |
m[k] = false | |
} | |
if allTrue { | |
validCount++ | |
} | |
} | |
println(validCount) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
bytes, _ := ioutil.ReadFile("./input.txt") | |
input := strings.TrimRight(string(bytes), "\n") | |
m := map[string]bool{ | |
"byr": false, | |
"iyr": false, | |
"eyr": false, | |
"hgt": false, | |
"hcl": false, | |
"ecl": false, | |
"pid": false, | |
"cid": false, | |
} | |
validCount := 0 | |
for _, row := range strings.Split(input, "\n\n") { | |
println("================================") | |
for _, f := range strings.Fields(row) { | |
kv := strings.Split(f, ":") | |
println(kv[0], kv[1], validate(kv[0], kv[1])) | |
m[kv[0]] = validate(kv[0], kv[1]) | |
} | |
allTrue := true | |
for k, v := range m { | |
if !v && k != "cid" { | |
allTrue = false | |
} | |
m[k] = false | |
} | |
if allTrue { | |
validCount++ | |
} | |
println("allTrue", allTrue) | |
} | |
println(validCount) | |
} | |
func validate(key, value string) bool { | |
switch key { | |
case "byr": | |
byr, err := strconv.Atoi(value) | |
return err == nil && byr >= 1920 && byr <= 2002 && len(value) == 4 | |
case "iyr": | |
iyr, err := strconv.Atoi(value) | |
return err == nil && iyr >= 2010 && iyr <= 2020 && len(value) == 4 | |
case "eyr": | |
eyr, err := strconv.Atoi(value) | |
return err == nil && eyr >= 2020 && eyr <= 2030 && len(value) == 4 | |
case "hgt": | |
if strings.HasSuffix(value, "cm") { | |
hgt, err := strconv.Atoi(strings.TrimRight(value, "cm")) | |
return err == nil && hgt >= 150 && hgt <= 193 | |
} else if strings.HasSuffix(value, "in") { | |
hgt, err := strconv.Atoi(strings.TrimRight(value, "in")) | |
return err == nil && hgt >= 59 && hgt <= 76 | |
} else { | |
return false | |
} | |
case "hcl": | |
matched, err := regexp.MatchString(`^#[0-9a-f]{6}$`, value) | |
return err == nil && matched | |
case "ecl": | |
options := map[string]struct{}{ | |
"amb": {}, | |
"blu": {}, | |
"brn": {}, | |
"gry": {}, | |
"grn": {}, | |
"hzl": {}, | |
"oth": {}, | |
} | |
_, ok := options[value] | |
return ok | |
case "pid": | |
_, err := strconv.Atoi(value) | |
return err == nil && len(value) == 9 | |
default: | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment