part1 (3441B)
1--- Day 16: Ticket Translation --- 2 3As you're walking to yet another connecting flight, you realize that one of the legs of your 4re-routed trip coming up is on a high-speed train. However, the train ticket you were given is in a 5language you don't understand. You should probably figure out what it says before you get to the 6train station after the next flight. 7 8Unfortunately, you can't actually [1m[37mread[0m the words on the ticket. You can, however, read 9the numbers, and so you figure out [1m[37mthe fields these tickets must have[0m and [1m[37mthe 10valid ranges[0m for values in those fields. 11 12You collect the [1m[37mrules for ticket fields[0m, the [1m[37mnumbers on your ticket[0m, and 13the [1m[37mnumbers on other nearby tickets[0m for the same train service (via the airport 14security cameras) together into a single document you can reference (your puzzle input). 15 16The [1m[37mrules for ticket fields[0m specify a list of fields that exist [1m[37msomewhere[0m 17on the ticket and the [1m[37mvalid ranges of values[0m for each field. For example, a rule like 18class: 1-3 or 5-7 means that one of the fields in every ticket is named class and can be any value 19in the ranges 1-3 or 5-7 (inclusive, such that 3 and 5 are both valid in this field, but 4 is not). 20 21Each ticket is represented by a single line of comma-separated values. The values are the numbers on 22the ticket in the order they appear; every ticket has the same format. For example, consider this 23ticket: 24 25.--------------------------------------------------------. 26| ????: 101 ?????: 102 ??????????: 103 ???: 104 | 27| | 28| ??: 301 ??: 302 ???????: 303 ??????? | 29| ??: 401 ??: 402 ???? ????: 403 ????????? | 30'--------------------------------------------------------' 31 32Here, ? represents text in a language you don't understand. This ticket might be represented as 33101,102,103,104,301,302,303,401,402,403; of course, the actual train tickets you're looking at are 34[1m[37mmuch[0m more complicated. In any case, you've extracted just the numbers in such a way 35that the first number is always the same specific field, the second number is always a different 36specific field, and so on - you just don't know what each position actually means! 37 38Start by determining which tickets are [1m[37mcompletely invalid[0m; these are tickets that 39contain values which [1m[37maren't valid for any field[0m. Ignore [1m[37myour ticket[0m for 40now. 41 42For example, suppose you have the following notes: 43 44class: 1-3 or 5-7 45row: 6-11 or 33-44 46seat: 13-40 or 45-50 47 48your ticket: 497,1,14 50 51nearby tickets: 527,3,47 5340,[1m[37m4[0m,50 54[1m[37m55[0m,2,20 5538,6,[1m[37m12[0m 56 57It doesn't matter which position corresponds to which field; you can identify invalid 58[1m[37mnearby tickets[0m by considering only whether tickets contain [1m[37mvalues that are not 59valid for any field[0m. In this example, the values on the first [1m[37mnearby ticket[0m are all 60valid for at least one field. This is not true of the other three [1m[37mnearby tickets[0m: the 61values 4, 55, and 12 are are not valid for any field. Adding together all of the invalid values 62produces your [1m[37mticket scanning error rate[0m: 4 + 55 + 12 = [1m[37m71[0m. 63 64Consider the validity of the [1m[37mnearby tickets[0m you scanned. [1m[37mWhat is your ticket 65scanning error rate?[0m 66 67