-
Notifications
You must be signed in to change notification settings - Fork 0
/
year2024_day9_puzzle.html
167 lines (104 loc) · 9.04 KB
/
year2024_day9_puzzle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8"/>
<title>Day 9 - Advent of Code 2024</title>
<link rel="stylesheet" type="text/css" href="/static/style.css?31"/>
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?1" title="High Contrast"/>
<link rel="shortcut icon" href="/favicon.png"/>
<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>
</head><!--
Oh, hello! Funny seeing you here.
I appreciate your enthusiasm, but you aren't going to find much down here.
There certainly aren't clues to any of the puzzles. The best surprises don't
even appear in the source until you unlock them for real.
Please be careful with automated requests; I'm not a massive company, and I can
only take so much traffic. Please be considerate so that everyone gets to play.
If you're curious about how Advent of Code works, it's running on some custom
Perl code. Other than a few integrations (auth, analytics, social media), I
built the whole thing myself, including the design, animations, prose, and all
of the puzzles.
The puzzles are most of the work; preparing a new calendar and a new set of
puzzles each year takes all of my free time for 4-5 months. A lot of effort
went into building this thing - I hope you're enjoying playing it as much as I
enjoyed making it for you!
If you'd like to hang out, I'm @was.tl on Bluesky, @[email protected] on
Mastodon, and @ericwastl on Twitter.
- Eric Wastl
-->
<body>
<div id="sidebar">
</div><!--/sidebar-->
<main>
<article class="day-desc"><h2>--- Day 9: Disk Fragmenter ---</h2><p>Another push of the button leaves you in the familiar hallways of some friendly <a href="/2021/day/23">amphipods</a>! Good thing you each somehow got your own personal mini submarine. The Historians jet away in search of the Chief, mostly by driving directly into walls.</p>
<p>While The Historians quickly figure out how to pilot these things, you notice an amphipod in the corner struggling with his computer. He's trying to make more contiguous free space by compacting all of the files, but his program isn't working; you offer to help.</p>
<p>He shows you the <em>disk map</em> (your puzzle input) he's already generated. For example:</p>
<pre><code>2333133121414131402</code></pre>
<p>The disk map uses a dense format to represent the layout of <em>files</em> and <em>free space</em> on the disk. The digits alternate between indicating the length of a file and the length of free space.</p>
<p>So, a disk map like <code>12345</code> would represent a one-block file, two blocks of free space, a three-block file, four blocks of free space, and then a five-block file. A disk map like <code>90909</code> would represent three nine-block files in a row (with no free space between them).</p>
<p>Each file on disk also has an <em>ID number</em> based on the order of the files as they appear <em>before</em> they are rearranged, starting with ID <code>0</code>. So, the disk map <code>12345</code> has three files: a one-block file with ID <code>0</code>, a three-block file with ID <code>1</code>, and a five-block file with ID <code>2</code>. Using one character for each block where digits are the file ID and <code>.</code> is free space, the disk map <code>12345</code> represents these individual blocks:</p>
<pre><code>0..111....22222</code></pre>
<p>The first example above, <code>2333133121414131402</code>, represents these individual blocks:</p>
<pre><code>00...111...2...333.44.5555.6666.777.888899</code></pre>
<p>The amphipod would like to <em>move file blocks one at a time</em> from the end of the disk to the leftmost free space block (until there are no gaps remaining between file blocks). For the disk map <code>12345</code>, the process looks like this:</p>
<pre><code>0..111....22222
02.111....2222.
022111....222..
0221112...22...
02211122..2....
022111222......
</code></pre>
<p>The first example requires a few more steps:</p>
<pre><code>00...111...2...333.44.5555.6666.777.888899
009..111...2...333.44.5555.6666.777.88889.
0099.111...2...333.44.5555.6666.777.8888..
00998111...2...333.44.5555.6666.777.888...
009981118..2...333.44.5555.6666.777.88....
0099811188.2...333.44.5555.6666.777.8.....
009981118882...333.44.5555.6666.777.......
0099811188827..333.44.5555.6666.77........
00998111888277.333.44.5555.6666.7.........
009981118882777333.44.5555.6666...........
009981118882777333644.5555.666............
00998111888277733364465555.66.............
0099811188827773336446555566..............
</code></pre>
<p>The final step of this file-compacting process is to update the <em>filesystem checksum</em>. To calculate the checksum, add up the result of multiplying each of these blocks' position with the file ID number it contains. The leftmost block is in position <code>0</code>. If a block contains free space, skip it instead.</p>
<p>Continuing the first example, the first few blocks' position multiplied by its file ID number are <code>0 * 0 = 0</code>, <code>1 * 0 = 0</code>, <code>2 * 9 = 18</code>, <code>3 * 9 = 27</code>, <code>4 * 8 = 32</code>, and so on. In this example, the checksum is the sum of these, <code><em>1928</em></code>.</p>
<p><span title="Bonus points if you make a cool animation of this process.">Compact the amphipod's hard drive</span> using the process he requested. <em>What is the resulting filesystem checksum?</em> <span class="quiet">(Be careful copy/pasting the input for this puzzle; it is a single, very long line.)</span></p>
</article>
<p>Your puzzle answer was <code>6334655979668</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Upon completion, two things immediately become clear. First, the disk definitely has a lot more contiguous free space, just like the amphipod hoped. Second, the computer is running much more slowly! Maybe introducing all of that <a href="https://en.wikipedia.org/wiki/File_system_fragmentation" target="_blank">file system fragmentation</a> was a bad idea?</p>
<p>The eager amphipod already has a new plan: rather than move individual blocks, he'd like to try compacting the files on his disk by moving <em>whole files</em> instead.</p>
<p>This time, attempt to move whole files to the leftmost span of free space blocks that could fit the file. Attempt to move each file exactly once in order of <em>decreasing file ID number</em> starting with the file with the highest file ID number. If there is no span of free space to the left of a file that is large enough to fit the file, the file does not move.</p>
<p>The first example from above now proceeds differently:</p>
<pre><code>00...111...2...333.44.5555.6666.777.888899
0099.111...2...333.44.5555.6666.777.8888..
0099.1117772...333.44.5555.6666.....8888..
0099.111777244.333....5555.6666.....8888..
00992111777.44.333....5555.6666.....8888..
</code></pre>
<p>The process of updating the filesystem checksum is the same; now, this example's checksum would be <code><em>2858</em></code>.</p>
<p>Start over, now compacting the amphipod's hard drive using this new method instead. <em>What is the resulting filesystem checksum?</em></p>
</article>
<p>Your puzzle answer was <code>6349492251099</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
<p>At this point, you should <a href="/2024">return to your Advent calendar</a> and try another puzzle.</p>
<p>If you still want to see it, you can <a href="9/input" target="_blank">get your puzzle input</a>.</p>
<p>You can also <span class="share">[Share<span class="share-content">on
<a href="https://bsky.app/intent/compose?text=I%27ve+completed+%22Disk+Fragmenter%22+%2D+Day+9+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F9" target="_blank">Bluesky</a>
<a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Disk+Fragmenter%22+%2D+Day+9+%2D+Advent+of+Code+2024&url=https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
<a href="javascript:void(0);" onclick="var ms; try{ms=localStorage.getItem('mastodon.server')}finally{} if(typeof ms!=='string')ms=''; ms=prompt('Mastodon Server?',ms); if(typeof ms==='string' && ms.length){this.href='https://'+ms+'/share?text=I%27ve+completed+%22Disk+Fragmenter%22+%2D+Day+9+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F9';try{localStorage.setItem('mastodon.server',ms);}finally{}}else{return false;}" target="_blank">Mastodon</a
></span>]</span> this puzzle.</p>
</main>
<!-- ga -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-69522494-1', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
<!-- /ga -->
</body>
</html>