mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 12:32:49 +00:00
2024 day 9
This commit is contained in:
40
2024/day-9/day9.test.ts
Normal file
40
2024/day-9/day9.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import * as path from "node:path";
|
||||
import { part1, part2 } from "./day9.ts";
|
||||
|
||||
test("day 9, part 1", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
console.log("\n\n");
|
||||
|
||||
const testResult = part1(testInput);
|
||||
console.log("Test data:", testResult);
|
||||
expect(testResult).toEqual(1928);
|
||||
|
||||
const finalResult = part1(input);
|
||||
console.log("Full data:", finalResult);
|
||||
expect(finalResult).toEqual(6349606724455);
|
||||
|
||||
console.log("\n\n");
|
||||
});
|
||||
|
||||
test("day 9, part 2", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
const testResult = part2(testInput);
|
||||
console.log("\n\n");
|
||||
console.log("Test data:", testResult);
|
||||
expect(testResult).toEqual(2858);
|
||||
|
||||
const finalResult = part2(input);
|
||||
console.log("Full data:", finalResult);
|
||||
expect(finalResult).toEqual(6376648986651);
|
||||
|
||||
console.log("\n\n");
|
||||
});
|
||||
147
2024/day-9/day9.ts
Normal file
147
2024/day-9/day9.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
export function part1(input: string) {
|
||||
const lines = input.split("\n");
|
||||
const line = lines[0];
|
||||
const diskMap = line.split("").map(Number);
|
||||
|
||||
const breakdown = diskMap
|
||||
.map((e, i) => {
|
||||
if (i % 2 === 0) {
|
||||
return {
|
||||
type: "file",
|
||||
id: i / 2,
|
||||
count: e,
|
||||
};
|
||||
}
|
||||
return {
|
||||
type: "free",
|
||||
count: e,
|
||||
};
|
||||
})
|
||||
.filter((e) => e.count !== 0);
|
||||
|
||||
while (true) {
|
||||
const lastFileIndex = breakdown.findLastIndex((v) => v.type === "file");
|
||||
const lastFile = breakdown[lastFileIndex];
|
||||
|
||||
const firstFreeSpaceIndex = breakdown.findIndex((v) => v.type === "free");
|
||||
const firstFreeSpace = breakdown[firstFreeSpaceIndex];
|
||||
|
||||
if (!firstFreeSpace || !lastFile) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (lastFile.count < firstFreeSpace.count) {
|
||||
const diff = firstFreeSpace.count - lastFile.count;
|
||||
breakdown.splice(firstFreeSpaceIndex, 0, {
|
||||
...lastFile,
|
||||
});
|
||||
firstFreeSpace.count = diff;
|
||||
breakdown.splice(lastFileIndex + 1, 1);
|
||||
} else if (lastFile.count > firstFreeSpace.count) {
|
||||
const diff = lastFile.count - firstFreeSpace.count;
|
||||
|
||||
breakdown.splice(firstFreeSpaceIndex, 1, {
|
||||
...lastFile,
|
||||
count: firstFreeSpace.count,
|
||||
});
|
||||
lastFile.count = diff;
|
||||
} else if (lastFile.count === firstFreeSpace.count) {
|
||||
breakdown.splice(firstFreeSpaceIndex, 1, {
|
||||
...lastFile,
|
||||
});
|
||||
breakdown.splice(lastFileIndex, 1);
|
||||
}
|
||||
|
||||
// const x = [];
|
||||
// for (const v of breakdown) {
|
||||
// x.push((v.id ?? ".").toString().repeat(v.count));
|
||||
// }
|
||||
// console.log(x.join(""));
|
||||
}
|
||||
// console.log(breakdown.filter((v) => v.count !== 0));
|
||||
let indexCount = 0;
|
||||
let final = 0;
|
||||
for (const b of breakdown) {
|
||||
for (let i = 0; i < b.count; i++) {
|
||||
final += indexCount * b.id;
|
||||
indexCount++;
|
||||
}
|
||||
}
|
||||
return final;
|
||||
}
|
||||
|
||||
export function part2(input: string) {
|
||||
const lines = input.split("\n");
|
||||
const line = lines[0];
|
||||
const diskMap = line.split("").map(Number);
|
||||
|
||||
const breakdown = diskMap
|
||||
.map((e, i) => {
|
||||
if (i % 2 === 0) {
|
||||
return {
|
||||
type: "file",
|
||||
id: i / 2,
|
||||
count: e,
|
||||
};
|
||||
}
|
||||
return {
|
||||
type: "free",
|
||||
count: e,
|
||||
};
|
||||
})
|
||||
.filter((e) => e.count !== 0);
|
||||
const files = breakdown
|
||||
.filter((v) => v.type === "file")
|
||||
.sort((a, b) => b.id - a.id);
|
||||
for (let i1 = 0; i1 < files.length; i1++) {
|
||||
const file = files[i1];
|
||||
const fileIndex = breakdown.findIndex((v) => v.id === file.id);
|
||||
const fileInBreakdown = breakdown[fileIndex];
|
||||
|
||||
const firstFreeSpaceIndex = breakdown.findIndex(
|
||||
(v, i) =>
|
||||
v.type === "free" && i < fileIndex && v.count >= fileInBreakdown?.count,
|
||||
);
|
||||
|
||||
const firstFreeSpace = breakdown[firstFreeSpaceIndex];
|
||||
|
||||
if (!firstFreeSpace || !fileInBreakdown) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fileInBreakdown.count < firstFreeSpace.count) {
|
||||
const diff = firstFreeSpace.count - fileInBreakdown.count;
|
||||
breakdown.splice(firstFreeSpaceIndex, 0, {
|
||||
...fileInBreakdown,
|
||||
});
|
||||
firstFreeSpace.count = diff;
|
||||
fileInBreakdown.type = "free";
|
||||
fileInBreakdown.id = undefined;
|
||||
} else if (fileInBreakdown.count === firstFreeSpace.count) {
|
||||
breakdown.splice(firstFreeSpaceIndex, 1, {
|
||||
...fileInBreakdown,
|
||||
});
|
||||
fileInBreakdown.type = "free";
|
||||
fileInBreakdown.id = undefined;
|
||||
}
|
||||
|
||||
// const x = [];
|
||||
// for (const v of breakdown) {
|
||||
// x.push((v.id ?? ".").toString().repeat(v.count));
|
||||
// }
|
||||
// console.log(x.join(""));
|
||||
}
|
||||
let indexCount = 0;
|
||||
let final = 0;
|
||||
for (const b of breakdown) {
|
||||
for (let i = 0; i < b.count; i++) {
|
||||
if (!b.id) {
|
||||
indexCount++;
|
||||
continue;
|
||||
}
|
||||
final += indexCount * b.id;
|
||||
indexCount++;
|
||||
}
|
||||
}
|
||||
return final;
|
||||
}
|
||||
1
2024/day-9/input.txt
Normal file
1
2024/day-9/input.txt
Normal file
File diff suppressed because one or more lines are too long
1
2024/day-9/test-input.txt
Normal file
1
2024/day-9/test-input.txt
Normal file
@@ -0,0 +1 @@
|
||||
2333133121414131402
|
||||
Reference in New Issue
Block a user