Day 7 - Advent of Code 2022 Hints
- Parse the input to create a representation of the filesystem hierarchy
- For each directory, calculate its total size as the sum of the sizes of its files and the sizes of its subdirectories
- Identify all directories with a total size of at most 100000
- Calculate the sum of the total sizes of the identified directories
- Output the result
To calculate the total size of a directory, we can define a function that takes in a directory as input and returns its total size. The function first initializes the total size to 0, then iterates over the files in the directory, adding their sizes to the total size. Next, it iterates over the subdirectories of the directory, recursively calculating their total sizes and adding them to the total size. Finally, it returns the total size.
- Modify the function that calculates the total size of a directory to also take in the size of the unused space on the disk. The function should then return whether deleting the directory would free up enough space to run the update.
- Define a function that takes in a list of directories and the amount of unused space on the disk as inputs. The function should iterate over the directories, using the modified calculateTotalSize function on each one to determine if it is deletable and if it is smaller than the current smallest deletable directory. If the current directory is deletable and smaller than the current smallest deletable directory, the function should update the smallest deletable directory. Finally, the function should return the smallest deletable directory.
- Use the function defined in step 2 to find the smallest deletable directory in the input.
- Output the total size of the smallest deletable directory.