-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
36 lines (29 loc) · 816 Bytes
/
part1.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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n');
let path: string[] = [];
const dirs: Record<string, number> = {};
for (const line of input) {
let _a, _b;
let dirName = (_a = line.match(/\$ cd ([\w\/.]+)/)) === null || _a === void 0 ? void 0 : _a[1];
if (dirName === '/') {
path = [''];
continue;
} else if (dirName === '..') {
path.pop();
continue;
} else if (dirName) {
path.push(dirName);
continue;
}
let size = (_b = line.match(/(\d+) .+/)) === null || _b === void 0 ? void 0 : _b[1];
if (size) {
path.forEach((dir, i) => {
var key = path.slice(0, i).join('/') + '/' + dir;
dirs[key] ??= 0;
dirs[key] += +size;
});
continue;
}
}
const result = Object.values(dirs).reduce((a, b) => (b > 100000 ? a : a + b), 0);
console.log(result);