Taking advantage of a neat trick for using $ rather than a lambda
[advent-of-code-17.git] / problems / day22.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 22 - Advent of Code 2017</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?12"/>
9 <link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
10 <link rel="shortcut icon" href="/favicon.ico?2"/>
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 Google, and I can only take
23 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, ads, social media),
27 I built the whole thing myself, including the design, animations, prose, and
28 all of the puzzles.
29
30 The puzzles probably took the longest; the easiest ones took an hour or two
31 each, but the harder ones took 4-5 hours, and a few even longer than that. A
32 lot of effort went into building this thing - I hope you're enjoying playing it
33 as much as I 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="/2017/about">[About]</a></li><li><a href="/2017/support">[AoC++]</a></li><li><a href="/2017/events">[Events]</a></li><li><a href="/2017/settings">[Settings]</a></li><li><a href="/2017/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <span class="supporter">(AoC++)</span> <span class="star-count">44*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">0xffff&amp;</span><a href="/2017">2017</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2017">[Calendar]</a></li><li><a href="/2017/leaderboard">[Leaderboard]</a></li><li><a href="/2017/stats">[Stats]</a></li><li><a href="/2017/sponsors">[Sponsors]</a></li></ul></nav></div></header>
91
92 <div id="sidebar">
93 <div id="sponsor"><div class="quiet">Our <a href="/2017/sponsors">sponsors</a> help make Advent of Code possible:</div><p><a href="http://www.novetta.com/careers?utm_campaign=Advent%20of%20Code%202017&amp;utm_source=www.adventofcode.com&amp;utm_medium=ad" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);" rel="noopener">Novetta</a> - Unleash your imagination. Innovate at Novetta.</p></div>
94 <p class="quiet">By popular demand, there are now AoC-themed objects available (until Jan. 3rd)! Get them shipped <a href="https://teespring.com/advent-of-code" target="_blank">from the US</a> or <a href="https://teespring.com/advent-of-code-eu" target="_blank">from Europe</a>.</p>
95
96 </div><!--/sidebar-->
97
98 <main>
99 <article class="day-desc"><h2>--- Day 22: Sporifica Virus ---</h2><p>Diagnostics indicate that the local <em>grid computing cluster</em> has been contaminated with the <em>Sporifica Virus</em>. The grid computing cluster is a seemingly-<span title="The infinite is possible at AdventOfCodeCom.">infinite</span> two-dimensional grid of compute nodes. Each node is either <em>clean</em> or <em>infected</em> by the virus.<p>
100 <p>To <a href="https://en.wikipedia.org/wiki/Morris_worm#The_mistake">prevent overloading</a> the nodes (which would render them useless to the virus) or detection by system administrators, exactly one <em>virus carrier</em> moves through the network, infecting or cleaning nodes as it moves. The virus carrier is always located on a single node in the network (the <em>current node</em>) and keeps track of the <em>direction</em> it is facing.</p>
101 <p>To avoid detection, the virus carrier works in bursts; in each burst, it <em>wakes up</em>, does some <em>work</em>, and goes back to <em>sleep</em>. The following steps are all executed <em>in order</em> one time each burst:</p>
102 <ul>
103 <li>If the <em>current node</em> is <em>infected</em>, it turns to its <em>right</em>. Otherwise, it turns to its <em>left</em>. (Turning is done in-place; the <em>current node</em> does not change.)</li>
104 <li>If the <em>current node</em> is <em>clean</em>, it becomes <em>infected</em>. Otherwise, it becomes <em>cleaned</em>. (This is done <em>after</em> the node is considered for the purposes of changing direction.)</li>
105 <li>The virus carrier <a href="https://www.youtube.com/watch?v=2vj37yeQQHg">moves</a> <em>forward</em> one node in the direction it is facing.</li>
106 </ul>
107 <p>Diagnostics have also provided a <em>map of the node infection status</em> (your puzzle input). <em>Clean</em> nodes are shown as <code>.</code>; <em>infected</em> nodes are shown as <code>#</code>. This map only shows the center of the grid; there are many more nodes beyond those shown, but none of them are currently infected.</p>
108 <p>The virus carrier begins in the middle of the map facing <em>up</em>.</p>
109 <p>For example, suppose you are given a map like this:</p>
110 <pre><code>..#
111 #..
112 ...
113 </code></pre>
114 <p>Then, the middle of the infinite grid looks like this, with the virus carrier's position marked with <code>[ ]</code>:</p>
115 <pre><code>. . . . . . . . .
116 . . . . . . . . .
117 . . . . . . . . .
118 . . . . . # . . .
119 . . . #[.]. . . .
120 . . . . . . . . .
121 . . . . . . . . .
122 . . . . . . . . .
123 </code></pre>
124 <p>The virus carrier is on a <em>clean</em> node, so it turns <em>left</em>, <em>infects</em> the node, and moves left:</p>
125 <pre><code>. . . . . . . . .
126 . . . . . . . . .
127 . . . . . . . . .
128 . . . . . # . . .
129 . . .[#]# . . . .
130 . . . . . . . . .
131 . . . . . . . . .
132 . . . . . . . . .
133 </code></pre>
134 <p>The virus carrier is on an <em>infected</em> node, so it turns <em>right</em>, <em>cleans</em> the node, and moves up:</p>
135 <pre><code>. . . . . . . . .
136 . . . . . . . . .
137 . . . . . . . . .
138 . . .[.]. # . . .
139 . . . . # . . . .
140 . . . . . . . . .
141 . . . . . . . . .
142 . . . . . . . . .
143 </code></pre>
144 <p>Four times in a row, the virus carrier finds a <em>clean</em>, <em>infects</em> it, turns <em>left</em>, and moves forward, ending in the same place and still facing up:</p>
145 <pre><code>. . . . . . . . .
146 . . . . . . . . .
147 . . . . . . . . .
148 . . #[#]. # . . .
149 . . # # # . . . .
150 . . . . . . . . .
151 . . . . . . . . .
152 . . . . . . . . .
153 </code></pre>
154 <p>Now on the same node as before, it sees an infection, which causes it to turn <em>right</em>, <em>clean</em> the node, and move forward:</p>
155 <pre><code>. . . . . . . . .
156 . . . . . . . . .
157 . . . . . . . . .
158 . . # .[.]# . . .
159 . . # # # . . . .
160 . . . . . . . . .
161 . . . . . . . . .
162 . . . . . . . . .
163 </code></pre>
164 <p>After the above actions, a total of <code>7</code> bursts of activity had taken place. Of them, <code>5</code> bursts of activity caused an infection.</p>
165 <p>After a total of <code>70</code>, the grid looks like this, with the virus carrier facing up:</p>
166 <pre><code>. . . . . # # . .
167 . . . . # . . # .
168 . . . # . . . . #
169 . . # . #[.]. . #
170 . . # . # . . # .
171 . . . . . # # . .
172 . . . . . . . . .
173 . . . . . . . . .
174 </code></pre>
175 <p>By this time, <code>41</code> bursts of activity caused an infection (though most of those nodes have since been cleaned).</p>
176 <p>After a total of <code>10000</code> bursts of activity, <code>5587</code> bursts will have caused an infection.</p>
177 <p>Given your actual map, after <code>10000</code> bursts of activity, <em>how many bursts cause a node to become infected</em>? (Do not count nodes that begin infected.)</p>
178 </article>
179 <p>Your puzzle answer was <code>5182</code>.</p><article class="day-desc"><h2>--- Part Two ---</h2><p>As you go to remove the virus from the infected nodes, it <em>evolves</em> to resist your attempt.</p>
180 <p>Now, before it infects a clean node, it will <em>weaken</em> it to disable your defenses. If it encounters an infected node, it will instead <em>flag</em> the node to be cleaned in the future. So:</p>
181 <ul>
182 <li><em>Clean</em> nodes become <em>weakened</em>.</li>
183 <li><em>Weakened</em> nodes become <em>infected</em>.</li>
184 <li><em>Infected</em> nodes become <em>flagged</em>.</li>
185 <li><em>Flagged</em> nodes become <em>clean</em>.</li>
186 </ul>
187 <p>Every node is always in exactly one of the above states.</p>
188 <p>The virus carrier still functions in a similar way, but now uses the following logic during its bursts of action:</p>
189 <ul>
190 <li>Decide which way to turn based on the <em>current node</em>:
191 <ul>
192 <li>If it is <em>clean</em>, it turns <em>left</em>.</li>
193 <li>If it is <em>weakened</em>, it does <em>not</em> turn, and will continue moving in the same direction.</li>
194 <li>If it is <em>infected</em>, it turns <em>right</em>.</li>
195 <li>If it is <em>flagged</em>, it <em>reverses</em> direction, and will go back the way it came.</li>
196 </ul>
197 </li>
198 <li>Modify the state of the <em>current node</em>, as described above.</li>
199 <li>The virus carrier moves <em>forward</em> one node in the direction it is facing.</li>
200 </ul>
201 <p>Start with the same map (still using <code>.</code> for <em>clean</em> and <code>#</code> for infected) and still with the virus carrier starting in the middle and facing <em>up</em>.</p>
202 <p>Using the same initial state as the previous example, and drawing <em>weakened</em> as <code>W</code> and <em>flagged</em> as <code>F</code>, the middle of the infinite grid looks like this, with the virus carrier's position again marked with <code>[ ]</code>:</p>
203 <pre><code>. . . . . . . . .
204 . . . . . . . . .
205 . . . . . . . . .
206 . . . . . # . . .
207 . . . #[.]. . . .
208 . . . . . . . . .
209 . . . . . . . . .
210 . . . . . . . . .
211 </code></pre>
212 <p>This is the same as before, since no initial nodes are <em>weakened</em> or <em>flagged</em>. The virus carrier is on a clean node, so it still turns left, instead <em>weakens</em> the node, and moves left:</p>
213 <pre><code>. . . . . . . . .
214 . . . . . . . . .
215 . . . . . . . . .
216 . . . . . # . . .
217 . . .[#]W . . . .
218 . . . . . . . . .
219 . . . . . . . . .
220 . . . . . . . . .
221 </code></pre>
222 <p>The virus carrier is on an infected node, so it still turns right, instead <em>flags</em> the node, and moves up:</p>
223 <pre><code>. . . . . . . . .
224 . . . . . . . . .
225 . . . . . . . . .
226 . . .[.]. # . . .
227 . . . F W . . . .
228 . . . . . . . . .
229 . . . . . . . . .
230 . . . . . . . . .
231 </code></pre>
232 <p>This process repeats three more times, ending on the previously-flagged node and facing right:</p>
233 <pre><code>. . . . . . . . .
234 . . . . . . . . .
235 . . . . . . . . .
236 . . W W . # . . .
237 . . W[F]W . . . .
238 . . . . . . . . .
239 . . . . . . . . .
240 . . . . . . . . .
241 </code></pre>
242 <p>Finding a flagged node, it reverses direction and <em>cleans</em> the node:</p>
243 <pre><code>. . . . . . . . .
244 . . . . . . . . .
245 . . . . . . . . .
246 . . W W . # . . .
247 . .[W]. W . . . .
248 . . . . . . . . .
249 . . . . . . . . .
250 . . . . . . . . .
251 </code></pre>
252 <p>The <em>weakened</em> node becomes infected, and it continues in the same direction:</p>
253 <pre><code>. . . . . . . . .
254 . . . . . . . . .
255 . . . . . . . . .
256 . . W W . # . . .
257 .[.]# . W . . . .
258 . . . . . . . . .
259 . . . . . . . . .
260 . . . . . . . . .
261 </code></pre>
262 <p>Of the first <code>100</code> bursts, <code>26</code> will result in <em>infection</em>. Unfortunately, another feature of this evolved virus is <em>speed</em>; of the first <code>10000000</code> bursts, <code>2511944</code> will result in <em>infection</em>.</p>
263 <p>Given your actual map, after <code>10000000</code> bursts of activity, <em>how many bursts cause a node to become infected</em>? (Do not count nodes that begin infected.)</p>
264 </article>
265 <p>Your puzzle answer was <code>2512008</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
266 <p>At this point, you should <a href="/2017">return to your advent calendar</a> and try another puzzle.</p>
267 <p>If you still want to see it, you can <a href="22/input" target="_blank">get your puzzle input</a>.</p>
268 <p>You can also <span class="share">[Share<span class="share-content">on
269 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Sporifica+Virus%22+%2D+Day+22+%2D+Advent+of+Code+2017&amp;url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F22&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
270 <a href="https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F22" target="_blank">Google+</a>
271 <a href="http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F22&amp;title=I%27ve+completed+%22Sporifica+Virus%22+%2D+Day+22+%2D+Advent+of+Code+2017" target="_blank">Reddit</a
272 ></span>]</span> this puzzle.</p>
273 </main>
274
275 <!-- ga -->
276 <script>
277 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
278 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
279 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
280 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
281 ga('create', 'UA-69522494-1', 'auto');
282 ga('send', 'pageview');
283 </script>
284 <!-- /ga -->
285 </body>
286 </html>