Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2022 Day 1
- Advent of Code 2022 Day 2
- Advent of Code 2022 Day 3
- Advent of Code 2022 Day 4
- Advent of Code 2022 Day 5
- Advent of Code 2022 Day 6
- Advent of Code 2022 Day 7
- Advent of Code 2022 Day 8
- Advent of Code 2022 Day 9
- Advent of Code 2022 Day 10
- Advent of Code 2022 Day 11
- Advent of Code 2022 Day 12
- Advent of Code 2022 Day 13
- Advent of Code 2022 Day 14
- Advent of Code 2022 Day 15
- Advent of Code 2022 Day 16
- Advent of Code 2022 Day 17
- Advent of Code 2022 Day 18
- Advent of Code 2022 Day 19
- Advent of Code 2022 Day 20
- Advent of Code 2022 Day 21
- Advent of Code 2022 Day 22
- Advent of Code 2022 Day 23
- Advent of Code 2022 Day 24
- Advent of Code 2022 Day 25
-
Older post
-
Newer post
Advent of Code 2022 Day 7
Day 7: No Space Left On Device
https://adventofcode.com/2022/day/7
Yesterday, we got a communication device from an elf.
It needs a software update and there is not enough free space to apply it.
We browse around the file system using the command line and record what it displays. That’s our puzzle input for today.
An example input looks like this:
A line that starts with a $
indicates a command we typed.
cd
means change directory, it changes the current directory that future commands affect.cd ..
goes up a directory (to the parent directory).cd X
moves down a directory, to the child directory namedX
.cd /
is a special case ofcd X
where the current directory moves all the way up, to the root of the file tree.
ls
means list, it prints all files and directories the current directory contains.
The files and directories shown by ls
are represented as seperate lines in the input.
- Files start with the size of a file, followed by a space, and then the name of the file.
- So line
123 abc
is a file that has a size of123
, and is namedabc
.
- So line
- Directories start with
dir
, followed by a space, and then the name of the directory.- So line
dir xyz
is a directory namedxyz
.
- So line
With the input, we can completely map out the filesystem starting at the root directory, /
.
The total size of a directory is the sum of the sizes of all the files it contains, including the ones that are inside subfolders.
Part 1
The question asks to find the sum of all directories with a size of at most 100000.
My goal was to build up a map of all directories where the key is a path, and the value is the size of the directory at that path. Then I could filter out every entry that is too big, and sum up the remaining ones.
I kept track of 2 variables.
- One that holds the map of path to size. (the
HashMap
calledsizes
) - One with a list of all paths that are affected if we encounter a new file. (the
Vec
calledaffected
)
If we encounter a file, then its size should be added to all paths above it.
- Every time I
cd
down, the name gets added to the end ofaffected
. - Every time I
cd
up, the last item inaffected
gets removed.
Every file I encounter, I add its size to all paths that are affected.
I loop through affected
to construct the affected paths, and add the size to that path’s entry in sizes
.
The affected path can be built by constructing a path from increasing amounts of items in affected
.
For example, if affected
was ["/", "a", "b", "a"]
, the affected paths would be:
"/"
"/a"
"/a/b"
"/a/b/a"
I did this by using a PathBuf
, but any method will do (if this were JavaScript, I’d reach for path.join
).
Part 2
- The size of the filesystem is 70000000.
- The amount of unused space you need for the update is 30000000.
The question asks to find the size of the smallest directory you can delete to free enough space.
The part where the map of sizes is created is identical to part1.
We keep every directory that would result in enough free space if it’s deleted.
The minimum of those options is the answer to part2!