-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.js
96 lines (76 loc) · 2.6 KB
/
07.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
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable no-param-reassign */
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput, sum} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const lines = input.split('\n').filter(line => line !== '');
function updateDeep(currentObject, line, currentDirectoryPath) {
if (currentDirectoryPath.length === 1) {
if (line.startsWith('dir ')) {
const directory = line.split('dir ')[1];
currentObject[currentDirectoryPath[0]][directory] = {};
return;
}
const [fileSize, fileName] = line.split(' ');
currentObject[currentDirectoryPath[0]][fileName] = Number(fileSize);
return;
}
const nextPath = currentDirectoryPath.slice(1, currentDirectoryPath.length);
updateDeep(currentObject[currentDirectoryPath[0]], line, nextPath);
}
const fileSystem = {'/': {}};
const currentDirectory = [];
// Create file system hierarchy
for (const line of lines) {
if (line.startsWith('$ cd ')) {
const directory = line.split('$ cd ')[1];
if (directory === '..') {
currentDirectory.pop();
} else {
currentDirectory.push(directory);
}
continue;
}
if (line.startsWith('$')) continue;
if (line.startsWith('dir ')) {
updateDeep(fileSystem, line, currentDirectory);
continue;
}
updateDeep(fileSystem, line, currentDirectory);
}
const directoriesWithSummedSizes = [];
const directoriesMost100k = [];
function sumDirectory(directory, parent) {
let directorySize = 0;
const children = Object.values(directory);
for (const [index, child] of children.entries()) {
if (typeof child === 'object') {
directorySize += sumDirectory(child, Object.keys(directory)[index]);
} else if (typeof child === 'number') {
directorySize += child;
}
}
if (directorySize <= 100_000) {
directoriesMost100k.push({name: parent, size: directorySize});
}
directoriesWithSummedSizes.push({name: parent, size: directorySize});
return directorySize;
}
const usedSpace = sumDirectory(fileSystem['/'], '/');
console.log(
'Sum of directories below 100k size:',
sum(directoriesMost100k.map(directory => directory.size)),
);
const totalSpace = 70_000_000;
const minimumAvailableSpace = 30_000_000;
const availableSpace = totalSpace - usedSpace;
const neededSpace = minimumAvailableSpace - availableSpace;
const sortedSummedDirectories = directoriesWithSummedSizes.sort(
(a, b) => b.size - a.size,
);
// Smallest directory with needed space
const directoryToDelete = sortedSummedDirectories
.reverse()
.find(directory => directory.size >= neededSpace);
console.log('Directory to delete:', directoryToDelete);