From 03f0d06f300fe70de069bbfd471c0f90804a9c16 Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Wed, 7 Dec 2022 14:42:00 -0600
Subject: [PATCH] Completed Year 2022 Day 7

---
 2022/README.md                                | 12 +++++++-
 .../java/edu/unl/cse/bohn/year2022/Day7.java  | 28 +++++++++++++------
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/2022/README.md b/2022/README.md
index 80c889b..de75d51 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 52404cb..2036ed8 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 {
-- 
GitLab