2024(9): solve part 1 and some

Committed on the 8th of January, 2025, so I do not know what I did.
This commit is contained in:
2024-12-29 02:42:11 +01:00
parent 83685c7511
commit 4a94b3f018
2 changed files with 23 additions and 2 deletions

View File

@ -58,3 +58,24 @@ The final step of this file-compacting process is to update the _filesystem chec
Continuing the first example, the first few blocks' position multiplied by its file ID number are `0 * 0 = 0`, `1 * 0 = 0`, `2 * 9 = 18`, `3 * 9 = 27`, `4 * 8 = 32`, and so on. In this example, the checksum is the sum of these, _`1928`_.
<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. _What is the resulting filesystem checksum?_ <span class="quiet">(Be careful copy/pasting the input for this puzzle; it is a single, very long line.)</span>
### Part Two
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 [file system fragmentation](https://en.wikipedia.org/wiki/File_system_fragmentation) was a bad idea?
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 _whole files_ instead.
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 _decreasing file ID number_ 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.
The first example from above now proceeds differently:
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..
The process of updating the filesystem checksum is the same; now, this example's checksum would be _`2858`_.
Start over, now compacting the amphipod's hard drive using this new method instead. _What is the resulting filesystem checksum?_

View File

@ -19,7 +19,7 @@ def disk_map_to_blocks(disk_map: str) -> list:
return blocks
def move_ltr_to_empty_space(blocks: list) -> str:
def move_blocks_ltr_to_empty_space(blocks: list) -> str:
left = 0
right = len(blocks) - 1
@ -53,7 +53,7 @@ if __name__ == "__main__":
print("Blocks:")
print(blocks)
result = move_ltr_to_empty_space(blocks)
result = move_blocks_ltr_to_empty_space(blocks)
if verbose:
print()