|
1 | 1 | package day16
|
2 | 2 |
|
3 |
| -import "fmt" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + STATE_RULES = iota |
| 12 | + STATE_YOUR_TICKET |
| 13 | + STATE_NEARBY_TICKETS |
| 14 | +) |
4 | 15 |
|
5 | 16 | func Run(lines []string) error {
|
6 |
| - return fmt.Errorf("not yet implemented") |
| 17 | + rules, yourTicket, nearbyTickets, err := ParseInput(lines) |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + |
| 22 | + fmt.Println("Rule count:", len(rules)) |
| 23 | + |
| 24 | + errorRate := 0 |
| 25 | + validTickets := []Ticket{} |
| 26 | + for _, ticket := range nearbyTickets { |
| 27 | + invalidValues := ticket.GetInvalidValues(rules) |
| 28 | + for _, invalidValue := range invalidValues { |
| 29 | + errorRate += invalidValue |
| 30 | + } |
| 31 | + |
| 32 | + if len(invalidValues) == 0 { |
| 33 | + validTickets = append(validTickets, ticket) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + fmt.Println("Part 1:", errorRate) |
| 38 | + |
| 39 | + mappings, err := FindRuleMappings(validTickets, rules) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + result := 1 |
| 44 | + for key, idx := range mappings { |
| 45 | + if strings.HasPrefix(key, "departure") { |
| 46 | + result *= yourTicket.Values[idx] |
| 47 | + } |
| 48 | + } |
| 49 | + fmt.Println("Part 2:", result) |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func ParseInput(input []string) (map[string]Rule, Ticket, []Ticket, error) { |
| 54 | + rules := make(map[string]Rule) |
| 55 | + var yourTicket Ticket |
| 56 | + var nearbyTickets []Ticket |
| 57 | + state := STATE_RULES |
| 58 | + |
| 59 | + for _, line := range input { |
| 60 | + switch state { |
| 61 | + case STATE_RULES: |
| 62 | + if len(line) == 0 { |
| 63 | + state = STATE_YOUR_TICKET |
| 64 | + } else { |
| 65 | + rule, err := ParseRule(line) |
| 66 | + if err != nil { |
| 67 | + return map[string]Rule{}, Ticket{}, []Ticket{}, err |
| 68 | + } |
| 69 | + rules[rule.name] = rule |
| 70 | + } |
| 71 | + case STATE_YOUR_TICKET: |
| 72 | + if len(line) == 0 { |
| 73 | + state = STATE_NEARBY_TICKETS |
| 74 | + } else if line != "your ticket:" { |
| 75 | + t, err := ParseTicket(line) |
| 76 | + if err != nil { |
| 77 | + return map[string]Rule{}, Ticket{}, []Ticket{}, err |
| 78 | + } |
| 79 | + yourTicket = t |
| 80 | + } |
| 81 | + case STATE_NEARBY_TICKETS: |
| 82 | + if line != "nearby tickets:" { |
| 83 | + t, err := ParseTicket(line) |
| 84 | + if err != nil { |
| 85 | + return map[string]Rule{}, Ticket{}, []Ticket{}, err |
| 86 | + } |
| 87 | + nearbyTickets = append(nearbyTickets, t) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return rules, yourTicket, nearbyTickets, nil |
| 92 | +} |
| 93 | + |
| 94 | +type Ticket struct { |
| 95 | + Values []int |
| 96 | +} |
| 97 | + |
| 98 | +func ParseTicket(line string) (Ticket, error) { |
| 99 | + splat := strings.Split(line, ",") |
| 100 | + vals := make([]int, 0, len(splat)) |
| 101 | + for _, s := range splat { |
| 102 | + v, err := strconv.Atoi(s) |
| 103 | + if err != nil { |
| 104 | + return Ticket{}, fmt.Errorf("invalid value '%s'", s) |
| 105 | + } |
| 106 | + vals = append(vals, v) |
| 107 | + } |
| 108 | + return Ticket{vals}, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (t *Ticket) GetInvalidValues(rules map[string]Rule) []int { |
| 112 | + invalid := []int{} |
| 113 | + for _, val := range t.Values { |
| 114 | + validForRule := false |
| 115 | + for _, r := range rules { |
| 116 | + if r.Matches(val) { |
| 117 | + validForRule = true |
| 118 | + } |
| 119 | + } |
| 120 | + if !validForRule { |
| 121 | + invalid = append(invalid, val) |
| 122 | + } |
| 123 | + } |
| 124 | + return invalid |
| 125 | +} |
| 126 | + |
| 127 | +func ValidRulesAtIndex(tickets []Ticket, index int, rules map[string]Rule) []string { |
| 128 | + validRules := []string{} |
| 129 | + for _, rule := range rules { |
| 130 | + allValid := true |
| 131 | + for _, ticket := range tickets { |
| 132 | + if !rule.Matches(ticket.Values[index]) { |
| 133 | + allValid = false |
| 134 | + break |
| 135 | + } |
| 136 | + } |
| 137 | + if allValid { |
| 138 | + validRules = append(validRules, rule.name) |
| 139 | + } |
| 140 | + } |
| 141 | + return validRules |
| 142 | +} |
| 143 | + |
| 144 | +func FindAllValidRules(tickets []Ticket, rules map[string]Rule) [][]string { |
| 145 | + validRules := [][]string{} |
| 146 | + for i := 0; i < len(tickets[0].Values); i++ { |
| 147 | + validRules = append(validRules, ValidRulesAtIndex(tickets, i, rules)) |
| 148 | + } |
| 149 | + return validRules |
| 150 | +} |
| 151 | + |
| 152 | +func FindRuleMappings(tickets []Ticket, rules map[string]Rule) (map[string]int, error) { |
| 153 | + validRulesByIndex := FindAllValidRules(tickets, rules) |
| 154 | + mappings := map[string]int{} |
| 155 | + for cycle := 0; len(mappings) < len(rules); cycle++ { |
| 156 | + promoted := false |
| 157 | + // Check for any columns with only one valid rule |
| 158 | + for idx, validRules := range validRulesByIndex { |
| 159 | + if len(validRules) == 1 { |
| 160 | + promoted = true |
| 161 | + fmt.Printf("Promoted: '%s' => %d\n", validRules[0], idx) |
| 162 | + mappings[validRules[0]] = idx |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if !promoted { |
| 167 | + return nil, fmt.Errorf("made no progress") |
| 168 | + } |
| 169 | + |
| 170 | + // Remove the now-mapped rule from the valid set |
| 171 | + newValidRulesByIndex := [][]string{} |
| 172 | + for _, validRules := range validRulesByIndex { |
| 173 | + newValidRules := []string{} |
| 174 | + for _, currentRule := range validRules { |
| 175 | + if _, ok := mappings[currentRule]; !ok { |
| 176 | + newValidRules = append(newValidRules, currentRule) |
| 177 | + } |
| 178 | + } |
| 179 | + newValidRulesByIndex = append(newValidRulesByIndex, newValidRules) |
| 180 | + } |
| 181 | + validRulesByIndex = newValidRulesByIndex |
| 182 | + } |
| 183 | + return mappings, nil |
| 184 | +} |
| 185 | + |
| 186 | +type Range struct { |
| 187 | + Min int |
| 188 | + Max int |
| 189 | +} |
| 190 | + |
| 191 | +func (r *Range) Includes(num int) bool { |
| 192 | + return num >= r.Min && num <= r.Max |
| 193 | +} |
| 194 | + |
| 195 | +func ParseRange(input string) (Range, error) { |
| 196 | + splat := strings.Split(input, "-") |
| 197 | + if len(splat) != 2 { |
| 198 | + return Range{}, fmt.Errorf("invalid range '%s'", input) |
| 199 | + } |
| 200 | + |
| 201 | + min, err := strconv.Atoi(splat[0]) |
| 202 | + if err != nil { |
| 203 | + return Range{}, fmt.Errorf("invalid minimum '%s'", splat[0]) |
| 204 | + } |
| 205 | + max, err := strconv.Atoi(splat[1]) |
| 206 | + if err != nil { |
| 207 | + return Range{}, fmt.Errorf("invalid maximum '%s'", splat[0]) |
| 208 | + } |
| 209 | + return Range{min, max}, nil |
| 210 | +} |
| 211 | + |
| 212 | +type Rule struct { |
| 213 | + name string |
| 214 | + ranges []Range |
| 215 | +} |
| 216 | + |
| 217 | +var ruleMatcher = regexp.MustCompile(`^([^:]*): (\d+-\d+) or (\d+-\d+)$`) |
| 218 | + |
| 219 | +func ParseRules(input []string) ([]Rule, error) { |
| 220 | + rules := make([]Rule, 0, len(input)) |
| 221 | + for _, line := range input { |
| 222 | + rule, err := ParseRule(line) |
| 223 | + if err != nil { |
| 224 | + return nil, err |
| 225 | + } |
| 226 | + rules = append(rules, rule) |
| 227 | + } |
| 228 | + return rules, nil |
| 229 | +} |
| 230 | + |
| 231 | +func ParseRule(input string) (Rule, error) { |
| 232 | + subexprs := ruleMatcher.FindStringSubmatch(input) |
| 233 | + if len(subexprs) != 4 { |
| 234 | + return Rule{}, fmt.Errorf("invalid rule '%s'", input) |
| 235 | + } |
| 236 | + name := strings.TrimSpace(subexprs[1]) |
| 237 | + ranges := make([]Range, 0, len(subexprs)-2) |
| 238 | + for _, s := range subexprs[2:] { |
| 239 | + rg, err := ParseRange(s) |
| 240 | + if err != nil { |
| 241 | + return Rule{}, fmt.Errorf("invalid range '%s': %v", s, err) |
| 242 | + } |
| 243 | + ranges = append(ranges, rg) |
| 244 | + } |
| 245 | + return Rule{name, ranges}, nil |
| 246 | +} |
| 247 | + |
| 248 | +func (r *Rule) Matches(num int) bool { |
| 249 | + for _, rg := range r.ranges { |
| 250 | + if rg.Includes(num) { |
| 251 | + return true |
| 252 | + } |
| 253 | + } |
| 254 | + return false |
7 | 255 | }
|
0 commit comments