day07 part 2 and cleanup

This commit is contained in:
Felix Albrigtsen 2022-12-07 12:13:51 +01:00
parent 57150f3fde
commit 0b81d8d015
1 changed files with 23 additions and 5 deletions

View File

@ -29,18 +29,36 @@ public class App {
// Part 1 // Part 1
FileNode p1_root = fileSystemReader.getRoot(); FileNode root = fileSystemReader.getRoot();
root.printNode("");
System.out.println("\nTotal size: " + root.getSize() + "\n\n");
Integer directorySizeSum = 0; Integer directorySizeSum = 0;
ArrayList<Integer> p1_sizes = p1_root.getDirectorySizesFlat(new ArrayList<Integer>());
// System.out.println("Part 1: " + p1_sizes); ArrayList<Integer> directorySizes = root.getDirectorySizesFlat(new ArrayList<Integer>());
for (Integer size : p1_sizes) { for (Integer size : directorySizes) {
if (size <= 100000) { if (size <= 100000) {
directorySizeSum += size; directorySizeSum += size;
} }
} }
p1_root.printNode("");
// Java 8 alternative:
// Predicate<Integer> isLarge = (size) -> size <= 1000000;
// directorySizeSum = directorySizes.stream().filter(isLarge).reduce(0, (a, b) -> a + b);
System.out.println("Part 1: " + directorySizeSum); System.out.println("Part 1: " + directorySizeSum);
// Part 2
Integer maxUsedSpace = 70000000 - 30000000;
Integer p2_usedSpace = root.getSize();
Collections.sort(directorySizes);
for (Integer size : directorySizes) {
if (p2_usedSpace - size <= maxUsedSpace) {
System.out.println("Part 2: " + size);
break;
}
}
} }
} }