Skip to content
Snippets Groups Projects
Commit 03f0d06f authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Completed Year 2022 Day 7

parent fac478b8
No related branches found
No related tags found
No related merge requests found
...@@ -204,7 +204,17 @@ It looks like the puzzle input has fewer than 200 directories. YOLO. So, add to ...@@ -204,7 +204,17 @@ It looks like the puzzle input has fewer than 200 directories. YOLO. So, add to
### Part 2 ### 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 ## Day 8
... ...
......
...@@ -36,13 +36,16 @@ public class Day7 extends Puzzle { ...@@ -36,13 +36,16 @@ public class Day7 extends Puzzle {
Directory rootDirectory = null; Directory rootDirectory = null;
public static final long THRESHOLD_DIRECTORY_SIZE = 100000; public static final long THRESHOLD_DIRECTORY_SIZE = 100000;
public static final long TOTAL_FILESYSTEM_SIZE = 70000000;
public static final long SPACE_REQUIRED = 30000000;
@Override @Override
public long computePart1(List<String> data) { public long computePart1(List<String> data) {
if (rootDirectory == null) { if (rootDirectory == null) {
rootDirectory = buildDirectoryTree(data); rootDirectory = buildDirectoryTree(data);
} }
Set<Directory> directoriesBelowThreshold = getSmallDirectories(rootDirectory); Set<Directory> directoriesBelowThreshold
= getDirectoriesAboveOrBelowThreshold(rootDirectory, THRESHOLD_DIRECTORY_SIZE, true);
return directoriesBelowThreshold.stream().mapToLong(Directory::getSize).sum(); return directoriesBelowThreshold.stream().mapToLong(Directory::getSize).sum();
} }
...@@ -51,7 +54,11 @@ public class Day7 extends Puzzle { ...@@ -51,7 +54,11 @@ public class Day7 extends Puzzle {
if (rootDirectory == null) { if (rootDirectory == null) {
rootDirectory = buildDirectoryTree(data); 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) { private Directory buildDirectoryTree(List<String> data) {
...@@ -85,15 +92,20 @@ public class Day7 extends Puzzle { ...@@ -85,15 +92,20 @@ public class Day7 extends Puzzle {
return root; return root;
} }
private Set<Directory> getSmallDirectories(Directory rootOfSubtree) { private Set<Directory> getDirectoriesAboveOrBelowThreshold(Directory rootOfSubtree,
Set<Directory> smallDirectories = new HashSet<>(); long threshold,
if (rootOfSubtree.getSize() <= THRESHOLD_DIRECTORY_SIZE) { boolean lookBelowThreshold) {
smallDirectories.add(rootOfSubtree); 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()) { for (Directory subdirectory : rootOfSubtree.getSubdirectories()) {
smallDirectories.addAll(getSmallDirectories(subdirectory)); matches.addAll(getDirectoriesAboveOrBelowThreshold(subdirectory, threshold, lookBelowThreshold));
} }
return smallDirectories; return matches;
} }
private static class Directory { private static class Directory {
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment