aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

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 sea monster! 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 the rules valid messages should obey and a list of
      9received messages they've collected so far (your puzzle input).
     10
     11The rules for valid messages (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
     26at least one 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 or 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 received messages (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 the number of messages that completely match rule 0. In the
     72above example, ababbb and abbbab match, but bababa, aaabbb, and aaaabbb do not, producing the answer
     732. 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
     77How many messages completely match rule 0?
     78
     79