Done day 21
[advent-of-code-20.git] / problems / day19.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 19 - Advent of Code 2020</title>
6 <!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
7 <link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
8 <link rel="stylesheet" type="text/css" href="/static/style.css?25"/>
9 <link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
10 <link rel="shortcut icon" href="/favicon.png"/>
11 </head><!--
12
13
14
15
16 Oh, hello! Funny seeing you here.
17
18 I appreciate your enthusiasm, but you aren't going to find much down here.
19 There certainly aren't clues to any of the puzzles. The best surprises don't
20 even appear in the source until you unlock them for real.
21
22 Please be careful with automated requests; I'm not a massive company, and I can
23 only take so much traffic. Please be considerate so that everyone gets to play.
24
25 If you're curious about how Advent of Code works, it's running on some custom
26 Perl code. Other than a few integrations (auth, analytics, social media), I
27 built the whole thing myself, including the design, animations, prose, and all
28 of the puzzles.
29
30 The puzzles are most of the work; preparing a new calendar and a new set of
31 puzzles each year takes all of my free time for 4-5 months. A lot of effort
32 went into building this thing - I hope you're enjoying playing it as much as I
33 enjoyed making it for you!
34
35 If you'd like to hang out, I'm @ericwastl on Twitter.
36
37 - Eric Wastl
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 -->
89 <body>
90 <header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">38*</span></div></div><div><h1 class="title-event">&nbsp;<span class="title-event-wrap">{&apos;year&apos;:</span><a href="/2020">2020</a><span class="title-event-wrap">}</span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
91
92 <div id="sidebar">
93 <div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://jobs.americanexpress.com/technology" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">American Express</a> - We architect, code and ship software that makes us an essential part of our customers’ digital lives. Work with the latest tech and back the engineering community through open source. Find your place in tech on #TeamAmex.</div></div>
94 </div><!--/sidebar-->
95
96 <main>
97 <script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
98 <article class="day-desc"><h2>--- Day 19: Monster Messages ---</h2><p>You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves at the <span title="This is a purely fictional organization. Any resemblance to actual organizations, past or present, is purely coincidental.">Mythical Information Bureau</span> contact you again. They think their satellite has collected an image of a <em>sea monster</em>! Unfortunately, the connection to the satellite is having problems, and many of the messages sent back from the satellite have been corrupted.</p>
99 <p>They sent you a list of <em>the rules valid messages should obey</em> and a list of <em>received messages</em> they've collected so far (your puzzle input).</p>
100 <p>The <em>rules for valid messages</em> (the top part of your puzzle input) are numbered and build upon each other. For example:</p>
101 <pre><code>0: 1 2
102 1: "a"
103 2: 1 3 | 3 1
104 3: "b"
105 </code></pre>
106 <p>Some rules, like <code>3: "b"</code>, simply match a single character (in this case, <code>b</code>).</p>
107 <p>The remaining rules list the sub-rules that must be followed; for example, the rule <code>0: 1 2</code> means that to match rule <code>0</code>, the text being checked must match rule <code>1</code>, and the text after the part that matched rule <code>1</code> must then match rule <code>2</code>.</p>
108 <p>Some of the rules have multiple lists of sub-rules separated by a pipe (<code>|</code>). This means that <em>at least one</em> list of sub-rules must match. (The ones that match might be different each time the rule is encountered.) For example, the rule <code>2: 1 3 | 3 1</code> means that to match rule <code>2</code>, the text being checked must match rule <code>1</code> followed by rule <code>3</code> <em>or</em> it must match rule <code>3</code> followed by rule <code>1</code>.</p>
109 <p>Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since rule <code>1</code> matches <code>a</code> and rule <code>3</code> matches <code>b</code>, rule <code>2</code> matches either <code>ab</code> or <code>ba</code>. Therefore, rule <code>0</code> matches <code>aab</code> or <code>aba</code>.</p>
110 <p>Here's a more interesting example:</p>
111 <pre><code>0: 4 1 5
112 1: 2 3 | 3 2
113 2: 4 4 | 5 5
114 3: 4 5 | 5 4
115 4: "a"
116 5: "b"
117 </code></pre>
118 <p>Here, because rule <code>4</code> matches <code>a</code> and rule <code>5</code> matches <code>b</code>, rule <code>2</code> matches two letters that are the same (<code>aa</code> or <code>bb</code>), and rule <code>3</code> matches two letters that are different (<code>ab</code> or <code>ba</code>).</p>
119 <p>Since rule <code>1</code> matches rules <code>2</code> and <code>3</code> 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: <code>aaab</code>, <code>aaba</code>, <code>bbab</code>, <code>bbba</code>, <code>abaa</code>, <code>abbb</code>, <code>baaa</code>, or <code>babb</code>.</p>
120 <p>Rule <code>0</code>, therefore, matches <code>a</code> (rule <code>4</code>), then any of the eight options from rule <code>1</code>, then <code>b</code> (rule <code>5</code>): <code>aaaabb</code>, <code>aaabab</code>, <code>abbabb</code>, <code>abbbab</code>, <code>aabaab</code>, <code>aabbbb</code>, <code>abaaab</code>, or <code>ababbb</code>.</p>
121 <p>The <em>received messages</em> (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:</p>
122 <pre><code>0: 4 1 5
123 1: 2 3 | 3 2
124 2: 4 4 | 5 5
125 3: 4 5 | 5 4
126 4: "a"
127 5: "b"
128
129 ababbb
130 bababa
131 abbbab
132 aaabbb
133 aaaabbb
134 </code></pre>
135 <p>Your goal is to determine <em>the number of messages that completely match rule <code>0</code></em>. In the above example, <code>ababbb</code> and <code>abbbab</code> match, but <code>bababa</code>, <code>aaabbb</code>, and <code>aaaabbb</code> do not, producing the answer <em><code>2</code></em>. The whole message must match all of rule <code>0</code>; there can't be extra unmatched characters in the message. (For example, <code>aaaabbb</code> might appear to match rule <code>0</code> above, but it has an extra unmatched <code>b</code> on the end.)</p>
136 <p><em>How many messages completely match rule <code>0</code>?</em></p>
137 </article>
138 <p>Your puzzle answer was <code>235</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules <code>8: 42</code> and <code>11: 42 31</code> with the following:</p>
139 <pre><code>8: 42 | 42 8
140 11: 42 31 | 42 11 31
141 </code></pre>
142 <p>This small change has a big impact: now, the rules <em>do</em> 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.</p>
143 <p>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 <em>those</em> rules (especially rules <code>42</code> and <code>31</code>) are used by the new versions of rules <code>8</code> and <code>11</code>.</p>
144 <p>(Remember, <em>you only need to handle the rules you have</em>; building a solution that could handle any hypothetical combination of rules would be <a href="https://en.wikipedia.org/wiki/Formal_grammar" target="_blank">significantly more difficult</a>.)</p>
145 <p>For example:</p>
146 <pre><code>42: 9 14 | 10 1
147 9: 14 27 | 1 26
148 10: 23 14 | 28 1
149 1: "a"
150 11: 42 31
151 5: 1 14 | 15 1
152 19: 14 1 | 14 14
153 12: 24 14 | 19 1
154 16: 15 1 | 14 14
155 31: 14 17 | 1 13
156 6: 14 14 | 1 14
157 2: 1 24 | 14 4
158 0: 8 11
159 13: 14 3 | 1 12
160 15: 1 | 14
161 17: 14 2 | 1 7
162 23: 25 1 | 22 14
163 28: 16 1
164 4: 1 1
165 20: 14 14 | 1 15
166 3: 5 14 | 16 1
167 27: 1 6 | 14 18
168 14: "b"
169 21: 14 1 | 1 14
170 25: 1 1 | 1 14
171 22: 14 14
172 8: 42
173 26: 14 22 | 1 20
174 18: 15 15
175 7: 14 5 | 1 21
176 24: 14 1
177
178 abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
179 bbabbbbaabaabba
180 babbbbaabbbbbabbbbbbaabaaabaaa
181 aaabbbbbbaaaabaababaabababbabaaabbababababaaa
182 bbbbbbbaaaabbbbaaabbabaaa
183 bbbababbbbaaaaaaaabbababaaababaabab
184 ababaaaaaabaaab
185 ababaaaaabbbaba
186 baabbaaaabbaaaababbaababb
187 abbbbabbbbaaaababbbbbbaaaababb
188 aaaaabbaabaaaaababaa
189 aaaabbaaaabbaaa
190 aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
191 babaaabbbaaabaababbaabababaaab
192 aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
193 </code></pre>
194 <p>Without updating rules <code>8</code> and <code>11</code>, these rules only match three messages: <code>bbabbbbaabaabba</code>, <code>ababaaaaaabaaab</code>, and <code>ababaaaaabbbaba</code>.</p>
195 <p>However, after updating rules <code>8</code> and <code>11</code>, a total of <em><code>12</code></em> messages match:</p>
196 <ul>
197 <li><code>bbabbbbaabaabba</code></li>
198 <li><code>babbbbaabbbbbabbbbbbaabaaabaaa</code></li>
199 <li><code>aaabbbbbbaaaabaababaabababbabaaabbababababaaa</code></li>
200 <li><code>bbbbbbbaaaabbbbaaabbabaaa</code></li>
201 <li><code>bbbababbbbaaaaaaaabbababaaababaabab</code></li>
202 <li><code>ababaaaaaabaaab</code></li>
203 <li><code>ababaaaaabbbaba</code></li>
204 <li><code>baabbaaaabbaaaababbaababb</code></li>
205 <li><code>abbbbabbbbaaaababbbbbbaaaababb</code></li>
206 <li><code>aaaaabbaabaaaaababaa</code></li>
207 <li><code>aaaabbaabbaaaaaaabbbabbbaaabbaabaaa</code></li>
208 <li><code>aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba</code></li>
209 </ul>
210 <p><em>After updating rules <code>8</code> and <code>11</code>, how many messages completely match rule <code>0</code>?</em></p>
211 </article>
212 <p>Your puzzle answer was <code>379</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
213 <p>At this point, you should <a href="/2020">return to your Advent calendar</a> and try another puzzle.</p>
214 <p>If you still want to see it, you can <a href="19/input" target="_blank">get your puzzle input</a>.</p>
215 <p>You can also <span class="share">[Share<span class="share-content">on
216 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Monster+Messages%22+%2D+Day+19+%2D+Advent+of+Code+2020&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F19&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
217 <a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=I%27ve+completed+%22Monster+Messages%22+%2D+Day+19+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F19'}else{return false;}" target="_blank">Mastodon</a
218 ></span>]</span> this puzzle.</p>
219 </main>
220
221 <!-- ga -->
222 <script>
223 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
224 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
225 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
226 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
227 ga('create', 'UA-69522494-1', 'auto');
228 ga('set', 'anonymizeIp', true);
229 ga('send', 'pageview');
230 </script>
231 <!-- /ga -->
232 </body>
233 </html>