advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

task.txt (5364B)


      1 --- Day 7: No Space Left On Device ---
      2 
      3 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?
      4 
      5 The device the Elves gave you has problems with more than just its communication system. You try to run a system update:
      6 
      7 $ system-update --please --pretty-please-with-sugar-on-top
      8 Error: No space left on device
      9 
     10 Perhaps you can delete some files to make space for the update?
     11 
     12 You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For example:
     13 
     14 $ cd /
     15 $ ls
     16 dir a
     17 14848514 b.txt
     18 8504156 c.dat
     19 dir d
     20 $ cd a
     21 $ ls
     22 dir e
     23 29116 f
     24 2557 g
     25 62596 h.lst
     26 $ cd e
     27 $ ls
     28 584 i
     29 $ cd ..
     30 $ cd ..
     31 $ cd d
     32 $ ls
     33 4060174 j
     34 8033020 d.log
     35 5626152 d.ext
     36 7214296 k
     37 
     38 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.
     39 
     40 Within the terminal output, lines that begin with $ are commands you executed, very much like some modern computers:
     41 
     42     cd means change directory. This changes which directory is the current directory, but the specific result depends on the argument:
     43         cd x moves in one level: it looks in the current directory for the directory named x and makes it the current directory.
     44         cd .. moves out one level: it finds the directory that contains the current directory, then makes that directory the current directory.
     45         cd / switches the current directory to the outermost directory, /.
     46     ls means list. It prints out all of the files and directories immediately contained by the current directory:
     47         123 abc means that the current directory contains a file named abc with size 123.
     48         dir xyz means that the current directory contains a directory named xyz.
     49 
     50 Given the commands and output in the example above, you can determine that the filesystem looks visually like this:
     51 
     52 - / (dir)
     53   - a (dir)
     54     - e (dir)
     55       - i (file, size=584)
     56     - f (file, size=29116)
     57     - g (file, size=2557)
     58     - h.lst (file, size=62596)
     59   - b.txt (file, size=14848514)
     60   - c.dat (file, size=8504156)
     61   - d (dir)
     62     - j (file, size=4060174)
     63     - d.log (file, size=8033020)
     64     - d.ext (file, size=5626152)
     65     - k (file, size=7214296)
     66 
     67 Here, there are four directories: / (the outermost directory), a and d (which are in /), and e (which is in a). These directories also contain files of various sizes.
     68 
     69 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.)
     70 
     71 The total sizes of the directories above can be found as follows:
     72 
     73     The total size of directory e is 584 because it contains a single file i of size 584 and no other directories.
     74     The directory a has total size 94853 because it contains files f (size 29116), g (size 2557), and h.lst (size 62596), plus file i indirectly (a contains e which contains i).
     75     Directory d has total size 24933642.
     76     As the outermost directory, / contains every file. Its total size is 48381165, the sum of the size of every file.
     77 
     78 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 are a and e; the sum of their total sizes is 95437 (94853 + 584). (As in this example, this process can count files more than once!)
     79 
     80 Find all of the directories with a total size of at most 100000. What is the sum of the total sizes of those directories?
     81 
     82 Your puzzle answer was 1743217.
     83 --- Part Two ---
     84 
     85 Now, you're ready to choose a directory to delete.
     86 
     87 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.
     88 
     89 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.
     90 
     91 To achieve this, you have the following options:
     92 
     93     Delete directory e, which would increase unused space by 584.
     94     Delete directory a, which would increase unused space by 94853.
     95     Delete directory d, which would increase unused space by 24933642.
     96     Delete directory /, which would increase unused space by 48381165.
     97 
     98 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.
     99 
    100 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?
    101 
    102 Your puzzle answer was 8319096.