-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart-1.ts
41 lines (33 loc) · 1.03 KB
/
part-1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Heavily inspired from JPBM135's result:
// https://github.com/JPBM135/advent-of-coding-2022/blob/main/day-07/index.ts
import { getInput } from "../../../src/get-input.ts";
const input = getInput(2022, 7);
const lines = input.split("\n");
const dirSizes = new Map<string, number>();
const currentDir: string[] = [];
for (const line of lines) {
const split = line.split(" ");
if (line.startsWith("$ cd")) {
const dirName = split[2];
if (dirName === "..") {
currentDir.pop();
} else {
currentDir.push(dirName);
}
continue;
} else {
if (split[0] === "dir" || line.startsWith("$")) continue;
for (let count = 0; count < currentDir.length + 1; count++) {
const filePath = `/${currentDir.slice(0, count).join("/")}`;
const fileSize = parseInt(line) + (dirSizes.get(filePath) ?? 0);
dirSizes.set(filePath, fileSize);
}
}
}
let total = 0;
const dirArr = [...dirSizes.entries()];
for (const [_, size] of dirArr) {
if (size >= 100_000) continue;
total += size;
}
console.log(total);