Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7c6f495

Browse filesBrowse files
committed
part 2
1 parent 05c38a7 commit 7c6f495
Copy full SHA for 7c6f495

File tree

Expand file treeCollapse file tree

1 file changed

+13
-8
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+13
-8
lines changed

‎day7/program.js

Copy file name to clipboardExpand all lines: day7/program.js
+13-8Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export default function Day(data) {
2-
3-
const {files, dirs} = loopThroughCommands(data);
2+
const { files, dirs } = loopThroughCommands(data);
43
dirs.sort((a, b) => a.size - b.size);
54
const sum = dirs.reduce((acc, dir) => {
65
if (dir.size < 100000) {
@@ -10,6 +9,14 @@ export default function Day(data) {
109
}, 0);
1110

1211
console.log(`Part 1: ${sum}`);
12+
13+
const totalFileSize = files.size;
14+
15+
const sizeRemaining = 70000000 - totalFileSize;
16+
const sizeRequiredForUpgrade = 30000000 - sizeRemaining;
17+
18+
const smallestDir = dirs.find(dir => dir.size >= sizeRequiredForUpgrade);
19+
console.log(`Part 2: ${smallestDir.size}`);
1320
}
1421

1522
function loopThroughCommands(commands, from = 0, name = "/") {
@@ -27,9 +34,9 @@ function loopThroughCommands(commands, from = 0, name = "/") {
2734
if (command[0] === "$") {
2835
if (command[1] === "cd") {
2936
if (command[2] === "..") {
30-
return {files: dirStructure, newI: i, dirs: directories};
37+
return { files: dirStructure, newI: i, dirs: directories };
3138
} else if (command[2] !== "/") {
32-
let {files, newI, dirs} = loopThroughCommands(commands, i + 1, command[2]);
39+
let { files, newI, dirs } = loopThroughCommands(commands, i + 1, command[2]);
3340
directories.push(files);
3441
directories.push(...dirs);
3542
i = newI;
@@ -39,15 +46,13 @@ function loopThroughCommands(commands, from = 0, name = "/") {
3946
}
4047
}
4148
}
42-
}
43-
else if (!isNaN(command[0])) {
49+
} else if (!isNaN(command[0])) {
4450
dirStructure.size += parseInt(command[0]);
4551
dirStructure.files.push({
4652
name: command[1],
4753
size: parseInt(command[0]),
4854
});
4955
}
50-
5156
}
52-
return {files: dirStructure, newI: i, dirs: directories};
57+
return { files: dirStructure, newI: i, dirs: directories };
5358
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.