Skip to content

Commit 2a78c45

Browse files
committed
Day 7 Part 2
1 parent c9e75d6 commit 2a78c45

File tree

1 file changed

+17
-11
lines changed
  • src/main/java/com/sbaars/adventofcode/year22/days

1 file changed

+17
-11
lines changed

src/main/java/com/sbaars/adventofcode/year22/days/Day7.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,19 @@ public static void main(String[] args) throws IOException {
1818
Day d = new Day7();
1919
// d.downloadIfNotDownloaded();
2020
// d.downloadExample();
21-
d.printParts();
21+
// d.printParts();
2222
// System.in.read();
23-
d.submitPart1();
24-
// d.submitPart2();
23+
// d.submitPart1();
24+
d.submitPart2();
2525
}
2626

2727
public record Node(Map<String, Node> children, long size){}
2828
public record File(long size, String name){}
2929

3030
@Override
3131
public Object part1() {
32-
// Node parent = new Node(null, new ArrayList<>(), 0);
3332
List<String> commands = Arrays.asList(dayStrings()).stream().toList();
34-
// new Node("/", null, new ArrayList<>(), 0)
3533
Node root = findChildren(commands);
36-
// for(int i = 1; i< commands.length; i++) {
37-
// findChildren("")
38-
// }
3934
return sumSize(root);
4035
}
4136

@@ -48,8 +43,6 @@ public Node findChildren(List<String> commands) {
4843
String command = c.substring(2);
4944
if (command.startsWith("cd")) {
5045
String folder = command.substring(3);
51-
// System.out.println(folder);
52-
// i++;
5346
if (folder.equals("..")) break;
5447
i++;
5548
children.put(folder, findChildren(commands));
@@ -78,8 +71,21 @@ public long sumSize(Node n) {
7871
return total;
7972
}
8073

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+
8185
@Override
8286
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();
8490
}
8591
}

0 commit comments

Comments
 (0)