1
1
export default function Day ( data ) {
2
-
3
- const { files, dirs} = loopThroughCommands ( data ) ;
2
+ const { files, dirs } = loopThroughCommands ( data ) ;
4
3
dirs . sort ( ( a , b ) => a . size - b . size ) ;
5
4
const sum = dirs . reduce ( ( acc , dir ) => {
6
5
if ( dir . size < 100000 ) {
@@ -10,6 +9,14 @@ export default function Day(data) {
10
9
} , 0 ) ;
11
10
12
11
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 } ` ) ;
13
20
}
14
21
15
22
function loopThroughCommands ( commands , from = 0 , name = "/" ) {
@@ -27,9 +34,9 @@ function loopThroughCommands(commands, from = 0, name = "/") {
27
34
if ( command [ 0 ] === "$" ) {
28
35
if ( command [ 1 ] === "cd" ) {
29
36
if ( command [ 2 ] === ".." ) {
30
- return { files : dirStructure , newI : i , dirs : directories } ;
37
+ return { files : dirStructure , newI : i , dirs : directories } ;
31
38
} 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 ] ) ;
33
40
directories . push ( files ) ;
34
41
directories . push ( ...dirs ) ;
35
42
i = newI ;
@@ -39,15 +46,13 @@ function loopThroughCommands(commands, from = 0, name = "/") {
39
46
}
40
47
}
41
48
}
42
- }
43
- else if ( ! isNaN ( command [ 0 ] ) ) {
49
+ } else if ( ! isNaN ( command [ 0 ] ) ) {
44
50
dirStructure . size += parseInt ( command [ 0 ] ) ;
45
51
dirStructure . files . push ( {
46
52
name : command [ 1 ] ,
47
53
size : parseInt ( command [ 0 ] ) ,
48
54
} ) ;
49
55
}
50
-
51
56
}
52
- return { files : dirStructure , newI : i , dirs : directories } ;
57
+ return { files : dirStructure , newI : i , dirs : directories } ;
53
58
}
0 commit comments