part1 (3183B)
1--- Day 19: Monster Messages --- 2 3You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves 4at the Mythical Information Bureau contact you again. They think their satellite has collected an 5image of a [1m[37msea monster[0m! Unfortunately, the connection to the satellite is having 6problems, and many of the messages sent back from the satellite have been corrupted. 7 8They sent you a list of [1m[37mthe rules valid messages should obey[0m and a list of 9[1m[37mreceived messages[0m they've collected so far (your puzzle input). 10 11The [1m[37mrules for valid messages[0m (the top part of your puzzle input) are numbered and build 12upon each other. For example: 13 140: 1 2 151: "a" 162: 1 3 | 3 1 173: "b" 18 19Some rules, like 3: "b", simply match a single character (in this case, b). 20 21The remaining rules list the sub-rules that must be followed; for example, the rule 0: 1 2 means 22that to match rule 0, the text being checked must match rule 1, and the text after the part that 23matched rule 1 must then match rule 2. 24 25Some of the rules have multiple lists of sub-rules separated by a pipe (|). This means that 26[1m[37mat least one[0m list of sub-rules must match. (The ones that match might be different each 27time the rule is encountered.) For example, the rule 2: 1 3 | 3 1 means that to match rule 2, the 28text being checked must match rule 1 followed by rule 3 [1m[37mor[0m it must match rule 3 29followed by rule 1. 30 31Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since 32rule 1 matches a and rule 3 matches b, rule 2 matches either ab or ba. Therefore, rule 0 matches aab 33or aba. 34 35Here's a more interesting example: 36 370: 4 1 5 381: 2 3 | 3 2 392: 4 4 | 5 5 403: 4 5 | 5 4 414: "a" 425: "b" 43 44Here, because rule 4 matches a and rule 5 matches b, rule 2 matches two letters that are the same 45(aa or bb), and rule 3 matches two letters that are different (ab or ba). 46 47Since rule 1 matches rules 2 and 3 once each in either order, it must match two pairs of letters, 48one pair with matching letters and one pair with different letters. This leaves eight possibilities: 49aaab, aaba, bbab, bbba, abaa, abbb, baaa, or babb. 50 51Rule 0, therefore, matches a (rule 4), then any of the eight options from rule 1, then b (rule 5): 52aaaabb, aaabab, abbabb, abbbab, aabaab, aabbbb, abaaab, or ababbb. 53 54The [1m[37mreceived messages[0m (the bottom part of your puzzle input) need to be checked against 55the rules so you can determine which are valid and which are corrupted. Including the rules and the 56messages together, this might look like: 57 580: 4 1 5 591: 2 3 | 3 2 602: 4 4 | 5 5 613: 4 5 | 5 4 624: "a" 635: "b" 64 65ababbb 66bababa 67abbbab 68aaabbb 69aaaabbb 70 71Your goal is to determine [1m[37mthe number of messages that completely match rule 0[0m. In the 72above example, ababbb and abbbab match, but bababa, aaabbb, and aaaabbb do not, producing the answer 73[1m[37m2[0m. The whole message must match all of rule 0; there can't be extra unmatched 74characters in the message. (For example, aaaabbb might appear to match rule 0 above, but it has an 75extra unmatched b on the end.) 76 77[1m[37mHow many messages completely match rule 0?[0m 78 79