-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07_1.js
85 lines (80 loc) · 2.04 KB
/
day07_1.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
console.log("###########");
import { readLines } from "https://deno.land/[email protected]/io/bufio.ts";
import { range } from "https://deno.land/x/[email protected]/range.mjs";
import { slidingWindows } from "https://deno.land/[email protected]/collections/mod.ts";
import count from "https://deno.land/x/[email protected]/src/collection/count.ts";
const input = await Deno.readTextFile("./resources/input07.txt");
const cmds = input
.trim()
.split("$")
.map((s) => s.trim().split("\n").filter(Boolean))
.filter((a) => a.length > 0);
//console.log({ cmds });
const root = {
dirs: {},
files: [],
path: [],
totalSize: 0,
};
let pwd = root;
const cd = (pwd, dir) => {
if (dir === "..") {
return pwd.parent;
}
if (!pwd.dirs[dir])
pwd.dirs[dir] = {
dirs: {},
files: [],
name: dir,
parent: pwd,
totalSize: 0,
path: [...pwd.path, pwd],
};
return pwd.dirs[dir];
};
for (let l of cmds) {
if (l.length === 1) {
let [cm, val] = l[0].split(" ");
if (cm === "cd") pwd = cd(pwd, val);
} else {
if (l[0] === "ls") {
const list = l.slice(1);
for (let e of list) {
let [size, name] = e.split(" ");
if (size === "dir") {
console.log("--", pwd);
pwd.dirs[name] = {
name,
dirs: {},
files: [],
parent: pwd,
totalSize: 0,
path: [...pwd.path, pwd],
};
} else {
const s = parseInt(size, 10);
pwd.files.push({ name, size: s });
pwd.totalSize += s;
pwd.path.forEach((p) => (p.totalSize += s));
}
}
}
}
console.log({ l });
}
let res = 0;
let arr = [];
const scanSizes = (r) => {
arr.push(r.totalSize);
if (r.totalSize < 100000) res += r.totalSize;
for (let c of Object.values(r.dirs)) {
scanSizes(c);
}
};
scanSizes(root);
console.log(res);
console.log(root.dirs["/"]);
const toFree = 30000000 - (70000000 - root.totalSize);
arr.sort((a, b) => a - b);
arr = arr.filter((a) => a > toFree);
console.log({ toFree, arr }, arr[0]);