…and the Advent of Code challenge just keeps chugging along, just like Advent itself. Funny that, huh? Day 9, still recovering from yesterday’s Problem 2 and walking into this wondering what things are going to look like. Today’s challenge brought up an extra special surprise for old school Windows users (more on that to come).
Apparently we’re now in an underwater location, If we didn’t know we were still on Earth, we may think we got transported to Mon Cala, in a galaxy far far away…thankfully we’re all provided with our own personal mini-submarines to get around. The other historians think they’ve managed to figure out how these things work and jet off looking for their chief. Meanwhile, you notice an amphipod struggling with their computer. The driver is attempting to create more space on the computer by making more contiguous free space available but the program isn’t working properly. Did they try Window’s defrag?
Files are represented by a series of digits that alternate between the length of a file, and the length of free space. Additionally, every file has an ID number based on the order of the files before they are rearranged. The UID of files begins at 0. Our goal is to move file blocks one at a time from the end of the disk, to the leftmost free space block until no gaps remain between blocks. The end of the process is updating the checksum of the filesystem which involves multiplying each blocks’ position with the ID number it contains. Our goal is to provide the total of checksums as our result.
Breaking down our problem statement, we can identify the following;
With this in mind, we can develop the following approach to break down and solve this problem;
Success! We’ve completed our process and the disk has a lot more contiguous free space, however the computer is now running much slower. Our new amphipod friend surmises we could modify our compacting process and move whole files instead of individual storage blocks. We must update our solution to move whole files to the leftmost span of free space that can fit the file. Each file should only be moved once, in decreasing order of file ID starting with the highest and again, providing a resulting checksum of the filesystem.
Input and output does not change from Problem 1. Our constraints do change in that we are now moving a whole file instead of individual blocks, and if a file cannot fit within the available space, we do not move it.
With the above in mind, we can approach this problem in the following way;
Today’s challenge thankfully proved to involve less time for me than yesterday’s, I have to admit I’m dreading the inevitable back-to-back day’s of struggle bus during this challenge. Today’s Problem’s presented us with the following lessons and strategies;
Tomorrow brings us day 10, a new challenge, and likely new and unanticipated challenges.