Done day 22
[advent-of-code-21.git] / problems / day20.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 20 - 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">40*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">var 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://jb.gg/AoC2021tips" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">JetBrains</a> - Get ready to jingle with Advent of Code in Kotlin! Have fun, learn new things, and win prizes. Believe in magic with Kotlin. Happy holidays! https://jb.gg/AoC</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 20: Trench Map ---</h2><p>With the scanners fully deployed, you turn their attention to mapping the floor of the ocean trench.</p>
99 <p>When you get back the image from the scanners, it seems to just be random noise. Perhaps you can combine an image enhancement algorithm and the input image (your puzzle input) to clean it up a little.</p>
100 <p>For example:</p>
101 <pre><code>..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
102 #..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###
103 .######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.
104 .#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....
105 .#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..
106 ...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....
107 ..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
108
109 #..#.
110 #....
111 ##..#
112 ..#..
113 ..###
114 </code></pre>
115 <p>The first section is the <em>image enhancement algorithm</em>. It is normally given on a single line, but it has been wrapped to multiple lines in this example for legibility. The second section is the <em>input image</em>, a two-dimensional grid of <em>light pixels</em> (<code>#</code>) and <em>dark pixels</em> (<code>.</code>).</p>
116 <p>The image enhancement algorithm describes how to enhance an image by <em>simultaneously</em> converting all pixels in the input image into an output image. Each pixel of the output image is determined by looking at a 3x3 square of pixels centered on the corresponding input image pixel. So, to determine the value of the pixel at (5,10) in the output image, nine pixels from the input image need to be considered: (4,9), (4,10), (4,11), (5,9), (5,10), (5,11), (6,9), (6,10), and (6,11). These nine input pixels are combined into a single binary number that is used as an index in the <em>image enhancement algorithm</em> string.</p>
117 <p>For example, to determine the output pixel that corresponds to the very middle pixel of the input image, the nine pixels marked by <code>[...]</code> would need to be considered:</p>
118 <pre><code># . . # .
119 #[. . .].
120 #[# . .]#
121 .[. # .].
122 . . # # #
123 </code></pre>
124 <p>Starting from the top-left and reading across each row, these pixels are <code>...</code>, then <code>#..</code>, then <code>.#.</code>; combining these forms <code>...#...#.</code>. By turning dark pixels (<code>.</code>) into <code>0</code> and light pixels (<code>#</code>) into <code>1</code>, the binary number <code>000100010</code> can be formed, which is <code>34</code> in decimal.</p>
125 <p>The image enhancement algorithm string is exactly 512 characters long, enough to match every possible 9-bit binary number. The first few characters of the string (numbered starting from zero) are as follows:</p>
126 <pre><code>0 10 20 30 <em>34</em> 40 50 60 70
127 | | | | <em>|</em> | | | |
128 ..#.#..#####.#.#.#.###.##.....###.<em>#</em>#.#..###.####..#####..#....#..#..##..##
129 </code></pre>
130 <p>In the middle of this first group of characters, the character at index 34 can be found: <code>#</code>. So, the output pixel in the center of the output image should be <code>#</code>, a <em>light pixel</em>.</p>
131 <p>This process can then be repeated to calculate every pixel of the output image.</p>
132 <p>Through advances in imaging technology, the images being operated on here are <em>infinite</em> in size. <em>Every</em> pixel of the infinite output image needs to be calculated exactly based on the relevant pixels of the input image. The small input image you have is only a small region of the actual infinite input image; the rest of the input image consists of dark pixels (<code>.</code>). For the purposes of the example, to save on space, only a portion of the infinite-sized input and output images will be shown.</p>
133 <p>The starting input image, therefore, looks something like this, with more dark pixels (<code>.</code>) extending forever in every direction not shown here:</p>
134 <pre><code>...............
135 ...............
136 ...............
137 ...............
138 ...............
139 .....#..#......
140 .....#.........
141 .....##..#.....
142 .......#.......
143 .......###.....
144 ...............
145 ...............
146 ...............
147 ...............
148 ...............
149 </code></pre>
150 <p>By applying the image enhancement algorithm to every pixel simultaneously, the following output image can be obtained:</p>
151 <pre><code>...............
152 ...............
153 ...............
154 ...............
155 .....##.##.....
156 ....#..#.#.....
157 ....##.#..#....
158 ....####..#....
159 .....#..##.....
160 ......##..#....
161 .......#.#.....
162 ...............
163 ...............
164 ...............
165 ...............
166 </code></pre>
167 <p>Through further advances in imaging technology, the above output image can also be used as an input image! This allows it to be enhanced <em>a second time</em>:</p>
168 <pre><code>...............
169 ...............
170 ...............
171 ..........#....
172 ....#..#.#.....
173 ...#.#...###...
174 ...#...##.#....
175 ...#.....#.#...
176 ....#.#####....
177 .....#.#####...
178 ......##.##....
179 .......###.....
180 ...............
181 ...............
182 ...............
183 </code></pre>
184 <p>Truly incredible - now the small details are really starting to come through. After enhancing the original input image twice, <code><em>35</em></code> pixels are lit.</p>
185 <p>Start with the original input image and apply the image enhancement algorithm twice, being careful to account for the infinite size of the images. <em>How many pixels are lit in the resulting image?</em></p>
186 </article>
187 <p>Your puzzle answer was <code>5225</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>You still can't quite make out the details in the image. Maybe you just didn't <a href="https://en.wikipedia.org/wiki/Kernel_(image_processing)" target="_blank">enhance</a> it <span title="Yeah, that's definitely the problem.">enough</span>.</p>
188 <p>If you enhance the starting input image in the above example a total of <em>50</em> times, <code><em>3351</em></code> pixels are lit in the final output image.</p>
189 <p>Start again with the original input image and apply the image enhancement algorithm 50 times. <em>How many pixels are lit in the resulting image?</em></p>
190 </article>
191 <p>Your puzzle answer was <code>18131</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
192 <p>At this point, you should <a href="/2021">return to your Advent calendar</a> and try another puzzle.</p>
193 <p>If you still want to see it, you can <a href="20/input" target="_blank">get your puzzle input</a>.</p>
194 <p>You can also <span class="share">[Share<span class="share-content">on
195 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Trench+Map%22+%2D+Day+20+%2D+Advent+of+Code+2021&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F20&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
196 <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+%22Trench+Map%22+%2D+Day+20+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F20'}else{return false;}" target="_blank">Mastodon</a
197 ></span>]</span> this puzzle.</p>
198 </main>
199
200 <!-- ga -->
201 <script>
202 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
203 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
204 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
205 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
206 ga('create', 'UA-69522494-1', 'auto');
207 ga('set', 'anonymizeIp', true);
208 ga('send', 'pageview');
209 </script>
210 <!-- /ga -->
211 </body>
212 </html>