|
| 1 | +package day04 |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +func Part1(input string) uint { |
| 10 | + passports := strings.Split(input, "\n\n") |
| 11 | + valid := uint(0) |
| 12 | + for _, passport := range passports { |
| 13 | + // easier to remove newlines than deal with multiline regexp :P |
| 14 | + passport = strings.ReplaceAll(passport, "\n", " ") |
| 15 | + current := map[string]bool{} |
| 16 | + re := regexp.MustCompile(`(...):([^ ]+)`) |
| 17 | + matches := re.FindAllStringSubmatch(passport, -1) |
| 18 | + for _, match := range matches { |
| 19 | + if match[1] == "cid" { |
| 20 | + continue |
| 21 | + } |
| 22 | + current[match[1]] = true |
| 23 | + } |
| 24 | + if len(current) == 7 { |
| 25 | + valid++ |
| 26 | + } |
| 27 | + } |
| 28 | + return valid |
| 29 | +} |
| 30 | + |
| 31 | +func Part2(input string) uint { |
| 32 | + passports := strings.Split(input, "\n\n") |
| 33 | + valid := uint(0) |
| 34 | + for _, passport := range passports { |
| 35 | + // easier to remove newlines than deal with multiline regexp :P |
| 36 | + passport = strings.ReplaceAll(passport, "\n", " ") |
| 37 | + current := map[string]bool{} |
| 38 | + re := regexp.MustCompile(`(...):([^ ]+)`) |
| 39 | + matches := re.FindAllStringSubmatch(passport, -1) |
| 40 | + for _, match := range matches { |
| 41 | + key := match[1] |
| 42 | + val := match[2] |
| 43 | + if key == "cid" { |
| 44 | + continue |
| 45 | + } |
| 46 | + if key == "byr" { |
| 47 | + t, err := strconv.Atoi(val) |
| 48 | + if err != nil || t < 1920 || t > 2002 { |
| 49 | + continue |
| 50 | + } |
| 51 | + } |
| 52 | + if key == "iyr" { |
| 53 | + t, err := strconv.Atoi(val) |
| 54 | + if err != nil || t < 2010 || t > 2020 { |
| 55 | + continue |
| 56 | + } |
| 57 | + } |
| 58 | + if key == "eyr" { |
| 59 | + t, err := strconv.Atoi(val) |
| 60 | + if err != nil || t < 2020 || t > 2030 { |
| 61 | + continue |
| 62 | + } |
| 63 | + } |
| 64 | + if key == "hgt" { |
| 65 | + if strings.HasSuffix(val, "cm") { |
| 66 | + t, err := strconv.Atoi(val[:len(val)-2]) |
| 67 | + if err != nil || t < 150 || t > 193 { |
| 68 | + continue |
| 69 | + } |
| 70 | + } else if strings.HasSuffix(val, "in") { |
| 71 | + t, err := strconv.Atoi(val[:len(val)-2]) |
| 72 | + if err != nil || t < 59 || t > 76 { |
| 73 | + continue |
| 74 | + } |
| 75 | + } else { |
| 76 | + continue |
| 77 | + } |
| 78 | + } |
| 79 | + if key == "hcl" { |
| 80 | + re2 := regexp.MustCompile(`^#[0-9a-f]{6}$`) |
| 81 | + if !re2.MatchString(val) { |
| 82 | + continue |
| 83 | + } |
| 84 | + } |
| 85 | + if key == "ecl" { |
| 86 | + switch val { |
| 87 | + case "amb", |
| 88 | + "blu", |
| 89 | + "brn", |
| 90 | + "gry", |
| 91 | + "grn", |
| 92 | + "hzl", |
| 93 | + "oth": |
| 94 | + default: |
| 95 | + continue |
| 96 | + } |
| 97 | + } |
| 98 | + if key == "pid" { |
| 99 | + re2 := regexp.MustCompile(`^[0-9]{9}$`) |
| 100 | + if !re2.MatchString(val) { |
| 101 | + continue |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + current[key] = true |
| 106 | + } |
| 107 | + if len(current) == 7 { |
| 108 | + valid++ |
| 109 | + } |
| 110 | + } |
| 111 | + return valid |
| 112 | +} |
0 commit comments