Optimising day 15
[advent-of-code-22.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 2022</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?30"/>
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 <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>
12 </head><!--
13
14
15
16
17 Oh, hello! Funny seeing you here.
18
19 I appreciate your enthusiasm, but you aren't going to find much down here.
20 There certainly aren't clues to any of the puzzles. The best surprises don't
21 even appear in the source until you unlock them for real.
22
23 Please be careful with automated requests; I'm not a massive company, and I can
24 only take so much traffic. Please be considerate so that everyone gets to play.
25
26 If you're curious about how Advent of Code works, it's running on some custom
27 Perl code. Other than a few integrations (auth, analytics, social media), I
28 built the whole thing myself, including the design, animations, prose, and all
29 of the puzzles.
30
31 The puzzles are most of the work; preparing a new calendar and a new set of
32 puzzles each year takes all of my free time for 4-5 months. A lot of effort
33 went into building this thing - I hope you're enjoying playing it as much as I
34 enjoyed making it for you!
35
36 If you'd like to hang out, I'm @ericwastl on Twitter.
37
38 - Eric Wastl
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 -->
90 <body>
91 <header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2022/about">[About]</a></li><li><a href="/2022/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2022/settings">[Settings]</a></li><li><a href="/2022/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2022/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;<span class="title-event-wrap">0xffff&amp;</span><a href="/2022">2022</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2022">[Calendar]</a></li><li><a href="/2022/support">[AoC++]</a></li><li><a href="/2022/sponsors">[Sponsors]</a></li><li><a href="/2022/leaderboard">[Leaderboard]</a></li><li><a href="/2022/stats">[Stats]</a></li></ul></nav></div></header>
92
93 <div id="sidebar">
94 <div id="sponsor"><div class="quiet">Our <a href="/2022/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://www.twilio.com/quest" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">TwilioQuest</a> - Discover your power to change the world with code with TwilioQuest, an educational RPG. Learn foundational programming skills, JavaScript and Python while you explore The Cloud alongside our community of developers.</div></div>
95 </div><!--/sidebar-->
96
97 <main>
98 <article class="day-desc"><h2>--- Day 7: No Space Left On Device ---</h2><p>You can hear birds chirping and raindrops hitting leaves as the expedition proceeds. Occasionally, you can even hear much louder sounds in the distance; how big do the animals get out here, anyway?</p>
99 <p>The device the Elves gave you has problems with more than just its communication system. You try to run a system update:</p>
100 <pre><code>$ system-update --please --pretty-please-with-sugar-on-top
101 <span title="E099 PROGRAMMER IS OVERLY POLITE">Error</span>: No space left on device
102 </code></pre>
103 <p>Perhaps you can delete some files to make space for the update?</p>
104 <p>You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For example:</p>
105 <pre><code>$ cd /
106 $ ls
107 dir a
108 14848514 b.txt
109 8504156 c.dat
110 dir d
111 $ cd a
112 $ ls
113 dir e
114 29116 f
115 2557 g
116 62596 h.lst
117 $ cd e
118 $ ls
119 584 i
120 $ cd ..
121 $ cd ..
122 $ cd d
123 $ ls
124 4060174 j
125 8033020 d.log
126 5626152 d.ext
127 7214296 k
128 </code></pre>
129 <p>The filesystem consists of a tree of files (plain data) and directories (which can contain other directories or files). The outermost directory is called <code>/</code>. You can navigate around the filesystem, moving into or out of directories and listing the contents of the directory you're currently in.</p>
130 <p>Within the terminal output, lines that begin with <code>$</code> are <em>commands you executed</em>, very much like some modern computers:</p>
131 <ul>
132 <li><code>cd</code> means <em>change directory</em>. This changes which directory is the current directory, but the specific result depends on the argument:
133 <ul>
134 <li><code>cd x</code> moves <em>in</em> one level: it looks in the current directory for the directory named <code>x</code> and makes it the current directory.</li>
135 <li><code>cd ..</code> moves <em>out</em> one level: it finds the directory that contains the current directory, then makes that directory the current directory.</li>
136 <li><code>cd /</code> switches the current directory to the outermost directory, <code>/</code>.</li>
137 </ul>
138 </li>
139 <li><code>ls</code> means <em>list</em>. It prints out all of the files and directories immediately contained by the current directory:
140 <ul>
141 <li><code>123 abc</code> means that the current directory contains a file named <code>abc</code> with size <code>123</code>.</li>
142 <li><code>dir xyz</code> means that the current directory contains a directory named <code>xyz</code>.</li>
143 </ul>
144 </li>
145 </ul>
146 <p>Given the commands and output in the example above, you can determine that the filesystem looks visually like this:</p>
147 <pre><code>- / (dir)
148 - a (dir)
149 - e (dir)
150 - i (file, size=584)
151 - f (file, size=29116)
152 - g (file, size=2557)
153 - h.lst (file, size=62596)
154 - b.txt (file, size=14848514)
155 - c.dat (file, size=8504156)
156 - d (dir)
157 - j (file, size=4060174)
158 - d.log (file, size=8033020)
159 - d.ext (file, size=5626152)
160 - k (file, size=7214296)
161 </code></pre>
162 <p>Here, there are four directories: <code>/</code> (the outermost directory), <code>a</code> and <code>d</code> (which are in <code>/</code>), and <code>e</code> (which is in <code>a</code>). These directories also contain files of various sizes.</p>
163 <p>Since the disk is full, your first step should probably be to find directories that are good candidates for deletion. To do this, you need to determine the <em>total size</em> of each directory. The total size of a directory is the sum of the sizes of the files it contains, directly or indirectly. (Directories themselves do not count as having any intrinsic size.)</p>
164 <p>The total sizes of the directories above can be found as follows:</p>
165 <ul>
166 <li>The total size of directory <code>e</code> is <em>584</em> because it contains a single file <code>i</code> of size 584 and no other directories.</li>
167 <li>The directory <code>a</code> has total size <em>94853</em> because it contains files <code>f</code> (size 29116), <code>g</code> (size 2557), and <code>h.lst</code> (size 62596), plus file <code>i</code> indirectly (<code>a</code> contains <code>e</code> which contains <code>i</code>).</li>
168 <li>Directory <code>d</code> has total size <em>24933642</em>.</li>
169 <li>As the outermost directory, <code>/</code> contains every file. Its total size is <em>48381165</em>, the sum of the size of every file.</li>
170 </ul>
171 <p>To begin, find all of the directories with a total size of <em>at most 100000</em>, then calculate the sum of their total sizes. In the example above, these directories are <code>a</code> and <code>e</code>; the sum of their total sizes is <code><em>95437</em></code> (94853 + 584). (As in this example, this process can count files more than once!)</p>
172 <p>Find all of the directories with a total size of at most 100000. <em>What is the sum of the total sizes of those directories?</em></p>
173 </article>
174 <p>Your puzzle answer was <code>1084134</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Now, you're ready to choose a directory to delete.</p>
175 <p>The total disk space available to the filesystem is <code><em>70000000</em></code>. To run the update, you need unused space of at least <code><em>30000000</em></code>. You need to find a directory you can delete that will <em>free up enough space</em> to run the update.</p>
176 <p>In the example above, the total size of the outermost directory (and thus the total amount of used space) is <code>48381165</code>; this means that the size of the <em>unused</em> space must currently be <code>21618835</code>, which isn't quite the <code>30000000</code> required by the update. Therefore, the update still requires a directory with total size of at least <code>8381165</code> to be deleted before it can run.</p>
177 <p>To achieve this, you have the following options:</p>
178 <ul>
179 <li>Delete directory <code>e</code>, which would increase unused space by <code>584</code>.</li>
180 <li>Delete directory <code>a</code>, which would increase unused space by <code>94853</code>.</li>
181 <li>Delete directory <code>d</code>, which would increase unused space by <code>24933642</code>.</li>
182 <li>Delete directory <code>/</code>, which would increase unused space by <code>48381165</code>.</li>
183 </ul>
184 <p>Directories <code>e</code> and <code>a</code> are both too small; deleting them would not free up enough space. However, directories <code>d</code> and <code>/</code> are both big enough! Between these, choose the <em>smallest</em>: <code>d</code>, increasing unused space by <code><em>24933642</em></code>.</p>
185 <p>Find the smallest directory that, if deleted, would free up enough space on the filesystem to run the update. <em>What is the total size of that directory?</em></p>
186 </article>
187 <p>Your puzzle answer was <code>6183184</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
188 <p>At this point, you should <a href="/2022">return to your Advent calendar</a> and try another puzzle.</p>
189 <p>If you still want to see it, you can <a href="7/input" target="_blank">get your puzzle input</a>.</p>
190 <p>You can also <span class="share">[Share<span class="share-content">on
191 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22No+Space+Left+On+Device%22+%2D+Day+7+%2D+Advent+of+Code+2022&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F7&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
192 <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+%22No+Space+Left+On+Device%22+%2D+Day+7+%2D+Advent+of+Code+2022+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F7'}else{return false;}" target="_blank">Mastodon</a
193 ></span>]</span> this puzzle.</p>
194 </main>
195
196 <!-- ga -->
197 <script>
198 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
199 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
200 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
201 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
202 ga('create', 'UA-69522494-1', 'auto');
203 ga('set', 'anonymizeIp', true);
204 ga('send', 'pageview');
205 </script>
206 <!-- /ga -->
207 </body>
208 </html>