Skip to content

Commit 3e0ed27

Browse files
committed
09/2024
1 parent 1702a98 commit 3e0ed27

File tree

5 files changed

+161
-10
lines changed

5 files changed

+161
-10
lines changed

2024/09/index.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { getPuzzle } from "../../utils";
2+
3+
const puzzleInput = getPuzzle(__dirname).trim();
4+
const diskMap = puzzleInput.split("");
5+
6+
// Part 1
7+
(() => {
8+
console.time("part 1");
9+
let representation = "";
10+
11+
for (let i = 0; i < diskMap.length; i++) {
12+
const entry = Number(diskMap[i]);
13+
const isFile = i % 2 === 0;
14+
15+
for (let j = 0; j < entry; j++) {
16+
representation += `${isFile ? i / 2 : "."}_`;
17+
}
18+
}
19+
20+
let representationArr = representation
21+
.split("_")
22+
.filter((e) => e !== "")
23+
.map((e) => (e === "." ? e : Number(e)));
24+
25+
let firstFreeSpaceBlockIndex = representationArr.indexOf(".");
26+
let lastFileBlockIndex = representationArr.findLastIndex((e) => e !== ".");
27+
28+
while (lastFileBlockIndex > firstFreeSpaceBlockIndex) {
29+
const lastEntry = representationArr.pop();
30+
31+
if (lastEntry !== ".") {
32+
representationArr[firstFreeSpaceBlockIndex] = lastEntry;
33+
}
34+
35+
firstFreeSpaceBlockIndex = representationArr.indexOf(".");
36+
lastFileBlockIndex = representationArr.findLastIndex((e) => e !== ".");
37+
38+
if (firstFreeSpaceBlockIndex === -1) {
39+
break;
40+
}
41+
}
42+
43+
let checksum = 0;
44+
45+
for (let i = 0; i < representationArr.length; i++) {
46+
const block = representationArr[i];
47+
48+
if (block !== ".") {
49+
checksum += block * i;
50+
}
51+
}
52+
53+
console.log("part 1 checksum ::", checksum);
54+
console.timeEnd("part 1");
55+
})();
56+
57+
// Part 2
58+
(() => {
59+
console.time("part 2");
60+
let representation = "";
61+
62+
for (let i = 0; i < diskMap.length; i++) {
63+
const entry = Number(diskMap[i]);
64+
const isFile = i % 2 === 0;
65+
66+
for (let j = 0; j < entry; j++) {
67+
representation += `${isFile ? i / 2 : "."}_`;
68+
}
69+
}
70+
71+
let representationArr = representation
72+
.split("_")
73+
.filter((e) => e !== "")
74+
.map((e) => (e === "." ? e : Number(e)));
75+
76+
const checkedNumbers = new Set<number>();
77+
78+
let i = representationArr.length;
79+
80+
while (true) {
81+
i = i - 1;
82+
83+
if (i <= 0) {
84+
break;
85+
}
86+
87+
const num = representationArr[i];
88+
89+
if (num === "." || checkedNumbers.has(num)) {
90+
continue;
91+
}
92+
93+
checkedNumbers.add(num);
94+
95+
if (num === 0) {
96+
break;
97+
}
98+
99+
let fileSize = 1;
100+
let j = i;
101+
102+
while (true) {
103+
j = j - 1;
104+
if (representationArr[j] === num) {
105+
fileSize++;
106+
} else {
107+
break;
108+
}
109+
}
110+
111+
const availableSpace = representationArr.findIndex((entry, i, arr) => {
112+
if (entry === ".") {
113+
let match = true;
114+
115+
for (let k = 0; k < fileSize; k++) {
116+
if (arr[i + k] !== ".") {
117+
match = false;
118+
}
119+
}
120+
121+
return match;
122+
}
123+
124+
return false;
125+
});
126+
127+
if (availableSpace === -1 || availableSpace > j) {
128+
continue;
129+
}
130+
131+
for (let k = 0; k < fileSize; k++) {
132+
representationArr[availableSpace + k] = num;
133+
134+
representationArr[j + 1 + k] = ".";
135+
}
136+
}
137+
138+
let checksum = 0;
139+
140+
for (let i = 0; i < representationArr.length; i++) {
141+
const block = representationArr[i];
142+
143+
if (block !== ".") {
144+
checksum += block * i;
145+
}
146+
}
147+
148+
console.log("part 2 checksum ::", checksum);
149+
console.timeEnd("part 2");
150+
})();

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :---------------------------------------: | :----: | :----: |
9+
| [09](https://adventofcode.com/2024/day/9) |||
910
| [08](https://adventofcode.com/2024/day/8) |||
1011
| [07](https://adventofcode.com/2024/day/7) |||
1112
| [06](https://adventofcode.com/2024/day/6) |||

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"inquirer": "^8.0.0",
1515
"nodemon": "^2.0.20",
1616
"ts-node": "^10.9.1",
17-
"typescript": "^4.9.3"
17+
"typescript": "^5.7.2"
1818
}
1919
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"compilerOptions": {
33
"downlevelIteration": true,
44
"esModuleInterop": true,
5-
"lib": ["ES2021.String"]
5+
"lib": ["ES2021.String", "ES2023.Array"]
66
}
77
}

0 commit comments

Comments
 (0)