From 0b81d8d0151340bee551de074b3941ef934c8f7d Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Wed, 7 Dec 2022 12:13:51 +0100 Subject: [PATCH] day07 part 2 and cleanup --- day07/src/main/java/com/felixalb/app/App.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) 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; + } + } } }