?? Day 9 of Advent of Code: Disk Fragmenter
Muhammad Fahad Bashir ??
Software Engineer | AI & ML Enthusiast | Generative AI Specialist | Prompt Engineer | Technical Content Writer | @ICodeGuru
Ever looked at a problem and thought, “This should be simple…†only to find out it’s anything but? That’s exactly how Day 9 greeted me! A disk map filled with alternating file sizes and free spaces seemed harmless enough until I realized the challenge wasn’t just in solving the problem—it was in understanding it deeply and explaining it clearly.
The Problem
We were given a dense disk map like this:
2333133121414131402
Each digit alternates between representing file lengths and free space lengths. For example, this map translates to:
- 2 blocks for file ID 0
- 3 blocks of free space
- 3 blocks for file ID 1 …and so on.
Our task was to:
- Simulate Compacting the disk—move file blocks one by one to the leftmost free space until no gaps remain.
- Calculate a Checksum—sum the products of each file block's position and its file ID, ignoring free spaces.
Sounds easy, right? Well…
The Real Challenge
The input wasn’t just a short, simple example. Instead, it was a massive file with hundreds of characters, each representing file lengths or free spaces. The real challenge was:
- Understanding the Problem: Parsing the dense map correctly and simulating movement step by step.
- Optimizing the Process: Compacting large inputs efficiently without wasting memory or processing time.
- Checksum Accuracy: Making sure the final calculation reflected only the compacted file blocks, not the free spaces.
The Approach
1?? Parse the Disk Map We started by breaking down the dense input into file IDs and free spaces. Each pair of digits alternated between file and space lengths, and we represented this in a list where files used IDs and free spaces used ..
For example Input: 2333133121414131402 Parsed: 0..111....22222
领英推è
2?? Simulate Compacting File blocks were moved left one step at a time to fill the nearest free space. This ensured no gaps remained between file blocks.
Before compacting:
0..111....22222
After compacting:
011122222......
3?? Calculate the Checksum Once compacted, the checksum was calculated as: position×file?ID\text{position} \times \text{file ID}position×file?ID For example:
Position 0, File ID 0 → 0 * 0 = 0
Position 1, File ID 1 → 1 * 1 = 1
Position 2, File ID 1 → 2 * 1 = 2
And the sum of these values became the final checksum.
The Takeaway
Day 9 wasn’t just about solving the problem; it was about approaching it systematically:
- Parsing dense data requires precision—understanding the structure of the input was half the battle.
- Simulating movement over large datasets demands efficiency, and small optimizations can make a huge difference.
- Communicating the solution? That’s the art of explaining complexity with clarity.
Understanding the problem is truly an art, but explaining it to others? That’s a masterpiece.
Reflections
The lesson here was clear: The problem itself isn’t hard—it’s how you approach it that makes the difference. With the right perspective and techniques like simulation, parsing, and optimization, even a daunting task becomes manageable.
Looking forward to tomorrow’s challenge! ??
#AdventOfCode #ProblemSolving #CodingJourney #EfficiencyMatters #Programming #Python #TechChallenges #UnderstandingIsAnArt #LearningByDoing #CodeOptimization
Talk about AI & Data | AWS | SQL | Python | Node JS
3 个月Very informative