Done day 21
[advent-of-code-20.git] / problems / day14.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 14 - 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">28*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">/*</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://github.com/" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">GitHub</a> - We&apos;re hiring engineers to make GitHub fast. Interested? Email fast@github.com with details of exceptional performance work you&apos;ve done in the past.</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 14: Docking Data ---</h2><p>As your ferry approaches the sea port, the captain asks for your help again. The computer system that runs this port isn't compatible with the docking program on the ferry, so the docking parameters aren't being correctly initialized in the docking program's memory.</p>
99 <p>After a brief inspection, you discover that the sea port's computer system uses a strange <a href="https://en.wikipedia.org/wiki/Mask_(computing)" target="_blank">bitmask</a> system in its initialization program. Although you don't have the correct decoder chip handy, you can emulate it in software!</p>
100 <p>The initialization program (your puzzle input) can either update the bitmask or write a value to memory. Values and memory addresses are both 36-bit unsigned integers. For example, ignoring bitmasks for a moment, a line like <code>mem[8] = 11</code> would write the value <code>11</code> to memory address <code>8</code>.</p>
101 <p>The bitmask is always given as a string of 36 bits, written with the most significant bit (representing <code>2^35</code>) on the left and the least significant bit (<code>2^0</code>, that is, the <code>1</code>s bit) on the right. The current bitmask is applied to values immediately before they are written to memory: a <code>0</code> or <code>1</code> overwrites the corresponding bit in the value, while an <code>X</code> leaves the bit in the value unchanged.</p>
102 <p>For example, consider the following program:</p>
103 <pre><code>mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
104 mem[8] = 11
105 mem[7] = 101
106 mem[8] = 0
107 </code></pre>
108 <p>This program starts by specifying a bitmask (<code>mask = ....</code>). The mask it specifies will overwrite two bits in every written value: the <code>2</code>s bit is overwritten with <code>0</code>, and the <code>64</code>s bit is overwritten with <code>1</code>.</p>
109 <p>The program then attempts to write the value <code>11</code> to memory address <code>8</code>. By expanding everything out to individual bits, the mask is applied as follows:</p>
110 <pre><code>value: 000000000000000000000000000000001011 (decimal 11)
111 mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
112 result: 00000000000000000000000000000<em>1</em>0010<em>0</em>1 (decimal 73)
113 </code></pre>
114 <p>So, because of the mask, the value <code>73</code> is written to memory address <code>8</code> instead. Then, the program tries to write <code>101</code> to address <code>7</code>:</p>
115 <pre><code>value: 000000000000000000000000000001100101 (decimal 101)
116 mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
117 result: 00000000000000000000000000000<em>1</em>1001<em>0</em>1 (decimal 101)
118 </code></pre>
119 <p>This time, the mask has no effect, as the bits it overwrote were already the values the mask tried to set. Finally, the program tries to write <code>0</code> to address <code>8</code>:</p>
120 <pre><code>value: 000000000000000000000000000000000000 (decimal 0)
121 mask: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
122 result: 00000000000000000000000000000<em>1</em>0000<em>0</em>0 (decimal 64)
123 </code></pre>
124 <p><code>64</code> is written to address <code>8</code> instead, overwriting the value that was there previously.</p>
125 <p>To initialize your ferry's docking program, you need the sum of all values left in memory after the initialization program completes. (The entire 36-bit address space begins initialized to the value <code>0</code> at every address.) In the above example, only two values in memory are not zero - <code>101</code> (at address <code>7</code>) and <code>64</code> (at address <code>8</code>) - producing a sum of <em><code>165</code></em>.</p>
126 <p>Execute the initialization program. <em>What is the sum of all values left in memory after it completes?</em> (Do not truncate the sum to 36 bits.)</p>
127 </article>
128 <p>Your puzzle answer was <code>15403588588538</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using <em>version 2</em> of the decoder chip!</p>
129 <p>A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a <a href="https://www.youtube.com/watch?v=PvfhANgLrm4" target="_blank">memory address decoder</a>. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination <em>memory address</em> in the following way:</p>
130 <ul>
131 <li>If the bitmask bit is <code>0</code>, the corresponding memory address bit is <em>unchanged</em>.</li>
132 <li>If the bitmask bit is <code>1</code>, the corresponding memory address bit is <em>overwritten with <code>1</code></em>.</li>
133 <li>If the bitmask bit is <code>X</code>, the corresponding memory address bit is <span title="Technically, since you're on a boat, they're all floating."><em>floating</em></span>.</li>
134 </ul>
135 <p>A <em>floating</em> bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on <em>all possible values</em>, potentially causing many memory addresses to be written all at once!</p>
136 <p>For example, consider the following program:</p>
137 <pre><code>mask = 000000000000000000000000000000X1001X
138 mem[42] = 100
139 mask = 00000000000000000000000000000000X0XX
140 mem[26] = 1
141 </code></pre>
142 <p>When this program goes to write to memory address <code>42</code>, it first applies the bitmask:</p>
143 <pre><code>address: 000000000000000000000000000000101010 (decimal 42)
144 mask: 000000000000000000000000000000X1001X
145 result: 000000000000000000000000000000<em>X1</em>10<em>1X</em>
146 </code></pre>
147 <p>After applying the mask, four bits are overwritten, three of which are different, and two of which are <em>floating</em>. Floating bits take on every possible combination of values; with two floating bits, four actual memory addresses are written:</p>
148 <pre><code>000000000000000000000000000000<em>0</em>1101<em>0</em> (decimal 26)
149 000000000000000000000000000000<em>0</em>1101<em>1</em> (decimal 27)
150 000000000000000000000000000000<em>1</em>1101<em>0</em> (decimal 58)
151 000000000000000000000000000000<em>1</em>1101<em>1</em> (decimal 59)
152 </code></pre>
153 <p>Next, the program is about to write to memory address <code>26</code> with a different bitmask:</p>
154 <pre><code>address: 000000000000000000000000000000011010 (decimal 26)
155 mask: 00000000000000000000000000000000X0XX
156 result: 00000000000000000000000000000001<em>X</em>0<em>XX</em>
157 </code></pre>
158 <p>This results in an address with three floating bits, causing writes to <em>eight</em> memory addresses:</p>
159 <pre><code>00000000000000000000000000000001<em>0</em>0<em>00</em> (decimal 16)
160 00000000000000000000000000000001<em>0</em>0<em>01</em> (decimal 17)
161 00000000000000000000000000000001<em>0</em>0<em>10</em> (decimal 18)
162 00000000000000000000000000000001<em>0</em>0<em>11</em> (decimal 19)
163 00000000000000000000000000000001<em>1</em>0<em>00</em> (decimal 24)
164 00000000000000000000000000000001<em>1</em>0<em>01</em> (decimal 25)
165 00000000000000000000000000000001<em>1</em>0<em>10</em> (decimal 26)
166 00000000000000000000000000000001<em>1</em>0<em>11</em> (decimal 27)
167 </code></pre>
168 <p>The entire 36-bit address space still begins initialized to the value 0 at every address, and you still need the sum of all values left in memory at the end of the program. In this example, the sum is <em><code>208</code></em>.</p>
169 <p>Execute the initialization program using an emulator for a version 2 decoder chip. <em>What is the sum of all values left in memory after it completes?</em></p>
170 </article>
171 <p>Your puzzle answer was <code>3260587250457</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
172 <p>At this point, you should <a href="/2020">return to your Advent calendar</a> and try another puzzle.</p>
173 <p>If you still want to see it, you can <a href="14/input" target="_blank">get your puzzle input</a>.</p>
174 <p>You can also <span class="share">[Share<span class="share-content">on
175 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Docking+Data%22+%2D+Day+14+%2D+Advent+of+Code+2020&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F14&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
176 <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+%22Docking+Data%22+%2D+Day+14+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F14'}else{return false;}" target="_blank">Mastodon</a
177 ></span>]</span> this puzzle.</p>
178 </main>
179
180 <!-- ga -->
181 <script>
182 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
183 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
184 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
185 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
186 ga('create', 'UA-69522494-1', 'auto');
187 ga('set', 'anonymizeIp', true);
188 ga('send', 'pageview');
189 </script>
190 <!-- /ga -->
191 </body>
192 </html>