diff --git a/2022/README.md b/2022/README.md index 80c889b675f22153a34669ac1a64e56327d5541f..de75d511cbfa258c10c9c54381fadfd32c8e3139 100644 --- a/2022/README.md +++ b/2022/README.md @@ -204,7 +204,17 @@ It looks like the puzzle input has fewer than 200 directories. YOLO. So, add to ### Part 2 -... +The new subproblems are +- Determine how much space needs to be freed +- Find the smallest directory whose size is greater than that amount + +### Refactoring + +In Part 1, we looked for all directories *below* some threshold size. +In Part 2 we want the smallest directory *above* some threshold size, which can be obtained by looking for all +directories larger than the threshold and selecting the smallest. +By parameterizing the searching method with the threshold size and whether we're looking above or below the threshold, +it will serve both parts. ## Day 8 diff --git a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java index 52404cbf5030729f2142dc96204585e1faab1c87..2036ed8301e4e901eb9d9ab72587dbaf01307fb9 100644 --- a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java +++ b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java @@ -36,13 +36,16 @@ public class Day7 extends Puzzle { Directory rootDirectory = null; public static final long THRESHOLD_DIRECTORY_SIZE = 100000; + public static final long TOTAL_FILESYSTEM_SIZE = 70000000; + public static final long SPACE_REQUIRED = 30000000; @Override public long computePart1(List<String> data) { if (rootDirectory == null) { rootDirectory = buildDirectoryTree(data); } - Set<Directory> directoriesBelowThreshold = getSmallDirectories(rootDirectory); + Set<Directory> directoriesBelowThreshold + = getDirectoriesAboveOrBelowThreshold(rootDirectory, THRESHOLD_DIRECTORY_SIZE, true); return directoriesBelowThreshold.stream().mapToLong(Directory::getSize).sum(); } @@ -51,7 +54,11 @@ public class Day7 extends Puzzle { if (rootDirectory == null) { rootDirectory = buildDirectoryTree(data); } - return 0; + long currentlyUnusedSpace = TOTAL_FILESYSTEM_SIZE - rootDirectory.getSize(); + long spaceNeededToBeFreed = SPACE_REQUIRED - currentlyUnusedSpace; + Set<Directory> directoriesAboveThreshold + = getDirectoriesAboveOrBelowThreshold(rootDirectory, spaceNeededToBeFreed, false); + return directoriesAboveThreshold.stream().mapToLong(Directory::getSize).min().orElse(Long.MAX_VALUE); } private Directory buildDirectoryTree(List<String> data) { @@ -85,15 +92,20 @@ public class Day7 extends Puzzle { return root; } - private Set<Directory> getSmallDirectories(Directory rootOfSubtree) { - Set<Directory> smallDirectories = new HashSet<>(); - if (rootOfSubtree.getSize() <= THRESHOLD_DIRECTORY_SIZE) { - smallDirectories.add(rootOfSubtree); + private Set<Directory> getDirectoriesAboveOrBelowThreshold(Directory rootOfSubtree, + long threshold, + boolean lookBelowThreshold) { + Set<Directory> matches = new HashSet<>(); + if (lookBelowThreshold && rootOfSubtree.getSize() <= threshold) { + matches.add(rootOfSubtree); + } + if (!lookBelowThreshold && rootOfSubtree.getSize() >= threshold) { + matches.add(rootOfSubtree); } for (Directory subdirectory : rootOfSubtree.getSubdirectories()) { - smallDirectories.addAll(getSmallDirectories(subdirectory)); + matches.addAll(getDirectoriesAboveOrBelowThreshold(subdirectory, threshold, lookBelowThreshold)); } - return smallDirectories; + return matches; } private static class Directory {