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
### 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
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment