Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
--- Day 7: No Space Left On Device ---
You can hear birds chirping and raindrops hitting leaves as the expedition proceeds. Occasionally, you can even hear much louder sounds in the distance; how big do the animals get out here, anyway?
The device the Elves gave you has problems with more than just its communication system. You try to run a system update:
You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For example:
The filesystem consists of a tree of files (plain data) and directories (which can contain other directories or files). The outermost directory is called /. You can navigate around the filesystem, moving into or out of directories and listing the contents of the directory you're currently in.
Within the terminal output, lines that begin with
$
are commands you executed, very much like some modern computers:cd
means change directory. This changes which directory is the current directory, but the specific result depends on the argument:cd x
moves in one level: it looks in the current directory for the directory namedx
and makes it the current directory.cd ..
moves out one level: it finds the directory that contains the current directory, then makes that directory the current directory.cd /
switches the current directory to the outermost directory,/
.ls
means list. It prints out all of the files and directories immediately contained by the current directory:123 abc
means that the current directory contains a file namedabc
with size123
.dir xyz
means that the current directory contains a directory namedxyz
.Given the commands and output in the example above, you can determine that the filesystem looks visually like this:
Here, there are four directories:
/
(the outermost directory),a
andd
(which are in/
), ande
(which is ina
). These directories also contain files of various sizes.Since the disk is full, your first step should probably be to find directories that are good candidates for deletion. To do this, you need to determine the total size of each directory. The total size of a directory is the sum of the sizes of the files it contains, directly or indirectly. (Directories themselves do not count as having any intrinsic size.)
The total sizes of the directories above can be found as follows:
The total size of directory
e
is584
because it contains a single filei
of size584
and no other directories.The directory
a
has total size94853
because it contains filesf
(size29116
),g
(size2557
), andh.lst
(size62596
), plus filei
indirectly (a
containse
which containsi
).Directory
d
has total size24933642
.As the outermost directory,
/
contains every file. Its total size is48381165
, the sum of the size of every file.To begin, find all of the directories with a total size of at most
100000
, then calculate the sum of their total sizes. In the example above, these directories area
ande
; the sum of their total sizes is95437
(94853
+584
). (As in this example, this process can count files more than once!)Find all of the directories with a total size of at most 100000. What is the sum of the total sizes of those directories?
--- Part Two ---
Now, you're ready to choose a directory to delete.
The total disk space available to the filesystem is 70000000. To run the update, you need unused space of at least 30000000. You need to find a directory you can delete that will free up enough space to run the update.
In the example above, the total size of the outermost directory (and thus the total amount of used space) is 48381165; this means that the size of the unused space must currently be 21618835, which isn't quite the 30000000 required by the update. Therefore, the update still requires a directory with total size of at least 8381165 to be deleted before it can run.
To achieve this, you have the following options:
Directories e and a are both too small; deleting them would not free up enough space. However, directories d and / are both big enough! Between these, choose the smallest: d, increasing unused space by 24933642.
Find the smallest directory that, if deleted, would free up enough space on the filesystem to run the update. What is the total size of that directory?