Tweaked some parsing code
[advent-of-code-21.git] / problems / day07.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 7 - 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">14*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">/^</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://getsturdy.com/aoc" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Sturdy</a> - Real-time version control for code. Watch us live!</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 7: The Treachery of Whales ---</h2><p>A giant <a href="https://en.wikipedia.org/wiki/Sperm_whale" target="_blank">whale</a> has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!</p>
99 <p>Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a <em>massive underground cave system</em> just beyond where they're aiming!</p>
100 <p>The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?</p>
101 <p>There's one major catch - crab submarines can only move horizontally.</p>
102 <p>You quickly make a list of <em>the horizontal position of each crab</em> (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.</p>
103 <p>For example, consider the following horizontal positions:</p>
104 <pre><code>16,1,2,0,4,2,7,1,2,14</code></pre>
105 <p>This means there's a crab with horizontal position <code>16</code>, a crab with horizontal position <code>1</code>, and so on.</p>
106 <p>Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position <code>2</code>:</p>
107 <ul>
108 <li>Move from <code>16</code> to <code>2</code>: <code>14</code> fuel</li>
109 <li>Move from <code>1</code> to <code>2</code>: <code>1</code> fuel</li>
110 <li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
111 <li>Move from <code>0</code> to <code>2</code>: <code>2</code> fuel</li>
112 <li>Move from <code>4</code> to <code>2</code>: <code>2</code> fuel</li>
113 <li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
114 <li>Move from <code>7</code> to <code>2</code>: <code>5</code> fuel</li>
115 <li>Move from <code>1</code> to <code>2</code>: <code>1</code> fuel</li>
116 <li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
117 <li>Move from <code>14</code> to <code>2</code>: <code>12</code> fuel</li>
118 </ul>
119 <p>This costs a total of <code><em>37</em></code> fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position <code>1</code> (<code>41</code> fuel), position <code>3</code> (<code>39</code> fuel), or position <code>10</code> (<code>71</code> fuel).</p>
120 <p>Determine the horizontal position that the crabs can align to using the least fuel possible. <em>How much fuel must they spend to align to that position?</em></p>
121 </article>
122 <p>Your puzzle answer was <code>336721</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?</p>
123 <p>As it turns out, crab submarine engines <span title="This appears to be due to the modial interaction of magneto-reluctance and capacitive duractance.">don't burn fuel at a constant rate</span>. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs <code>1</code>, the second step costs <code>2</code>, the third step costs <code>3</code>, and so on.</p>
124 <p>As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes <code>5</code>:</p>
125 <ul>
126 <li>Move from <code>16</code> to <code>5</code>: <code>66</code> fuel</li>
127 <li>Move from <code>1</code> to <code>5</code>: <code>10</code> fuel</li>
128 <li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
129 <li>Move from <code>0</code> to <code>5</code>: <code>15</code> fuel</li>
130 <li>Move from <code>4</code> to <code>5</code>: <code>1</code> fuel</li>
131 <li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
132 <li>Move from <code>7</code> to <code>5</code>: <code>3</code> fuel</li>
133 <li>Move from <code>1</code> to <code>5</code>: <code>10</code> fuel</li>
134 <li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
135 <li>Move from <code>14</code> to <code>5</code>: <code>45</code> fuel</li>
136 </ul>
137 <p>This costs a total of <code><em>168</em></code> fuel. This is the new cheapest possible outcome; the old alignment position (<code>2</code>) now costs <code>206</code> fuel instead.</p>
138 <p>Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! <em>How much fuel must they spend to align to that position?</em></p>
139 </article>
140 <p>Your puzzle answer was <code>91638945</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
141 <p>At this point, you should <a href="/2021">return to your Advent calendar</a> and try another puzzle.</p>
142 <p>If you still want to see it, you can <a href="7/input" target="_blank">get your puzzle input</a>.</p>
143 <p>You can also <span class="share">[Share<span class="share-content">on
144 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22The+Treachery+of+Whales%22+%2D+Day+7+%2D+Advent+of+Code+2021&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F7&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
145 <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+%22The+Treachery+of+Whales%22+%2D+Day+7+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F7'}else{return false;}" target="_blank">Mastodon</a
146 ></span>]</span> this puzzle.</p>
147 </main>
148
149 <!-- ga -->
150 <script>
151 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
152 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
153 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
154 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
155 ga('create', 'UA-69522494-1', 'auto');
156 ga('set', 'anonymizeIp', true);
157 ga('send', 'pageview');
158 </script>
159 <!-- /ga -->
160 </body>
161 </html>