diff --git a/day07/src/main/java/com/felixalb/app/App.java b/day07/src/main/java/com/felixalb/app/App.java index 4a372b7..6e6c555 100644 --- a/day07/src/main/java/com/felixalb/app/App.java +++ b/day07/src/main/java/com/felixalb/app/App.java @@ -29,18 +29,36 @@ public class App { // 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; - ArrayList p1_sizes = p1_root.getDirectorySizesFlat(new ArrayList()); - // System.out.println("Part 1: " + p1_sizes); - for (Integer size : p1_sizes) { + + ArrayList directorySizes = root.getDirectorySizesFlat(new ArrayList()); + for (Integer size : directorySizes) { if (size <= 100000) { directorySizeSum += size; } } - p1_root.printNode(""); + + // Java 8 alternative: + // Predicate isLarge = (size) -> size <= 1000000; + // directorySizeSum = directorySizes.stream().filter(isLarge).reduce(0, (a, b) -> a + b); + 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; + } + } } }