Tweaked some parsing code
[advent-of-code-21.git] / problems / day18.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 18 - Advent of Code 2021</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?26"/>
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="/2021/about">[About]</a></li><li><a href="/2021/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2021/settings">[Settings]</a></li><li><a href="/2021/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2021/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">36*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">sub y{</span><a href="/2021">2021</a><span class="title-event-wrap">}</span></h1><nav><ul><li><a href="/2021">[Calendar]</a></li><li><a href="/2021/support">[AoC++]</a></li><li><a href="/2021/sponsors">[Sponsors]</a></li><li><a href="/2021/leaderboard">[Leaderboard]</a></li><li><a href="/2021/stats">[Stats]</a></li></ul></nav></div></header>
91
92 <div id="sidebar">
93 <div id="sponsor"><div class="quiet">Our <a href="/2021/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://www.careers.philips.com/global/en" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Philips NV – DMIS</a> - Don&apos;t just challenge yourself, challenge the status quo</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 18: Snailfish ---</h2><p>You descend into the ocean trench and encounter some <a href="https://en.wikipedia.org/wiki/Snailfish" target="_blank">snailfish</a>. They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his <em><span title="Snailfish math is snailfish math!">math</span> homework</em>.</p>
99 <p>Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a <em>pair</em> - an ordered list of two elements. Each element of the pair can be either a regular number or another pair.</p>
100 <p>Pairs are written as <code>[x,y]</code>, where <code>x</code> and <code>y</code> are the elements within the pair. Here are some example snailfish numbers, one snailfish number per line:</p>
101 <pre><code>[1,2]
102 [[1,2],3]
103 [9,[8,7]]
104 [[1,9],[8,5]]
105 [[[[1,2],[3,4]],[[5,6],[7,8]]],9]
106 [[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
107 [[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]
108 </code></pre>
109 <p>This snailfish homework is about <em>addition</em>. To add two snailfish numbers, form a pair from the left and right parameters of the addition operator. For example, <code>[1,2]</code> + <code>[[3,4],5]</code> becomes <code>[[1,2],[[3,4],5]]</code>.</p>
110 <p>There's only one problem: <em>snailfish numbers must always be reduced</em>, and the process of adding two snailfish numbers can result in snailfish numbers that need to be reduced.</p>
111 <p>To <em>reduce a snailfish number</em>, you must repeatedly do the first action in this list that applies to the snailfish number:</p>
112 <ul>
113 <li>If any pair is <em>nested inside four pairs</em>, the leftmost such pair <em>explodes</em>.</li>
114 <li>If any regular number is <em>10 or greater</em>, the leftmost such regular number <em>splits</em>.</li>
115 </ul>
116 <p>Once no action in the above list applies, the snailfish number is reduced.</p>
117 <p>During reduction, at most one action applies, after which the process returns to the top of the list of actions. For example, if <em>split</em> produces a pair that meets the <em>explode</em> criteria, that pair <em>explodes</em> before other <em>splits</em> occur.</p>
118 <p>To <em>explode</em> a pair, the pair's left value is added to the first regular number to the left of the exploding pair (if any), and the pair's right value is added to the first regular number to the right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. Then, the entire exploding pair is replaced with the regular number <code>0</code>.</p>
119 <p>Here are some examples of a single explode action:</p>
120 <ul>
121 <li><code>[[[[<em>[9,8]</em>,1],2],3],4]</code> becomes <code>[[[[<em>0</em>,<em>9</em>],2],3],4]</code> (the <code>9</code> has no regular number to its left, so it is not added to any regular number).</li>
122 <li><code>[7,[6,[5,[4,<em>[3,2]</em>]]]]</code> becomes <code>[7,[6,[5,[<em>7</em>,<em>0</em>]]]]</code> (the <code>2</code> has no regular number to its right, and so it is not added to any regular number).</li>
123 <li><code>[[6,[5,[4,<em>[3,2]</em>]]],1]</code> becomes <code>[[6,[5,[<em>7</em>,<em>0</em>]]],<em>3</em>]</code>.</li>
124 <li><code>[[3,[2,[1,<em>[7,3]</em>]]],[6,[5,[4,[3,2]]]]]</code> becomes <code>[[3,[2,[<em>8</em>,<em>0</em>]]],[<em>9</em>,[5,[4,[3,2]]]]]</code> (the pair <code>[3,2]</code> is unaffected because the pair <code>[7,3]</code> is further to the left; <code>[3,2]</code> would explode on the next action).</li>
125 <li><code>[[3,[2,[8,0]]],[9,[5,[4,<em>[3,2]</em>]]]]</code> becomes <code>[[3,[2,[8,0]]],[9,[5,[<em>7</em>,<em>0</em>]]]]</code>.</li>
126 </ul>
127 <p>To <em>split</em> a regular number, replace it with a pair; the left element of the pair should be the regular number divided by two and rounded <em>down</em>, while the right element of the pair should be the regular number divided by two and rounded <em>up</em>. For example, <code>10</code> becomes <code>[5,5]</code>, <code>11</code> becomes <code>[5,6]</code>, <code>12</code> becomes <code>[6,6]</code>, and so on.</p>
128 <p>Here is the process of finding the reduced result of <code>[[[[4,3],4],4],[7,[[8,4],9]]]</code> + <code>[1,1]</code>:</p>
129 <pre><code>after addition: [[[[<em>[4,3]</em>,4],4],[7,[[8,4],9]]],[1,1]]
130 after explode: [[[[0,7],4],[7,[<em>[8,4]</em>,9]]],[1,1]]
131 after explode: [[[[0,7],4],[<em>15</em>,[0,13]]],[1,1]]
132 after split: [[[[0,7],4],[[7,8],[0,<em>13</em>]]],[1,1]]
133 after split: [[[[0,7],4],[[7,8],[0,<em>[6,7]</em>]]],[1,1]]
134 after explode: [[[[0,7],4],[[7,8],[6,0]]],[8,1]]
135 </code></pre>
136 <p>Once no reduce actions apply, the snailfish number that remains is the actual result of the addition operation: <code>[[[[0,7],4],[[7,8],[6,0]]],[8,1]]</code>.</p>
137 <p>The homework assignment involves adding up a <em>list of snailfish numbers</em> (your puzzle input). The snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, then add that result and the third, then add that result and the fourth, and so on until all numbers in the list have been used once.</p>
138 <p>For example, the final sum of this list is <code>[[[[1,1],[2,2]],[3,3]],[4,4]]</code>:</p>
139 <pre><code>[1,1]
140 [2,2]
141 [3,3]
142 [4,4]
143 </code></pre>
144 <p>The final sum of this list is <code>[[[[3,0],[5,3]],[4,4]],[5,5]]</code>:</p>
145 <pre><code>[1,1]
146 [2,2]
147 [3,3]
148 [4,4]
149 [5,5]
150 </code></pre>
151 <p>The final sum of this list is <code>[[[[5,0],[7,4]],[5,5]],[6,6]]</code>:</p>
152 <pre><code>[1,1]
153 [2,2]
154 [3,3]
155 [4,4]
156 [5,5]
157 [6,6]
158 </code></pre>
159 <p>Here's a slightly larger example:</p>
160 <pre><code>[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
161 [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
162 [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
163 [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
164 [7,[5,[[3,8],[1,4]]]]
165 [[2,[2,2]],[8,[8,1]]]
166 [2,9]
167 [1,[[[9,3],9],[[9,0],[0,7]]]]
168 [[[5,[7,4]],7],1]
169 [[[[4,2],2],6],[8,7]]
170 </code></pre>
171 <p>The final sum <code>[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]</code> is found after adding up the above snailfish numbers:</p>
172 <pre><code> [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
173 + [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
174 = [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
175
176 [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
177 + [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
178 = [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
179
180 [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
181 + [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
182 = [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
183
184 [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
185 + [7,[5,[[3,8],[1,4]]]]
186 = [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
187
188 [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
189 + [[2,[2,2]],[8,[8,1]]]
190 = [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
191
192 [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
193 + [2,9]
194 = [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
195
196 [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
197 + [1,[[[9,3],9],[[9,0],[0,7]]]]
198 = [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
199
200 [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
201 + [[[5,[7,4]],7],1]
202 = [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
203
204 [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
205 + [[[[4,2],2],6],[8,7]]
206 = [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]
207 </code></pre>
208 <p>To check whether it's the right answer, the snailfish teacher only checks the <em>magnitude</em> of the final sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude of its right element. The magnitude of a regular number is just that number.</p>
209 <p>For example, the magnitude of <code>[9,1]</code> is <code>3*9 + 2*1 = <em>29</em></code>; the magnitude of <code>[1,9]</code> is <code>3*1 + 2*9 = <em>21</em></code>. Magnitude calculations are recursive: the magnitude of <code>[[9,1],[1,9]]</code> is <code>3*29 + 2*21 = <em>129</em></code>.</p>
210 <p>Here are a few more magnitude examples:</p>
211 <ul>
212 <li><code>[[1,2],[[3,4],5]]</code> becomes <code><em>143</em></code>.</li>
213 <li><code>[[[[0,7],4],[[7,8],[6,0]]],[8,1]]</code> becomes <code><em>1384</em></code>.</li>
214 <li><code>[[[[1,1],[2,2]],[3,3]],[4,4]]</code> becomes <code><em>445</em></code>.</li>
215 <li><code>[[[[3,0],[5,3]],[4,4]],[5,5]]</code> becomes <code><em>791</em></code>.</li>
216 <li><code>[[[[5,0],[7,4]],[5,5]],[6,6]]</code> becomes <code><em>1137</em></code>.</li>
217 <li><code>[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]</code> becomes <code><em>3488</em></code>.</li>
218 </ul>
219 <p>So, given this example homework assignment:</p>
220 <pre><code>[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
221 [[[5,[2,8]],4],[5,[[9,9],0]]]
222 [6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
223 [[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
224 [[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
225 [[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
226 [[[[5,4],[7,7]],8],[[8,3],8]]
227 [[9,3],[[9,9],[6,[4,9]]]]
228 [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
229 [[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
230 </code></pre>
231 <p>The final sum is:</p>
232 <pre><code>[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]]</code></pre>
233 <p>The magnitude of this final sum is <code><em>4140</em></code>.</p>
234 <p>Add up all of the snailfish numbers from the homework assignment in the order they appear. <em>What is the magnitude of the final sum?</em></p>
235 </article>
236 <p>Your puzzle answer was <code>2501</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>You notice a second question on the back of the homework assignment:</p>
237 <p>What is the largest magnitude you can get from adding only two of the snailfish numbers?</p>
238 <p>Note that snailfish addition is not <a href="https://en.wikipedia.org/wiki/Commutative_property" target="_blank">commutative</a> - that is, <code>x + y</code> and <code>y + x</code> can produce different results.</p>
239 <p>Again considering the last example homework assignment above:</p>
240 <pre><code>[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
241 [[[5,[2,8]],4],[5,[[9,9],0]]]
242 [6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
243 [[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
244 [[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
245 [[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
246 [[[[5,4],[7,7]],8],[[8,3],8]]
247 [[9,3],[[9,9],[6,[4,9]]]]
248 [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
249 [[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
250 </code></pre>
251 <p>The largest magnitude of the sum of any two snailfish numbers in this list is <code><em>3993</em></code>. This is the magnitude of <code>[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]</code> + <code>[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]</code>, which reduces to <code>[[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]]</code>.</p>
252 <p><em>What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?</em></p>
253 </article>
254 <p>Your puzzle answer was <code>4935</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
255 <p>At this point, you should <a href="/2021">return to your Advent calendar</a> and try another puzzle.</p>
256 <p>If you still want to see it, you can <a href="18/input" target="_blank">get your puzzle input</a>.</p>
257 <p>You can also <span class="share">[Share<span class="share-content">on
258 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Snailfish%22+%2D+Day+18+%2D+Advent+of+Code+2021&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F18&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
259 <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+%22Snailfish%22+%2D+Day+18+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F18'}else{return false;}" target="_blank">Mastodon</a
260 ></span>]</span> this puzzle.</p>
261 </main>
262
263 <!-- ga -->
264 <script>
265 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
266 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
267 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
268 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
269 ga('create', 'UA-69522494-1', 'auto');
270 ga('set', 'anonymizeIp', true);
271 ga('send', 'pageview');
272 </script>
273 <!-- /ga -->
274 </body>
275 </html>