[2020-12-04] Add solutions for day04
parent
d83ed5dd0b
commit
e550a85656
2
Makefile
2
Makefile
|
@ -12,6 +12,8 @@ day03:
|
|||
go run main.go day03 input
|
||||
|
||||
day04:
|
||||
go run main.go day04 input
|
||||
|
||||
day05:
|
||||
day06:
|
||||
day07:
|
||||
|
|
|
@ -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
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,33 @@
|
|||
package day04
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.elis.nu/etu/aoc2020/utils"
|
||||
)
|
||||
|
||||
var rows []map[string]string
|
||||
|
||||
func ParseFile(input string) {
|
||||
row := make(map[string]string)
|
||||
|
||||
// Parse file
|
||||
for _, line := range utils.GetLinesFromFile("day04/" + input + ".txt") {
|
||||
if line == "" {
|
||||
rows = append(rows, row)
|
||||
|
||||
row = make(map[string]string)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
for _, part := range strings.Split(line, " ") {
|
||||
parts := strings.Split(part, ":")
|
||||
|
||||
row[parts[0]] = parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
// Don't forget the last item
|
||||
rows = append(rows, row)
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package day04
|
||||
|
||||
import "log"
|
||||
|
||||
func Solve1() {
|
||||
counter := 0
|
||||
for _, row := range rows {
|
||||
if len(row) == 8 || len(row) == 7 && row["cid"] == "" {
|
||||
counter++
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("2020-12-04.01: Answer:", counter)
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package day04
|
||||
|
||||
import (
|
||||
"log"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func Solve2() {
|
||||
counter := 0
|
||||
|
||||
validations := map[string]string{
|
||||
// Validate Birth year (1920-2002)
|
||||
"byr": `^(19[2-8][0-9]|199[0-9]|200[0-2])$`,
|
||||
|
||||
// Validate Issue year (2010-2020)
|
||||
"iyr": `^(201[0-9]|2020)$`,
|
||||
|
||||
// Validate Expiration year (2020-2030)
|
||||
"eyr": `^(202[0-9]|2030)$`,
|
||||
|
||||
// Validate height (59-76in) or (150-193cm)
|
||||
"hgt": `^((59|6[0-9]|7[0-6])in|(1[5-8][0-9]|19[0-3])cm)$`,
|
||||
|
||||
// Validate Hair color
|
||||
"hcl": `^#[0-9a-f]{6}$`,
|
||||
|
||||
// Validate Eye color
|
||||
"ecl": `^(amb|blu|brn|gry|grn|hzl|oth)$`,
|
||||
|
||||
// Validate Passport ID
|
||||
"pid": `^[0-9]{9}$`,
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
valid := true
|
||||
|
||||
for key, reg := range validations {
|
||||
if regexp.MustCompile(reg).FindString(row[key]) == "" {
|
||||
valid = false
|
||||
}
|
||||
}
|
||||
|
||||
if valid {
|
||||
counter++
|
||||
}
|
||||
}
|
||||
|
||||
// Correct answer: 127
|
||||
log.Println("2020-12-04.02: Answer:", counter)
|
||||
}
|
4
main.go
4
main.go
|
@ -7,6 +7,7 @@ import (
|
|||
"git.elis.nu/etu/aoc2020/day01"
|
||||
"git.elis.nu/etu/aoc2020/day02"
|
||||
"git.elis.nu/etu/aoc2020/day03"
|
||||
"git.elis.nu/etu/aoc2020/day04"
|
||||
"git.elis.nu/etu/aoc2020/utils"
|
||||
)
|
||||
|
||||
|
@ -24,5 +25,8 @@ func main() {
|
|||
|
||||
case "day03":
|
||||
utils.Perf("2020-12-03", day03.ParseFile, day03.Solve1, day03.Solve2)
|
||||
|
||||
case "day04":
|
||||
utils.Perf("2020-12-04", day04.ParseFile, day04.Solve1, day04.Solve2)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue