Skip to content

Commit

Permalink
Day 9 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Dec 9, 2024
1 parent 80e8718 commit 6be070e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
24 changes: 24 additions & 0 deletions 2024/Day_09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,27 @@ The final step of this file-compacting process is to update the **filesystem che
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`**.

Compact the amphipod's hard drive using the process he requested. **What is the resulting filesystem checksum?**

---

## --- 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?**
31 changes: 25 additions & 6 deletions 2024/Day_09/part_1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
// = Copyright (c) NullDev = //
// ========================= //

const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n");
/* eslint-disable one-var */

const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("");

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
const res = INPUT
.flatMap((bc, i) => Array.from({ length: Number(bc) }, () => (i % 2 ? "." : i / 2))) // @ts-ignore
.reduce((_, __, idx, arr) => {
if (idx === 0){
const fileBlockCount = arr.filter((blk) => blk !== ".").length; // @ts-ignore
let left = arr.indexOf("."), right = arr.findLastIndex(blk => blk !== ".");
while (left < fileBlockCount){
if (arr[left] !== "."){
left++;
continue;
}
if (arr[right] === "."){
right--;
continue;
} // @ts-ignore
[arr[left], arr[right]] = [arr[right], arr[left]];
}
}
return arr;
}, []) // @ts-ignore
.reduce((total, nxId, i) => total + (nxId === "." ? 0 : nxId * i), 0);

const pEnd = performance.now();

console.log("<DESCRIPTION>: " + result);
console.log("FS CHECKSUM: " + res);
console.log(pEnd - pStart);

0 comments on commit 6be070e

Please sign in to comment.