|
| 1 | +// [Day 16: Aunt Sue](https://adventofcode.com/2015/day/16) |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func solve(data []byte) (int, int) { |
| 16 | + lines := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 17 | + aunts := make(map[string]map[string]int) |
| 18 | + |
| 19 | + re := regexp.MustCompile(`Sue (\d+): (\w+): (\d+), (\w+): (\d+), (\w+): (\d+)`) |
| 20 | + |
| 21 | + for _, line := range lines { |
| 22 | + matches := re.FindStringSubmatch(line) |
| 23 | + if len(matches) != 8 { |
| 24 | + continue |
| 25 | + } |
| 26 | + |
| 27 | + sue := matches[1] |
| 28 | + aunt := make(map[string]int) |
| 29 | + |
| 30 | + val1, _ := strconv.Atoi(matches[3]) |
| 31 | + val2, _ := strconv.Atoi(matches[5]) |
| 32 | + val3, _ := strconv.Atoi(matches[7]) |
| 33 | + |
| 34 | + aunt[matches[2]] = val1 |
| 35 | + aunt[matches[4]] = val2 |
| 36 | + aunt[matches[6]] = val3 |
| 37 | + |
| 38 | + aunts[sue] = aunt |
| 39 | + } |
| 40 | + |
| 41 | + // Part 1 |
| 42 | + part1 := 0 |
| 43 | + for sue, aunt := range aunts { |
| 44 | + if getValue(aunt, "children", 3) != 3 { |
| 45 | + continue |
| 46 | + } |
| 47 | + if getValue(aunt, "cats", 7) != 7 { |
| 48 | + continue |
| 49 | + } |
| 50 | + if getValue(aunt, "samoyeds", 2) != 2 { |
| 51 | + continue |
| 52 | + } |
| 53 | + if getValue(aunt, "pomeranians", 3) != 3 { |
| 54 | + continue |
| 55 | + } |
| 56 | + if getValue(aunt, "akitas", 0) != 0 { |
| 57 | + continue |
| 58 | + } |
| 59 | + if getValue(aunt, "vizslas", 0) != 0 { |
| 60 | + continue |
| 61 | + } |
| 62 | + if getValue(aunt, "goldfish", 5) != 5 { |
| 63 | + continue |
| 64 | + } |
| 65 | + if getValue(aunt, "trees", 3) != 3 { |
| 66 | + continue |
| 67 | + } |
| 68 | + if getValue(aunt, "cars", 2) != 2 { |
| 69 | + continue |
| 70 | + } |
| 71 | + if getValue(aunt, "perfumes", 1) != 1 { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + part1, _ = strconv.Atoi(sue) |
| 76 | + break |
| 77 | + } |
| 78 | + |
| 79 | + // Part 2 |
| 80 | + part2 := 0 |
| 81 | + for sue, aunt := range aunts { |
| 82 | + if getValue(aunt, "children", 3) != 3 { |
| 83 | + continue |
| 84 | + } |
| 85 | + // cats should be greater than |
| 86 | + if getValue(aunt, "cats", 7+1) <= 7 { |
| 87 | + continue |
| 88 | + } |
| 89 | + if getValue(aunt, "samoyeds", 2) != 2 { |
| 90 | + continue |
| 91 | + } |
| 92 | + // pomeranians should be fewer than |
| 93 | + if getValue(aunt, "pomeranians", 3-1) >= 3 { |
| 94 | + continue |
| 95 | + } |
| 96 | + if getValue(aunt, "akitas", 0) != 0 { |
| 97 | + continue |
| 98 | + } |
| 99 | + if getValue(aunt, "vizslas", 0) != 0 { |
| 100 | + continue |
| 101 | + } |
| 102 | + // goldfish should be fewer than |
| 103 | + if getValue(aunt, "goldfish", 5-1) >= 5 { |
| 104 | + continue |
| 105 | + } |
| 106 | + // trees should be greater than |
| 107 | + if getValue(aunt, "trees", 3+1) <= 3 { |
| 108 | + continue |
| 109 | + } |
| 110 | + if getValue(aunt, "cars", 2) != 2 { |
| 111 | + continue |
| 112 | + } |
| 113 | + if getValue(aunt, "perfumes", 1) != 1 { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + part2, _ = strconv.Atoi(sue) |
| 118 | + break |
| 119 | + } |
| 120 | + |
| 121 | + return part1, part2 |
| 122 | +} |
| 123 | + |
| 124 | +func getValue(aunt map[string]int, key string, defaultValue int) int { |
| 125 | + if val, ok := aunt[key]; ok { |
| 126 | + return val |
| 127 | + } |
| 128 | + return defaultValue |
| 129 | +} |
| 130 | + |
| 131 | +func main() { |
| 132 | + filename := "input.txt" |
| 133 | + elapsed := false |
| 134 | + |
| 135 | + for i := 1; i < len(os.Args); i++ { |
| 136 | + arg := os.Args[i] |
| 137 | + if arg == "--elapsed" { |
| 138 | + elapsed = true |
| 139 | + } else if !strings.HasPrefix(arg, "-") { |
| 140 | + filename = arg |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + data, err := os.ReadFile(filename) |
| 145 | + if err != nil { |
| 146 | + log.Fatal(err) |
| 147 | + } |
| 148 | + |
| 149 | + if elapsed { |
| 150 | + start := time.Now() |
| 151 | + result1, result2 := solve(data) |
| 152 | + duration := time.Since(start) |
| 153 | + |
| 154 | + fmt.Println(result1) |
| 155 | + fmt.Println(result2) |
| 156 | + fmt.Printf("elapsed: %f ms\n", duration.Seconds()*1000.0) |
| 157 | + } else { |
| 158 | + result1, result2 := solve(data) |
| 159 | + |
| 160 | + fmt.Println(result1) |
| 161 | + fmt.Println(result2) |
| 162 | + } |
| 163 | +} |
0 commit comments