@@ -18,24 +18,19 @@ public static void main(String[] args) throws IOException {
18
18
Day d = new Day7 ();
19
19
// d.downloadIfNotDownloaded();
20
20
// d.downloadExample();
21
- d .printParts ();
21
+ // d.printParts();
22
22
// System.in.read();
23
- d .submitPart1 ();
24
- // d.submitPart2();
23
+ // d.submitPart1();
24
+ d .submitPart2 ();
25
25
}
26
26
27
27
public record Node (Map <String , Node > children , long size ){}
28
28
public record File (long size , String name ){}
29
29
30
30
@ Override
31
31
public Object part1 () {
32
- // Node parent = new Node(null, new ArrayList<>(), 0);
33
32
List <String > commands = Arrays .asList (dayStrings ()).stream ().toList ();
34
- // new Node("/", null, new ArrayList<>(), 0)
35
33
Node root = findChildren (commands );
36
- // for(int i = 1; i< commands.length; i++) {
37
- // findChildren("")
38
- // }
39
34
return sumSize (root );
40
35
}
41
36
@@ -48,8 +43,6 @@ public Node findChildren(List<String> commands) {
48
43
String command = c .substring (2 );
49
44
if (command .startsWith ("cd" )) {
50
45
String folder = command .substring (3 );
51
- // System.out.println(folder);
52
- // i++;
53
46
if (folder .equals (".." )) break ;
54
47
i ++;
55
48
children .put (folder , findChildren (commands ));
@@ -78,8 +71,21 @@ public long sumSize(Node n) {
78
71
return total ;
79
72
}
80
73
74
+ public List <Long > sumSize (Node n , long sizeRoot ) {
75
+ List <Long > total = new ArrayList <>();
76
+ for (Node node : n .children .values ()) {
77
+ if (node .size >=sizeRoot -(70000000 -30000000 ) && !node .children .isEmpty ()) {
78
+ total .add (node .size );
79
+ }
80
+ total .addAll (sumSize (node , sizeRoot ));
81
+ }
82
+ return total ;
83
+ }
84
+
81
85
@ Override
82
86
public Object part2 () {
83
- return "" ;
87
+ List <String > commands = Arrays .asList (dayStrings ()).stream ().toList ();
88
+ Node root = findChildren (commands );
89
+ return sumSize (root , root .size ()).stream ().mapToLong (e -> e ).min ().getAsLong ();
84
90
}
85
91
}
0 commit comments