Monster Messages


Fork me on GitHub
2020-12-19

Day 19: Monster Messages

Description:
You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves at the Mythical Information Bureau contact you again. They think their satellite has collected an image of a sea monster! Unfortunately, the connection to the satellite is having problems, and many of the messages sent back from the satellite have been corrupted.

They sent you a list of the rules valid messages should obey and a list of received messages they've collected so far (your puzzle input).

The rules for valid messages (the top part of your puzzle input) are numbered and build upon each other. For example:

0: 1 2
1: "a"
2: 1 3 | 3 1
3: "b"

Some rules, like 3: "b", simply match a single character (in this case, b).

The remaining rules list the sub-rules that must be followed; for example, the rule 0: 1 2 means that to match rule 0, the text being checked must match rule 1, and the text after the part that matched rule 1 must then match rule 2.

Some of the rules have multiple lists of sub-rules separated by a pipe (|). This means that at least one list of sub-rules must match. (The ones that match might be different each time the rule is encountered.) For example, the rule 2: 1 3 | 3 1 means that to match rule 2, the text being checked must match rule 1 followed by rule 3 or it must match rule 3 followed by rule 1.

Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since rule 1 matches a and rule 3 matches b, rule 2 matches either ab or ba. Therefore, rule 0 matches aab or aba.

Here's a more interesting example:

0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"

Here, because rule 4 matches a and rule 5 matches b, rule 2 matches two letters that are the same (aa or bb), and rule 3 matches two letters that are different (ab or ba).

Since rule 1 matches rules 2 and 3 once each in either order, it must match two pairs of letters, one pair with matching letters and one pair with different letters. This leaves eight possibilities: aaab, aaba, bbab, bbba, abaa, abbb, baaa, or babb.

Rule 0, therefore, matches a (rule 4), then any of the eight options from rule 1, then b (rule 5): aaaabb, aaabab, abbabb, abbbab, aabaab, aabbbb, abaaab, or ababbb.

The received messages (the bottom part of your puzzle input) need to be checked against the rules so you can determine which are valid and which are corrupted. Including the rules and the messages together, this might look like:

0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"

ababbb
bababa
abbbab
aaabbb
aaaabbb

Your goal is to determine the number of messages that completely match rule 0. In the above example, ababbb and abbbab match, but bababa, aaabbb, and aaaabbb do not, producing the answer 2. The whole message must match all of rule 0; there can't be extra unmatched characters in the message. (For example, aaaabbb might appear to match rule 0 above, but it has an extra unmatched b on the end.)

How many messages completely match rule 0?

--- Part Two ---

As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules 8: 42 and 11: 42 31 with the following:

8: 42 | 42 8
11: 42 31 | 42 11 31

This small change has a big impact: now, the rules do contain loops, and the list of messages they could hypothetically match is infinite. You'll need to determine how these changes affect which messages are valid.

Fortunately, many of the rules are unaffected by this change; it might help to start by looking at which rules always match the same set of values and how those rules (especially rules 42 and 31) are used by the new versions of rules 8 and 11.

(Remember, you only need to handle the rules you have; building a solution that could handle any hypothetical combination of rules would be significantly more difficult.)

For example:

42: 9 14 | 10 1
9: 14 27 | 1 26
10: 23 14 | 28 1
1: "a"
11: 42 31
5: 1 14 | 15 1
19: 14 1 | 14 14
12: 24 14 | 19 1
16: 15 1 | 14 14
31: 14 17 | 1 13
6: 14 14 | 1 14
2: 1 24 | 14 4
0: 8 11
13: 14 3 | 1 12
15: 1 | 14
17: 14 2 | 1 7
23: 25 1 | 22 14
28: 16 1
4: 1 1
20: 14 14 | 1 15
3: 5 14 | 16 1
27: 1 6 | 14 18
14: "b"
21: 14 1 | 1 14
25: 1 1 | 1 14
22: 14 14
8: 42
26: 14 22 | 1 20
18: 15 15
7: 14 5 | 1 21
24: 14 1

abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
bbabbbbaabaabba
babbbbaabbbbbabbbbbbaabaaabaaa
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
bbbbbbbaaaabbbbaaabbabaaa
bbbababbbbaaaaaaaabbababaaababaabab
ababaaaaaabaaab
ababaaaaabbbaba
baabbaaaabbaaaababbaababb
abbbbabbbbaaaababbbbbbaaaababb
aaaaabbaabaaaaababaa
aaaabbaaaabbaaa
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
babaaabbbaaabaababbaabababaaab
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba

Without updating rules 8 and 11, these rules only match three messages: bbabbbbaabaabba, ababaaaaaabaaab, and ababaaaaabbbaba.

However, after updating rules 8 and 11, a total of 12 messages match:

bbabbbbaabaabba
babbbbaabbbbbabbbbbbaabaaabaaa
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
bbbbbbbaaaabbbbaaabbabaaa
bbbababbbbaaaaaaaabbababaaababaabab
ababaaaaaabaaab
ababaaaaabbbaba
baabbaaaabbaaaababbaababb
abbbbabbbbaaaababbbbbbaaaababb
aaaaabbaabaaaaababaa
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba

After updating rules 8 and 11, how many messages completely match rule 0?

Input:
124: 72 26 | 58 91
76: 72 90 | 58 73
89: 58 135 | 72 25
43: 58 30 | 72 98
130: 58 58
87: 135 72 | 100 58
24: 72 5 | 58 78
61: 84 72 | 71 58
8: 42
68: 49 72 | 91 58
103: 126 58 | 124 72
132: 58 17 | 72 37
75: 72 89 | 58 50
99: 72 72 | 58 58
28: 58 19 | 72 65
2: 9 72 | 67 58
113: 58 100 | 72 130
66: 129 58 | 70 72
74: 72 116 | 58 56
45: 72 82 | 58 38
70: 137 58 | 12 72
78: 72 58 | 58 58
115: 78 58 | 27 72
48: 58 121 | 72 95
23: 120 58 | 93 72
44: 58 25 | 72 130
17: 58 27 | 72 91
65: 102 72 | 46 58
19: 72 84 | 58 35
79: 29 72 | 127 58
13: 58 88 | 72 136
6: 15 72 | 130 58
81: 58 72
100: 117 72 | 58 58
116: 78 72 | 49 58
136: 20 72 | 50 58
97: 53 72 | 85 58
107: 58 27 | 72 5
0: 8 11
49: 58 72 | 72 117
88: 105 58 | 21 72
83: 69 72 | 5 58
112: 58 32 | 72 2
47: 62 72 | 118 58
59: 58 134 | 72 101
60: 132 72 | 103 58
55: 58 44 | 72 92
67: 72 15 | 58 100
96: 81 72 | 135 58
121: 25 58 | 26 72
39: 58 81 | 72 25
82: 81 58
27: 72 72 | 117 58
85: 58 119 | 72 68
53: 72 39 | 58 80
91: 72 58 | 72 72
129: 58 133 | 72 3
18: 58 69 | 72 25
120: 110 58 | 36 72
114: 58 87 | 72 16
34: 72 40 | 58 45
29: 58 123 | 72 59
36: 15 117
133: 58 107 | 72 113
109: 58 58 | 58 72
46: 100 117
41: 109 58 | 91 72
26: 72 72 | 58 72
72: "a"
21: 58 78 | 72 130
117: 58 | 72
111: 58 5 | 72 81
95: 58 27 | 72 109
4: 97 72 | 28 58
135: 58 117 | 72 58
37: 58 27
1: 58 99 | 72 25
52: 130 58 | 109 72
106: 5 72 | 99 58
12: 111 58 | 68 72
42: 94 58 | 79 72
11: 42 31
77: 125 72 | 86 58
98: 58 91 | 72 109
62: 58 122 | 72 52
119: 109 72 | 135 58
108: 15 72 | 25 58
10: 78 58 | 15 72
5: 72 58
3: 72 33 | 58 37
57: 58 51 | 72 7
7: 72 43 | 58 74
38: 58 91 | 72 15
20: 26 58 | 130 72
22: 58 99 | 72 15
105: 15 72 | 99 58
25: 72 72
35: 72 99 | 58 91
56: 78 72 | 99 58
63: 114 72 | 55 58
86: 23 72 | 13 58
54: 47 58 | 112 72
50: 72 5 | 58 99
126: 58 81 | 72 26
127: 63 72 | 60 58
9: 72 27 | 58 15
137: 10 58 | 96 72
15: 117 117
128: 66 58 | 57 72
58: "b"
14: 18 72 | 116 58
92: 58 25 | 72 100
40: 83 58 | 131 72
31: 77 58 | 128 72
69: 58 72 | 72 58
125: 34 58 | 76 72
110: 117 99
94: 72 4 | 58 54
102: 72 25 | 58 109
30: 58 91 | 72 5
84: 99 117
93: 50 72 | 6 58
80: 72 100 | 58 91
64: 58 5 | 72 130
51: 58 61 | 72 48
122: 5 58 | 25 72
16: 58 130 | 72 78
90: 22 58 | 106 72
131: 58 91 | 72 130
123: 14 72 | 75 58
33: 58 15 | 72 26
71: 27 72 | 69 58
73: 104 58 | 24 72
32: 72 126 | 58 41
134: 72 1 | 58 35
101: 72 84 | 58 115
104: 130 58
118: 64 58 | 108 72

baabababbaababbbaabbbbab
baabababbbabaabaabbbbbbb
bbbabaaababbbbaaababbabbaabbabba
bbbbbbaaaababbaaaababaaabbaaababaaababba
bbabbaaaabbbabaaaabbbbbbabbbbbaa
aaaabaaabbbabbaaaabbbbbabaabbbaabbbaaaabbababbbb
bbbabababbbbbbbbbbbbabbbababbaba
aabbbbaaabbabbabaaaababaabbbabbbbabbaaabaaaaabbbbaaababbabbbbbbabbbbaaababbbbaab
bbababbaaabbbbbbabaaaabbbbbbabba
babaaaabbaaabbbabaaaaabb
bbbabaaabaaabbbabababbaaabbbbbabababababaaaaaaba
bbaabaabbbaaaabaabaaabaa
bbaabababbaaabbbaaaaabaabababbbbbaaabbaa
babbbbaaaaaaaabbabbabbba
bbbbbbabababbabbbbbbbbba
bbaaababbaaabbbbaababbbababaaaabaabbbaaaabbbabbabaaabbab
bbaaaabaaabbbaaabbaaabaa
baabbbaababbbbaaaaabbbbbbaaababbbbbbaaba
abbabbbabaaaaaaabbabaaabaaabababbabaaabaaabbbaaa
aababaaababbbaabababbabbabbbabbbbaaaababaabbabab
ababbbbaaaaaaaabababaabb
ababbbaaaaaabbabbaaabbabbabbaabbaabaaaab
bbaabaabaaabbaaabbbbbbbb
babaababbbbbbaaabbababba
bbaabbaaaaaabbbbabbbabbbabaabbabababaaba
bbbbbaaababbabaabbbaabbaaabbbbababbabaabaaaaaabaaabababaaaaaaaabaaaaababbbbbaabababbabbb
aabbbabababbaaabbbbaaaabbabbbbbbabaabaab
abbbabbaabbbababbbababbabaabaaabbababbab
baaaabaaaaabbababbaaaaaabababbbbabbbbababaaaaaaaabbbabab
aabaabababbabbbbbaabbaabbbaabbba
aaaabbbbabbabbabbaabbaabbaaaababababaaaabaabaabbabbbbbaabaaaaaba
bbbaaaabbaabbaababbabbbaabaababbbbaabbbbbbaabaaaabbbbaabaaaaabba
babbaabbaaaabbababaaabbabababbaaabbbababbbbbaabb
aabbbbaaabbbabbbabbababa
aaabaabaabbaaaabababaaabababbaabbabaaabbabaababaabbbaaab
aabbbabbbaababbbbbaaabaaaabaabaabaabaabbbaabbbaabbbaabba
bbaabbaababbbaabaaaaaabbbbbbbbba
ababbabbabbbaabbabaababaaaaaaaaabbbaabaabbbabbba
bbbabbabbabbbabbabbaaaaabbabbbabbbaaababababbabbbaaaaaaaaaababaaaabaabaaabbbababbbbaaaab
babaaaabababaaaabbabaaaababaababbabaabbabaabaaabaabbabba
aaabaabaabbbaabbbbbbabbababbabab
baababbbbabaabbaabbbaaaaabbabbbabbaabaabbababbaabbabbbbaabbbbbbb
ababbaababbabbbbbaababaaababbaba
baabbbabaaabbaababaaaabbbabbbaaaaaaaabaaaabbbbabababaabbabaaaabb
bbbaaababbbbbbaabbbbabbb
bbbaaaabaabaababaaabbaaaabbabbaaaaaaabbbaaabbabbbbabbaababbaaababaaabbaa
aabbbabaaaaaabbbabbabbba
aaaaaaabababbbabaabbbbba
aaaaaaaaababbbaababababbbbbabbbbabababaaabababaabbbbbaba
bbaaaaabababaababbbaabbababbaaababbabababbababbaabbababbabaababaaabababbbbbbaaba
bbaaaabbaaaaaaaababbaaaa
aaaaabaababaababaaababaa
babaabbbbaaabbbabbabbbbbbaaabbababaaabaababbaaaa
bbbbbbaabababbaaaaabbbbabaabbaba
abaaabbbaababbaaaababbbbbbaaaaababbabaaabbbaababaaababaa
abbabbbabaaabaabaabbbabbbabbbabaababaaaaaaaaaaabbbbabaaaabbababbbaaaaaaababbabbbabbbbaab
aababbaaaaabaababbbaaabababbababbbbaabbbbabaabbbabaabbaa
bbaabababbbbaababbaaabbabaaaaabbbabababa
aabbbabaabbbaabaaabbbabaabaababababbaaba
bbabbaabbbbbbbbabbbbbaaaaabbaaabbbbabbbbbaaabbabbbbbbbbbbbbbbaababbaaabbaaaababbbbbbaaab
abbbababbbaaaaaabbbabbabbbbabbabbbabaabb
aababbaaabbbaabbababbaaa
abbabbbbbbbbaababaaabbbb
aaabbbaaaaabbbabbabbaaaa
ababbbbbabbabaabaaaaabaaabbabbaabbbbbaaa
aaaabbbabbbbababbbababbaabbbaabbaaababba
aaabbbabbbaabaababbbaaabbaaaababaabbabaababbabaa
baaaabaabaaabaabababaaba
abbbabbbabbbabbaaaabbaab
bbabbabbbaaaababbbaaabbaabbabbbbaabbbaaaabbbaabbaabbababababaaba
abbabbaaaababababaabbbaa
ababbbaabaaabbbabbbabbab
bbaaabbbbaaaaaababbbaababbabbbaabbaaaabb
bbabbabbaabbababbaabbabaaaabaaaaabababaaaaabbaab
bbaababaaaabbbaaaabbbbabbbabaabb
bbababbaabaabbbababaababaabaaaab
bababaaaaaaabbbabbbbaabaabbbabaababbbabbababaaba
babbaabbbbbbababbbabbbbbbaaaaaabbabbabaaaaabbabb
babbbabbabaaabbabbbbaaaa
aaaabbabbbababbabbaaaabaaaabbbaa
abbbaabaaababaaaabbabbbbbbbaaaaabbabaabbababbaaabbbbbbbabaabaabb
abbbabaaababaaaaaabaababbbababbaaabbabaabbbbaaabbbbbbaab
babbaabbbbbaababbaaabbbbabbaaaaa
babbbbbbbbbabaaabaabbbba
bbbaaabaabaaaaabbaabbababbaabbabbabbababbabbbabaaabbabab
aabbbaaaababbaabbbabaaab
bbbbbbaaababbbaabaababba
bbabbbbbbbabaababbbabbba
babbabaababbbaaababbabababbbaabbbbaaabbbaaaabbababaababb
ababaaaaaaaaaaabbbaaaabbbaabbbbb
abbbaaaaaaaaabbaababaabb
baabaabaaabbbabaabbabbabaabbbbba
abaaabbbbbabbbbbbbbaabbbbbaaaabbbbbaabbbbbabbbababbaabaa
aaabbbaaababbbbabbbbabbabaabaabb
abbabbbbbababaaababbabababbabbbbbaaaaaabbbaabaaaaabaaabb
ababbabbaabbaaabaaababbb
bbbabaaaabaaaaabbaabbaabbbaababaaaaaababaaabaaabbbbbabaa
babbbababbbbbbabaaabaabababbbbbbbaaabbbb
aababaaaababbbaaababbbaababbbaaabbbaabbaaababbba
ababbbbaaabbbaaaaabaababaaaaaaaaabbababababbaaba
aabbaaababbabbaaaaabbbaa
bbbbaaabbaabbababaabbbabbbabaaabbbabbabababaabab
babaababbbbbabababbabbbbbbbbaaaa
abbbabaaabaaabbbaaabbbaaaaabbabaabbabbaaaabbaabaaabaaabaabbbbabbbabaaabbaababbaabbabaabb
aaabbbbaabbbababababbabbabaaabbabaabaabb
aaabbbaabbbbababbbaababb
baabababbbbabbbabbaabbababbbaaabaaababbb
bbbabaaaabaaaaabbbababab
baaaaabbbabbabbbbbaaaabaababbbbaabaabbabbbaaabbaababaaba
abbaaaabaaabbaaababbabababbbaababbbbbbaabbbbbaaa
baabbbabaababaaaabbababa
aabbbabbabaaaaaabbbbbaba
aabbbbaaaaaaababbaabbbaababbabbbaaababbbabaabbbbbabbaaaa
aabbaabbbabaaaabbbabbaab
aabbaaaaabababbbabaaaaaabbbababbaababababbaabbabaaabaaaababbbbab
bababbaaababaaaabbbbbbbb
aaaaababbbaaaabbbbaabbab
baabbaaaaabbbaaabaaababa
bbbabbbabbaabaaabbbbaabbbbabbbaabbbaaaababbababbbaabababbaabaaab
bbababababbababbbaaabbaaabbabababbbbbbaabaababaa
aababbaabaaaabbbabababaa
aaabbaaabbbaaaabbbbbaaaa
bbabbabbabbaaaababbababa
bbbabaaaaaabbaaaaabbabbbaaabbabaabbabaaa
aaaaaaaabbaababaaabbbbaabaabababbbbbbaab
babbaabaababaabaabababaabbbbbbba
aababaaaababbbbbbabbabababababaaabbbaaab
baabbbaabbbbbbbbabbaabbb
baababbbabbabbabaaaaaababaaabaaa
ababbbababbbbbabbaabbbaa
bbaababaaabbbbaaababaaababbababa
bbabbaaaabbabbbbaaabaaab
abbbabbabbabaabaaaaaaabbabbbbbbbbbbbbaaa
abbaaabbbbaaabbbbaababbbaaaabaab
bbaabababaaabbbaabbaabbb
ababbabbaaabaabaaaaaaaabababaaababaaabbbaabbabaabbbbbaba
aababababaaaababbabaaaababaaabbbbaaaaaba
babaaabaabbbbbbbabbbbbbbbaababaabbaaabaa
bababbbabaabbbabaaabbbaaaaabaababaabbbaabbbbbbba
ababbbabbbaaaabaabaabaaa
ababbbbaaabbaaabaababbbbaaaaaaaaabbababb
bbaabaabaaaabbabaaaaaaabbabbaaaaabbbbbbb
aabaaabbababbbabaaaabaabbbaababb
babbbbabaaaabababaababbabaaaabbabbabaaabaaabbbbbbbaabbbb
babbaababbbbbbbabbabaabbbbabbaaaababaaaa
baabaabababbbbabbaaabaaaabbbaabaaabaabbabbbbabbbabaaaabababaaaabbbbbbabb
bbbabaabaabbbabbbaabbaabaabaabba
babbbababbabbabbabbbaabaaaaabbbbbabaabbbabaaaabbbaaabaaa
aaabbbaababbbaaaaaabbbbaaaaaabbbaaaabbaaaababbbabbbabbba
babbaabbababbbbaaababbaabbaaaaaa
abaababaabbbabaaaaabbaba
abbabbabaabaaaaaaaabbaba
bbbbbbaaababbbbaabbaaabbaababbbaaaababbb
abbaaabbbbbbababaabaaaaaaaaaababbbbabbaa
abbbabaaaabbaabbaaabababbbbbaaaa
bbbababbaababbbbbbaaabbbbaabbababaaaaabb
abbaababaabbbabbabababab
ababbaababaaabbbbababaaaaabababaaaabaabb
aaaaababbabbabaababbabba
aabbaababbaabaaaaaabbabababaaaaa
bbabbabbaabaabbbbababaaabbbababbababaabb
babaaababbababbabbbbbbaabbbaaaaabbaaaabbbaaaaabb
aaabaabaaaaaabbbaaaabababaaabbbbbbbbabbbbaababababaabbbaabaaabaabbabbbba
baabababbabaaaabababaaabbaabbbba
abaaaaaababbbaababbbbbba
baabbaaabaabbbabbbabbbaa
ababbbbbabaababaaabbabba
abbaaabbbabbaaabbbbbbaaa
ababaaaaaabbaabbbbbaaaaaabaabbbabbbabaabbababbabbbbbaaababbbbaab
aabaaaaaaaaabaaabababbbaababaabaaaaabbababaabbababbaaaabbbabaaba
babaabababababbbaaabbbaaaabaaaabaaaabbbbaaababaaaababbaabaabaaabababaabb
ababbaabbbabbaababbababa
ababaaabbbaaabaabbbababbbaabaaabaabaaabb
aabbbabaaabaabababaaabbaaaabbabbaaababbb
baaabbabbbaaaababaaabaabbaaaabbbabbaaaabaaaabaabbaaabaaa
baaaababbaabbbbaaaaabbba
bbababbaaabaababbbbbbaaa
bbbaaabaabaaabbabaabaaaa
aabbbbaabaababbaaaabbaab
ababababbbbabbbaabbbbabbbaaababb
abbaaabbbaaaabbabbbbbaba
aababbaaaaaaababbabbaaba
abbaaabababbaabaaaaaaabaabbaabaabaabaaababaaaaba
abaabbbababbbbaabbbbaabb
abaaabbaaaaabbbbaaabaaaa
bababbbbbaabbbbbbababbababbbbaba
abbbabbbabbaaaabbabbbaaaaaabaabababbbbbaabaaabab
ababbbbbbabbbbabaaaaabba
baaabbabbaaaaaaaaabbbbaabbbbbaba
aabaaabbabbaabbabaabaaabbabbababaaabbaabaababaab
abbbbabbaaabbaaaabbbbbaaababbbbbbbbaaabbbabbaaab
ababbaabbbbabaaaabaabbaa
abababbababbabaabbaaaabaaaaaaabbaaaaaaabaababbababababbbaababbba
aaaaababbaababaaabbbaaababbbbaabbbbabbbaababbaaa
bbaaaaabbabbaabbbbbbbbba
ababbabbabaaabbbaabababa
bababbbaabbaaaaabbbbbaab
abaaaaaabababaaabaabaabaaaaabaaa
abaaabbaaababaaabbbbbabaababbbbbababaabababbbbbaabbbabbbabbaaabaababbaabbaabbabb
ababaaaabbaabaaaabbabbba
bbbabaaabbbabaaaabaaaaabbababbaaabababbb
bbabbabbbaaaaaabbbbbbbbb
bbbbbbaabbaabaabaaabbabb
bbbabaaabbabbabbaabbabba
aabaababaaaaaaaaaabbbabaabbbabbaabbbbaabbbbbaaaa
abbaaabbaaabbbbaaababbab
abaabababbbabaabbaaaabaaaaabbabbabaabaaa
babbbaaabaabbaabbbbaabbbabbbabbababbbababbbabbaa
aaaaaabbbbbaaababbaababb
abbaaabbbbbbabbabbaabbab
bbbbabbaabaababaaaaabababaaaabba
baaababbbbaaabaaaabababbbbaabbbbbbababab
babbbbababbbabbbaaabaaab
bbaaabbbbbbaabaaaabbbbbbababbbabaaababbabbbbbaba
bbabababbababbbabbbbbaabbaababbbbbaababaaabbabbababbaaaaababbbbabaaabaabbbbabbaaaabbbaba
bbaabbbababbbbbbbaaabababbaaabaabbababbaabaaabaababbbaabbbbbbbba
baaaabababbaaaaabbabbbbabbbababb
aababbbbbabbaabbbbaaaaabaabbabbabaaababa
babbbbaaababbbabbbbbabababaaaaaaabbaaaba
babbaabbabbabbabaaabbbaabababaaababbabbaaababaabbabbabbb
bbaabaabaabaabbbababaabb
aaaabbbababbababbabbabaaaaabbbbabbbababbbbbabbbbbaaaaabb
aabbbbaababbbbababbbbabbbaaabaaa
aaabbbbbabbbaaabbaababaa
babaaaabaabbbaaabbaabbaa
aaaaabbbaaabbbbabaaabbabbabbbababbabbbab
bbbbaababaabababbabbabbb
abbaababbbaaaababbbaaabaaabbabaa
ababbaabaaabbbabbaabaaab
baababbbabaaaaaaabbbabbabbabbabaaababbba
baaabbbaaabbbaaababbabaabbbbbaba
bababbbbababbbbababaababbaabbbaabaaabbbbababaababbbbabbbbabaaababababbaababbbbaa
abbaaaaabbbbbbababbbbaaa
bbaabbababbbbaaaabaabababaaaabbababababbbbababbb
abbbabbaabbaaabbaaabbaba
aaaabbbababbbbabaababaab
bbaabbaaabaabbbababaaaabbbaabbbabbbabbab
babbbbbbbbababbaabaababb
bbbbabbbaaabbbbbaaaabaabaababaababaabbbb
babbbabbaababbaabaaaaabb
abbbabbbaabbbabbbbaaaaba
babababbaabbabbaaabbbbab
baaaabaaaaabaabaabbaaaba
ababbaaabbbabbbbabababbabbbbabbb
babaabababaaabbabababbaaaabaabaa
bababbaaabbabbabaaababaa
baaaabaaaabbbbbbabbabbba
aabbaababaabbbabbabbbbbbbabbbabbaaaaabba
abbaabbbabbbbbaabbaabbbbbbbbaababababbabababbaab
ababbbbbabaaaaabbbbaaababbbaabbbaaabaaaabaaaaababaabbbaa
babbababababaaaaaababbab
babaaabbabaaaaaabbbbaaaa
bbbbaaaabbbabbbabaabaabbbbbabbab
bababbaaabbbaababababaaaabbabbbbbbbaababbabbabba
ababbbababaaaaababbabaaa
bbbbaababababaaaababaabb
baaaabbbaabababababbabba
aabbbabbaaabbbabbbaaaaabbbabbaabaaaaaaaabbaaaabaabbaabaa
aabaababaababbabbbbaababaabbbbabaaabbbbbaaabaaabbababbab
abbabaabbbabbbaaabbaabba
aababaaaaababaaabbaaaabbabbbbbaaabababaa
baabbaababbbbaabbabbbaaaabbabaaaaaaabbbabbbaaabaaaabaaabbbaabbaaabbaabbbaabbabaaaaaabaab
babbbabbbaababbbabbabaaa
aaababababbbaabbabbabbba
babbbbbbaaabbaaabbbbbaab
abbbaabbaaaaabbbbbabbbab
bbaabababaabbbabbabababa
bbabbbbabbbaaaabbaabaabaaabbbabbbbbababb
abbabbabbbbbaabaababbbbbbbababbaabbbbbbaaabaaabbbbaabbba
bababaabaababaaabbabbbbb
babbaabbbbaabaabbbaabbbabaabbaaabbbbaaabaabbabba
bbabbaabababbbbabbbabaaa
baabbaabaaaaabaabbaaabab
aaabababbabbbaaabaaaabbaaaabbbaabaaaabbbabbbbbabaaaabaababbbbaab
baabbbabaabababaaaaabaabaaaaabaaaaaaabbaaabbbaabaabbbbaabaaaabba
bbaabbaaaabbbaaaabaaabab
baaaaaaaaabbbababaaaaabbaabbbbbabbaaabab
aabbaabbabbbaaaabbbabbaaabaabbbbababbaba
abaabbbaaaabbbaabbaaaabbbaabbbbb
bbbbabbbaaabaabbaaaaabba
babbabaabaaababaabaabbabbbaaaaaa
abbbbaababbbaabbabaaabbbbaaaaababaaabababbbbbaab
bbaaaabbbbbbaaabbababaabaaabbbaababbbbabaaabbababbbababaaaaaabbbbbababba
baaaabbbbbbbabbabbaababb
bbaaabbaabbbaaaabaabbaba
abaaaaabbbbbaabaaababbbbbbbaaaab
aaaabbbaababaaabbbbbabbabbaaaabbaababababbbbbababaabbababbbbaaaabbbaaabb
abaaabbbbbbaaaaabaabbbaa
aabaababbbabbbbaaaabbbaabbaaaaabaaabaabb
bababaabbababaabbbaaabaa
baaaaaabbabaabbbbabaaabbbbabaabb
bbabbbbbabbabbababbaaabbbabbababbaabbbbbbbbabbaa
aabbbbbaabbbabbabababbbaaabbbbbaaaabaaab
bbbabaabaabbbabababbabaababbbbbababbaaaa
aaaabababaaababbbbbbbababbaabbbbbbbbaabbaababbbbaaababaaabbbaababaabbabbbbbbbbbb
aaaabaaabbabbabaabbbaaab
aaaabaabbbbaaaabbaaabaabbababababbbaabaaababbabaaabbabaa
bbbabaaabaaaabbaabbbbaab
abbabbbbbbabaaaababbbaabbaaaaababaabbabbbbbaababbabaaaaa
abbabbaaaaaabababaaabbabaaaaabaabbbaabbaababaaba
aababbbabaababaaabbbaabbbbbbabaaaabbaababababbaaaaaaababbbbbbabbaababbbaaababbaabbbbabab
abbaaaabbbaaaaabbaabbaaaaabbabbbbbbabaabaabbabbbbbbabbbaabbababb
baababbaababbabbbaabbaba
bbababbaabbaababaaababbabbaaaaabbbabbabaabbbbababbabbbabababaaaa
aabbbbababababbaabaabbaababbbbba
abaaaabbaaabbaaabbabbbaa
abbaaabbabaabbbabababaabbbbaabba
ababaaababaaabbbabaaabaa
aabbaabaaabbbbaabaabaaab
aabaabbbabbaaaaaabbbbabaaabaabbaaaababbbaabbabbabbbbaaaaabbaabaa
baaabaabbaaaaaaabaaaaaabbababaaaabbbababbbbbaaaa
aababbbabbabaaabbababaaaabaabaaaabbbaaba
abbabbbabbbabaaabbabababbbabaaaabbaaaaaaaaabababbbabbbab
bbbabaaaaaaabbabababbaabaaaaabaaabaabbbabbbabbbabaababbbbbababbbaaaaabbb
baabababaaaaabbbbbbabbbb
abbaaabbaaabaabaabaabaab
abbbbbabbabaaababbabaabbbbabaaaaabbbbaabbaaabababaabaaaabababbbabbabbbaaabbaaabbababbabb
ababaaababaabbbabbbbaabb
bbaababaabababbaabbbabbbbbabbabaabaabbaa
babaabbbababbbbabaaaababbbaaaabababababaabababab
bbababbabbbbababaabbabaa
bbaaaabababaababaaaabbbaabaaaaabbaabbbabbbbaabba
bbabaababbbabaabaabaababababbbaabbbbbaabbbabbaba
bbababbabbabaaabaaaabbbb
baababbbbbaabbaaabaabaaa
abaaaaaaabbaaaababaabaabbbbbaabbbaaababb
abaaaabbbabbabababbbbaaa
abbbababaabbaabbbaaabbaabbbabbbbaabbbbbabbbbaabb
aabbaabbbbabbaaaaabbbbba
bbbbabbaabaaaabbbaaababbabbababa
babbbabaaabaaaaaabbbbbabaabbabab
baaaaaababbbababbbbababa
baabbaabaabaaaaabaaabaabaaaabaabaabbbbba
aabaabbbbabbababaaabaabaaabaabaa
aabbaabbbabbabbbbababaababaaaaabbbaababaabbabbaabbabaaabbbabbaab
bbabbaabbabaaabaabbbaabbbbbaaaaaaaaabbaaaabaabaa
babbaaabbabaabbbaababbba
aaaaabbbbbaaabbaabbabbaabababbbbaaababba
babbbaaaabaaabbabaaabbabbbabbaabbbababbb
babbbabbbabbbbaabababbaaabababbbbbaababbbbaabaaabbbbaabb
bbabbbbbaabbbabbbabaabbbabababaa
abbbabbabaaabbbbaaaabaaa
babbbbaabaaaaaababaabbbb
aaaababababbbaabbbbaabbbbabbaaabbbaaaaaa
abbbaabbbababaaabaaaaabb
bbaabbaabbaaabbabbaaabbaabaabaabbaaabbbb
bababbababbabbbbabababbaaaabaabaaabaaababbaabaabbbabbabbaabbabbbaaaaaaababaaabba
abbababaaaabaaabbaabbaba
baaababbbabaaabaaaaabaaaabaaaaababbbbbbbbabbabbbabbbabaabaababbabaaabbaaaaabaaabaabaaabababbbaab
bbaaabbbabbabbaaabababab
aabababaabbabbbbbbababbaaaaabbbbabbbbbba
aaaaaabbaaabababaababaaabaaababb
abbabaabbbbaaaaaabbaaaba
abbbababaaabaababaaaaaaababaaaababababbbaabbbbba
abbabbaababbbbababbaaaabbbbaabbbbabbbabbbabbabba
bbbaabaaaaabbbaababbaabbbbbabaabbaaababaaaaababb
bbaaaaababaabaaabaaabbaaabbaababbbbaaaabbbbbbaba
bbaaaabbabbbbbbbaaaaaabbabbbbbabaababbaabaaabaabbbabaaabbaaabbbabbbabaabaaaabbaaaabaaabb
abbabbaabbaaabbbbabbbbaabbaabaababaabaaaabbbaaababaabaabaaabaaab
abbbabaaaabbaaabbbbabbaa
baabaaaabaaaabbaabaaaaab
aabbbabaabaaabbbbabbabba
bbababbabbbaabbbbbbaaaabbaaabbabbbaabaaa
aaaabbbbbaaaababaabababababaabba
aabbbabababbbaaaaaabbbbaaaabaaaa
abbaaaabbababbbaabbabbabababaababbbaaabb
baaaaaabbbabaaaaabbaabbb
aaaaaaabbbaaaabaabaaabaa
aabbbbbbbbabbaabababaaba
bbbbbbaaaaabbaaaabbaabbb
bbaaabbbbabaaabbbaaaabaaaabbaabababbbabbabaaaabaabababbb
abbbbbabaabbbabbbbbababb
abaababaabbbaabbabbbaababbababbbbbbbbbba
aabbbabaaaabbaaabbaabbaaabbbbaaa
aabbabbbaaaabbabababbaba
aaabbbbababbbaaababaabbbbaabbaba
aabbbababbabbaaaabbaaaaaaaaaaaaabbbaabbaaabababb
ababbbabbabaaababbaabbbb
aabbbabbbaabbbabababbabbabbbabbabaaabbbabaabbbbb
abbbabbaaabbbababbabbbbbbbbbabaabaaaaabbabbbbababbbababb
baabaababaaaababbababbbb
bbaaaaaabaabbbaaaaaabbbaaabbbbabbbaaabbbbbbbbbbbabbbbaaababaabaaaaaaaaabbbabaababaabaabb
aababbbbabaaababbbabbaaaaaaaaabbaabbbbbaaababababababaababbaabababababbbabbbababbbaabaab
bbabaabaaabbbbbbbababbaa
aaababbabbbbababaababaabbbababaabbbabaababaaaabbaabbbaaa
bbababbaaabbbaaaabbaabbb
aabbbabaababbbaabbbabaaaaaaabbbabbbbbbaaabbababaabbaaabaabaabbaa
ababbbaaaabaabbbbbbbbbbb
abbbabaababbabaababaabba
aaabbbaababbabaabbbbaababbabbbab
bbaaaababaaabbbabbaababbababbababaaabaaa
bbaaabbaabbaababaaaaabbbbaabbbaaabbbbaaa
aaaaabababbbaaaabbbbbbabbabbbaababbabbaabbbbaabb
aaabbaaaabbbabaabbbbbabbbbaababbbbbbbbbabbaaaabaabbbabbbaaaabbabbbabbaaaaaaaabbbbbabaaba
babbbaabbaabaabaaaaabbbabbbbaabaaaabaabbbbabaabb
abbabbabbabaaabaabbaabaa
abaaaabbaaaaaaabbaaabbbabbaaabaa
bbbbbbaababbababababbaba
bbaaabbaabbbabbbabbaabba
babaabbbaabaababaabaabba
ababbbabbabbbaaabababbab
bbaabbaaaaaaaaabbaabaaaa
abbbbbabbbbabaaaaaabbbbabbbbbbbabaabbbaaaababbab
bbbbaababaaabbabababaaba
babbabaababbaaabaababbaa
babbbaaaababaaababbbabaaabbbabaaabbbbbba
bbbaabaababbababbbabaabb
aababbbbabbabbbbababbbabbbabbababbababab
baaaabbbbaaabbabbaabaabaaababbaaaaaaaabbbaabbbaaaabaaabb
aaaaaaaaababbbbbbbbaaaaabababaaabbbbbaaa
aaaaabababbabaabbabbabaaaaaabaab
ababbbabbabbbaabbaaababb
abaaabbababaaaababbabbaaabaaabaa
babbabbbbabaaabbaabbbbaaabbbabaababbbbbbabaabbabaaabbbba
aaaaabbbabbabbbbbbaaaaaa
aaababbabbabaaabbabbabbb
babaabbbabbbabbbaabbbbbbbaabbaabaababbab
aaabbaabbaabbbbaaaaabbabbbaaaabbbaabababaaabbbabbababaaabbaababb
babbbbabababbabbbababbab
bababbbbbaabaaabaabaaababbaabaabaaaaaaabbbababaaaaabaaba
aaaaaaaabaaaabbbbbaaabaa
bbabbbbbaabaaaaaaaaaabaaabaabababbbbbbaaaaababbaaaababbb
aaabababaababbababbbbbaabbaabbba
baababababaaabbbbaaabbbb
ababbbabbaababbbbaaaaaababbbaabbaaaaabaabaabaaaa
babbaabbaabbaabbbaaaabbbababbbbbbabbbabbaaaababbbaaabaaaaababbaaaaababbbbbbbabbaaaababaa
aaaaaabbbbabaababbbaaababbababaa
babbbbaababbaabbabbbbaba
bbaabbaaaaaabbbbabababbbbbabbbbbbaaabaaaabbabbbabbabbaaaaaaababa
ababbbaababbbaaaaabaabaa
baaaabbbbbbbabbaabbabaaa

use std::collections::HashMap;

use crate::common::AdventOfCodeDay;
use regex::Regex;

pub struct Day19 {
    rules: HashMap<u32, Rule>,
    input: Vec<String>,
}

#[derive(Debug, Clone)]
enum Rule {
    RuleExpand(Vec<u32>),
    RuleSplit(Vec<u32>, Vec<u32>),
    RuleLiteral(char),
}

impl Day19 {
    pub fn new() -> Self {
        let input_bytes = include_bytes!("../res/19_input.txt");
        let input_str = String::from_utf8_lossy(input_bytes);

        let rex_lines = Regex::new(r"(\r?\n){2}").unwrap();
        let split = rex_lines.split(&input_str).collect::<Vec<&str>>();

        Self {
            rules: split[0].lines().map(|p| Day19::parse_rule(String::from(p))).collect(),
            input: split[1].lines().map(|p| String::from(p)).collect(),
        }
    }
}

impl Day19 {
    fn parse_rule(input: String) -> (u32, Rule) {
        lazy_static! {
            static ref REX_EXPAND: Regex = Regex::new(r#"^(?P<id>[0-9]+):(?P<exp>( [0-9]+)+)$"#).unwrap();
            static ref REX_SPLIT: Regex  = Regex::new(r#"^(?P<id>[0-9]+):(?P<exp1>( [0-9]+)+) \|(?P<exp2>( [0-9]+)+)$"#).unwrap();
            static ref REX_LITERAL: Regex  = Regex::new(r#"^(?P<id>[0-9]+): "(?P<chr>[a-z])"$"#).unwrap();
        }

        if let Some(cap) = REX_EXPAND.captures(&input) {
            let id = cap.name("id").unwrap().as_str().parse::<u32>().unwrap();
            let exp = cap.name("exp").unwrap().as_str().trim().split(' ').map(|p| p.parse::<u32>().unwrap()).collect::<Vec<u32>>();

            return (id, Rule::RuleExpand(exp));
        }

        if let Some(cap) = REX_SPLIT.captures(&input) {
            let id = cap.name("id").unwrap().as_str().parse::<u32>().unwrap();
            let exp1 = cap.name("exp1").unwrap().as_str().trim().split(' ').map(|p| p.parse::<u32>().unwrap()).collect::<Vec<u32>>();
            let exp2 = cap.name("exp2").unwrap().as_str().trim().split(' ').map(|p| p.parse::<u32>().unwrap()).collect::<Vec<u32>>();

            return (id, Rule::RuleSplit(exp1, exp2));
        }

        if let Some(cap) = REX_LITERAL.captures(&input) {
            let id = cap.name("id").unwrap().as_str().parse::<u32>().unwrap();
            let chr = cap.name("chr").unwrap().as_str().chars().nth(0).unwrap();

            return (id, Rule::RuleLiteral(chr));
        }

        panic!();
    }

    fn check_rule(rules: &HashMap<u32, Rule>, str: Vec<char>, exp: Vec<u32>) -> bool {

        if str.len() == 0 && exp.len() == 0 { return true; }
        
        if str.len() == 0 || exp.len() == 0 { return false; }

        let r = rules.get(&exp[0]).unwrap();

        match r {
            Rule::RuleLiteral(rchr) => {
                if *rchr != str[0] { return false; }
                let str_sub = str.iter().skip(1).map(|p| *p).collect::<Vec<char>>();
                let exp_sub = exp.iter().skip(1).map(|p| *p).collect::<Vec<u32>>();
                return Self::check_rule(rules, str_sub, exp_sub);
            }

            Rule::RuleExpand(rexp) => {
                let str_sub = str.clone();
                let mut exp_sub = rexp.clone();
                exp_sub.extend(exp.iter().skip(1).map(|p| *p));
                return Self::check_rule(rules, str_sub, exp_sub);
            }

            Rule::RuleSplit(rexp1, rexp2) => {
                let str_sub1 = str.clone();
                let mut exp_sub1 = rexp1.clone();
                exp_sub1.extend(exp.iter().skip(1).map(|p| *p));
                if Self::check_rule(rules, str_sub1, exp_sub1) { return true; }
                
                let str_sub2 = str.clone();
                let mut exp_sub2 = rexp2.clone();
                exp_sub2.extend(exp.iter().skip(1).map(|p| *p));
                if Self::check_rule(rules, str_sub2, exp_sub2) { return true; }

                return false;
            }
        }
    }
}

impl AdventOfCodeDay for Day19 {

    fn task_1(&self) -> String {

        return self.input.iter().filter(|v| Day19::check_rule(&self.rules, v.chars().collect(), vec![0]) ).count().to_string();

    }

    fn task_2(&self) -> String  {

        let mut rules = self.rules.clone();

        rules.insert(8, Rule::RuleSplit(vec![42], vec![42, 8]));
        rules.insert(11, Rule::RuleSplit(vec![42, 31], vec![42, 11, 31]));

        return self.input.iter().filter(|v| Day19::check_rule(&rules, v.chars().collect(), vec![0]) ).count().to_string();

    }
}
Result Part 1: 142
Result Part 2: 294


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript