-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.js
85 lines (74 loc) · 2.19 KB
/
day7.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
import { readFile } from './helpers.js';
const data = readFile(7).split('\n');
// Build file tree
const fileTree = data.reduce(
(dir, line) => {
const details = line.split(' ');
// Not needed
if (details.includes('ls')) return dir;
if (details.includes('dir')) {
dir.children = [
...(dir?.children ?? []),
{
name: details.at(-1),
children: [],
parent: dir,
size: 0,
totalSize: 0,
},
];
return dir;
}
if (details.includes('cd')) {
if (details.at(-1) === '/') return dir;
if (details.at(-1) === '..') {
return dir.parent;
}
return dir.children.find((child) => child.name === details.at(-1));
}
// Must be a file
const size = Number(details[0]);
dir.size += size;
dir.totalSize += size;
// Walk up every parent and add size to totalSize
let parent = dir.parent;
while (parent) {
parent.totalSize += size;
parent = parent.parent;
}
return dir;
},
{
name: '/',
children: [],
parent: null,
size: 0,
totalSize: 0,
}
);
// Walk up to the outer most parent
let parent = fileTree.parent;
do {
parent = parent.parent || parent;
} while (parent?.parent);
// Exclude less than 100k
const sumIt = (node) =>
(node.totalSize > 100_000 ? 0 : node.totalSize) +
node.children.reduce((sum, child) => sum + sumIt(child), 0);
const part1 = sumIt(parent);
console.log(part1);
// Part 2
const totalSpace = 70_000_000;
const spaceNeeded = 30_000_000 - (totalSpace - parent.totalSize);
// Find all directories where total size is greater than space needed
const findDirs = (node) => {
if (node.totalSize > spaceNeeded) {
return [node, ...node.children.flatMap(findDirs)];
}
return node.children.flatMap(findDirs);
};
// Get smallest of this set
const part2 = findDirs(parent)
.sort((a, b) => a.totalSize - b.totalSize)
.at(0).totalSize;
console.log(part2);