Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Coding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
Advent of Coding
Commits
03f0d06f
Commit
03f0d06f
authored
Dec 7, 2022
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Completed Year 2022 Day 7
parent
fac478b8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
2022/README.md
+11
-1
11 additions, 1 deletion
2022/README.md
2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java
+20
-8
20 additions, 8 deletions
2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java
with
31 additions
and
9 deletions
2022/README.md
+
11
−
1
View file @
03f0d06f
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
2022/src/main/java/edu/unl/cse/bohn/year2022/Day7.java
+
20
−
8
View file @
03f0d06f
...
...
@@ -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
())
{
s
ma
llDirectori
es
.
addAll
(
get
Small
Directories
(
subdirectory
));
ma
tch
es
.
addAll
(
getDirectories
AboveOrBelowThreshold
(
subdirectory
,
threshold
,
lookBelowThreshold
));
}
return
s
ma
llDirectori
es
;
return
ma
tch
es
;
}
private
static
class
Directory
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment