diff --git a/day7/README.md b/day7/README.md new file mode 100644 index 0000000..398782a --- /dev/null +++ b/day7/README.md @@ -0,0 +1,128 @@ +# 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: + +```shell +$ system-update --please --pretty-please-with-sugar-on-top +Error: No space left on device +``` + +Perhaps you can delete some files to make space for the update? + +You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For +example: + +``` shell +$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k +``` + +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 named x 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 the files and directories immediately contained by the current directory: + - `123 abc` means that the current directory contains a file named `abc` with size `123`. + - `dir xyz` means that the current directory contains a directory named `xyz`. + +Given the commands and output in the example above, you can determine that the filesystem looks visually like this: + +```shell +- / (dir) + - a (dir) + - e (dir) + - i (file, size=584) + - f (file, size=29116) + - g (file, size=2557) + - h.lst (file, size=62596) + - b.txt (file, size=14848514) + - c.dat (file, size=8504156) + - d (dir) + - j (file, size=4060174) + - d.log (file, size=8033020) + - d.ext (file, size=5626152) + - k (file, size=7214296) +``` + +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. + +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` is **584** because it contains a single file `i` of size 584 and no other directories. +- 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`). +- Directory `d` has total size **24933642**. +- As the outermost directory, `/` contains every file. Its total size is **48381165**, 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 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!) + +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: + +- Delete directory `e`, which would increase unused space by `584`. +- Delete directory `a`, which would increase unused space by `94853`. +- Delete directory `d`, which would increase unused space by `24933642`. +- Delete directory `/`, which would increase unused space by `48381165`. + +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?** \ No newline at end of file diff --git a/day7/__init__.py b/day7/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day7/input.txt b/day7/input.txt new file mode 100644 index 0000000..67a6e8b --- /dev/null +++ b/day7/input.txt @@ -0,0 +1,967 @@ +$ cd / +$ ls +dir fcqv +dir fcv +72939 hdpgfcwd +236918 jlncjqh.csz +dir jvwfwrg +dir tzwpllhq +dir vglf +28586 wzljr.zvp +$ cd fcqv +$ ls +dir fhg +277152 qldfrhm.qnr +269351 qsd +dir thbb +$ cd fhg +$ ls +dir jljrdvw +$ cd jljrdvw +$ ls +101940 vsvgjg.tpn +$ cd .. +$ cd .. +$ cd thbb +$ ls +144311 cchngzcc.lcd +178246 fbsj.wtl +48521 gfsqtssb.vtg +284713 jwl.hll +96717 wdqqqv.pcr +$ cd .. +$ cd .. +$ cd fcv +$ ls +dir ffhwwg +275505 fprcz.hrd +179689 jwn +69265 tfnws.drt +38365 twvt.dbs +dir vcqsrw +247592 zldbq +dir znzpm +$ cd ffhwwg +$ ls +dir hlqf +$ cd hlqf +$ ls +30443 vrdbrwp.dss +$ cd .. +$ cd .. +$ cd vcqsrw +$ ls +dir bzlnsjmv +dir rvbtnz +224487 slr +255340 vrdbrwp.dss +$ cd bzlnsjmv +$ ls +272548 qwqlzvzv +$ cd .. +$ cd rvbtnz +$ ls +165670 jwl.lhn +$ cd .. +$ cd .. +$ cd znzpm +$ ls +dir mjwnsw +$ cd mjwnsw +$ ls +219638 bzcb +$ cd .. +$ cd .. +$ cd .. +$ cd jvwfwrg +$ ls +dir ffhwwg +42104 hmphb.qvq +dir hzjpg +dir jwl +dir nmz +243217 qldfrhm.rws +dir tdjtv +dir wdn +$ cd ffhwwg +$ ls +179995 hcbf.dhw +176263 lgssjp.mnl +$ cd .. +$ cd hzjpg +$ ls +241535 ddlcslcs.zjh +dir ddrwfq +dir mqwww +29329 qwqlzvzv +91380 vrdbrwp.dss +dir wctwz +210633 wdqqqv.pcr +dir wlrmbtdg +172051 zldbq +$ cd ddrwfq +$ ls +11756 ffhwwg +$ cd .. +$ cd mqwww +$ ls +149750 wlfmbv.cgp +$ cd .. +$ cd wctwz +$ ls +277720 qldfrhm.bph +$ cd .. +$ cd wlrmbtdg +$ ls +14154 ffhwwg +dir hgdjv +dir qldfrhm +48920 qwqlzvzv +101228 wdqqqv.pcr +159676 zldbq +$ cd hgdjv +$ ls +dir hlw +$ cd hlw +$ ls +229687 cdzwfzww.wcv +$ cd .. +$ cd .. +$ cd qldfrhm +$ ls +254189 nfbp.gcg +$ cd .. +$ cd .. +$ cd .. +$ cd jwl +$ ls +277778 drtw +dir ghgf +dir gqlmq +163875 jwl.pmm +dir mjm +dir qgq +$ cd ghgf +$ ls +181540 zsgjptrm.thf +$ cd .. +$ cd gqlmq +$ ls +dir ffhwwg +283989 hzpwn +dir jwl +dir pqdqntr +62581 vrdbrwp.dss +$ cd ffhwwg +$ ls +242533 qwqlzvzv +21991 wdqqqv.pcr +$ cd .. +$ cd jwl +$ ls +179926 jfgbmb.qsd +dir zjv +$ cd zjv +$ ls +102693 ffhwwg.hrt +$ cd .. +$ cd .. +$ cd pqdqntr +$ ls +159376 nqwcsdmv +$ cd .. +$ cd .. +$ cd mjm +$ ls +273410 hdbmpnfv +8169 qwqlzvzv +87728 tfgq.vbt +221237 vrdbrwp.dss +$ cd .. +$ cd qgq +$ ls +dir dgq +dir hlw +164971 jvwfwrg +dir jwl +158534 qftjvcz.pfv +251561 qzzpmh +282035 wsz +$ cd dgq +$ ls +dir ffhwwg +237291 nfqzn +70142 qfnrvmp.vbb +55607 zldbq +$ cd ffhwwg +$ ls +117790 sqhrdmd.thc +186465 tfctr.scc +236863 wdqqqv.pcr +$ cd .. +$ cd .. +$ cd hlw +$ ls +244567 wrnplfgg.pcw +$ cd .. +$ cd jwl +$ ls +105997 lpsg.jjl +$ cd .. +$ cd .. +$ cd .. +$ cd nmz +$ ls +51802 qwqlzvzv +128617 zldbq +$ cd .. +$ cd tdjtv +$ ls +dir jvwfwrg +dir mdmwf +dir rlpbrq +$ cd jvwfwrg +$ ls +35406 wdqqqv.pcr +$ cd .. +$ cd mdmwf +$ ls +dir ffhwwg +15609 rdpwqw.pfv +205721 zldbq +$ cd ffhwwg +$ ls +117473 hdcv.cwj +$ cd .. +$ cd .. +$ cd rlpbrq +$ ls +120058 dgddvmt.fnv +194704 ffhwwg +256467 qwqlzvzv +$ cd .. +$ cd .. +$ cd wdn +$ ls +170094 bzqsnv +dir gqmghw +150252 gwgz.fnf +243953 jfgbmb.qsd +123382 qwqlzvzv +96220 wpc.swm +dir ztqbhjr +$ cd gqmghw +$ ls +dir tlgbcrh +$ cd tlgbcrh +$ ls +281457 ztjhcrwf.ljf +$ cd .. +$ cd .. +$ cd ztqbhjr +$ ls +271212 jfgbmb.qsd +dir nbhq +dir vfmmj +260079 vrdbrwp.dss +$ cd nbhq +$ ls +179096 hlw.hrh +150184 wdqqqv.pcr +$ cd .. +$ cd vfmmj +$ ls +57445 hlw.pfb +40329 qldfrhm +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tzwpllhq +$ ls +33596 cmwwd +16963 hlw.jrw +161107 hlw.npq +dir jwv +152431 nqf.jjg +dir qldfrhm +dir swmwvl +dir wzpth +$ cd jwv +$ ls +dir bgswd +188795 hlw +dir jvwfwrg +dir jwl +dir mbcb +dir qldfrhm +30173 qwqlzvzv +193577 zldbq +$ cd bgswd +$ ls +dir hdgt +216966 ldbg.dzh +$ cd hdgt +$ ls +70441 fnv.bpl +$ cd .. +$ cd .. +$ cd jvwfwrg +$ ls +dir mctsws +69374 qldfrhm +$ cd mctsws +$ ls +257337 qsgvbj +$ cd .. +$ cd .. +$ cd jwl +$ ls +161061 ffhwwg +$ cd .. +$ cd mbcb +$ ls +dir fbhhq +dir rgjwh +$ cd fbhhq +$ ls +dir vnn +$ cd vnn +$ ls +55629 cthtjm.wbt +$ cd .. +$ cd .. +$ cd rgjwh +$ ls +7826 wrhpmqd.fvw +$ cd .. +$ cd .. +$ cd qldfrhm +$ ls +14471 hlw.lwh +163165 jnpgzcmt +10880 qwqlzvzv +$ cd .. +$ cd .. +$ cd qldfrhm +$ ls +dir ffhwwg +dir jwl +15164 ldmm +188544 pwg.zsf +255566 qjlggt +dir tbngqwb +59898 zldbq +$ cd ffhwwg +$ ls +218749 ffhwwg.ztr +$ cd .. +$ cd jwl +$ ls +40245 cbhtl.frv +268818 fmwvvg +161866 nqp +dir nsvjcpc +dir nwdvblg +171528 qldfrhm +dir qqmb +282266 wdqqqv.pcr +$ cd nsvjcpc +$ ls +43660 wtnhh.zgw +$ cd .. +$ cd nwdvblg +$ ls +dir cltj +dir fds +$ cd cltj +$ ls +86970 dlzsw.whj +111805 qwqlzvzv +247438 zldbq +$ cd .. +$ cd fds +$ ls +49560 fsrvhv.lhd +234970 jvwfwrg.pfd +133376 qgbmc +$ cd .. +$ cd .. +$ cd qqmb +$ ls +139542 qzm.jjz +$ cd .. +$ cd .. +$ cd tbngqwb +$ ls +dir jwl +273706 vcfmt.jqv +76731 wdqqqv.pcr +$ cd jwl +$ ls +45969 ltvvm +$ cd .. +$ cd .. +$ cd .. +$ cd swmwvl +$ ls +32187 dzn.rnz +dir gsj +83763 jfgbmb.qsd +93113 vrdbrwp.dss +$ cd gsj +$ ls +dir fpzj +152202 wntpsp.tbf +$ cd fpzj +$ ls +83665 btwqdhnf.qwz +$ cd .. +$ cd .. +$ cd .. +$ cd wzpth +$ ls +dir snhss +$ cd snhss +$ ls +dir hlw +$ cd hlw +$ ls +15123 qwqlzvzv +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vglf +$ ls +dir gdjmt +dir hnnw +dir htqzfcc +dir nqf +269558 qldfrhm +116732 rqg.zbf +dir tvmqzwmn +72822 vrdbrwp.dss +$ cd gdjmt +$ ls +214742 jfgbmb.qsd +$ cd .. +$ cd hnnw +$ ls +dir ffhwwg +217384 jfgbmb.qsd +153385 jvwfwrg +dir jwl +dir shdt +143935 vrdbrwp.dss +90319 zldbq +101043 zqvwfhrz.rfz +$ cd ffhwwg +$ ls +dir dsqwrdnq +261158 jvwfwrg.plf +$ cd dsqwrdnq +$ ls +129823 pngjr.rpg +$ cd .. +$ cd .. +$ cd jwl +$ ls +dir bzq +215828 hpqtdllh +dir jjmndb +dir pjzqjbd +dir pmslv +dir smfg +dir wmjjwh +$ cd bzq +$ ls +199534 jvwfwrg.tnt +$ cd .. +$ cd jjmndb +$ ls +dir cbfdqtc +dir gnjlnz +dir hlw +201443 lgdvsvc.wvp +dir lzlwstch +175514 nlvh.rjc +dir qldfrhm +144450 qwqlzvzv +dir ssh +dir tbdhtqn +150014 ttp.zqf +240619 wdqqqv.pcr +$ cd cbfdqtc +$ ls +39125 ffhwwg.fpg +264178 jpnslr.qqt +180897 wjmfvtvn +$ cd .. +$ cd gnjlnz +$ ls +217994 jwl.ddh +$ cd .. +$ cd hlw +$ ls +dir chgwjl +dir dsz +165192 ffhwwg.qfc +40771 gzph.mvb +46495 jvwfwrg +122203 lhgfb.blq +175167 qwqlzvzv +dir ttr +$ cd chgwjl +$ ls +284909 jwl.qqp +$ cd .. +$ cd dsz +$ ls +47411 bsshfjl.wbd +$ cd .. +$ cd ttr +$ ls +dir fgbgl +dir qsflswrq +5609 sfz +250311 vrdbrwp.dss +$ cd fgbgl +$ ls +203288 scclvct +$ cd .. +$ cd qsflswrq +$ ls +222611 ffhwwg +143339 thrwtw.nsg +$ cd .. +$ cd .. +$ cd .. +$ cd lzlwstch +$ ls +dir ffhwwg +dir hwmzp +dir jvwfwrg +dir ndpbn +dir qllzhd +$ cd ffhwwg +$ ls +51165 hlw.hdn +$ cd .. +$ cd hwmzp +$ ls +54571 qwqlzvzv +$ cd .. +$ cd jvwfwrg +$ ls +130055 qldfrhm +$ cd .. +$ cd ndpbn +$ ls +160215 hptz.zms +$ cd .. +$ cd qllzhd +$ ls +41306 hwnllpw.vjp +162845 vrdbrwp.dss +$ cd .. +$ cd .. +$ cd qldfrhm +$ ls +dir bwcg +dir ffhwwg +$ cd bwcg +$ ls +181254 wpzpd +$ cd .. +$ cd ffhwwg +$ ls +240618 cdmlz.wsf +dir ffhwwg +201345 jfww +128709 jwl.gwc +284209 pswsrf.wwt +dir qldfrhm +$ cd ffhwwg +$ ls +13278 wcc.qmr +165094 wdqqqv.pcr +$ cd .. +$ cd qldfrhm +$ ls +104797 ccgfmcnc.jrg +273814 hlw.pnt +182685 jvwfwrg.szl +190595 stjhgqd +dir stpvb +105499 vbqw.vwc +dir zldj +$ cd stpvb +$ ls +221672 vrdbrwp.dss +$ cd .. +$ cd zldj +$ ls +dir jwl +$ cd jwl +$ ls +130858 jhqhfrbp.nff +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd ssh +$ ls +65814 jfgbmb.qsd +dir jvwfwrg +70744 zqhftjbs.jwd +$ cd jvwfwrg +$ ls +212356 jfgf.szv +$ cd .. +$ cd .. +$ cd tbdhtqn +$ ls +37228 ffhwwg.vvc +69069 nrtgtbmp.qlv +dir rmrwm +166294 zldbq +$ cd rmrwm +$ ls +118846 qldfrhm.hns +$ cd .. +$ cd .. +$ cd .. +$ cd pjzqjbd +$ ls +259092 cqlfdc.qdp +89322 nmpvmdlp +$ cd .. +$ cd pmslv +$ ls +82637 jwl.lhz +$ cd .. +$ cd smfg +$ ls +dir gfbpcr +dir sghtlzqv +$ cd gfbpcr +$ ls +23958 hnpgr.htf +$ cd .. +$ cd sghtlzqv +$ ls +275943 jfgbmb.qsd +149592 qwqlzvzv +285215 sgptfc.msd +179076 zldbq +$ cd .. +$ cd .. +$ cd wmjjwh +$ ls +198333 jfgbmb.qsd +59553 qwqlzvzv +91883 wdqqqv.pcr +7054 zldbq +$ cd .. +$ cd .. +$ cd shdt +$ ls +27781 hlw.vcl +255994 jpd +dir mrgppm +dir mwvgmjmb +dir nsbrbcq +263384 rbwbqf.qjc +251640 tgr.rdc +16959 zldbq +$ cd mrgppm +$ ls +dir ghzhzzp +dir jwl +dir tgpm +$ cd ghzhzzp +$ ls +dir bqplqq +dir vtqgwsj +$ cd bqplqq +$ ls +234131 lntldzhp +$ cd .. +$ cd vtqgwsj +$ ls +260109 qrzrfzr +$ cd .. +$ cd .. +$ cd jwl +$ ls +112258 ffhwwg.wvp +$ cd .. +$ cd tgpm +$ ls +dir hlw +167788 zldbq +$ cd hlw +$ ls +70960 qwqlzvzv +$ cd .. +$ cd .. +$ cd .. +$ cd mwvgmjmb +$ ls +142087 pvpw.wtv +$ cd .. +$ cd nsbrbcq +$ ls +dir hljjp +$ cd hljjp +$ ls +125853 jfgbmb.qsd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd htqzfcc +$ ls +dir cqs +dir dsnpcnv +dir ffhwwg +90664 jfgbmb.qsd +92316 lsjtfbvt +dir mftm +dir mmgn +dir qqvzvw +166400 qwqlzvzv +dir vtf +dir wrbwbdt +$ cd cqs +$ ls +232548 nnv.wph +35933 rwqj.crp +$ cd .. +$ cd dsnpcnv +$ ls +115805 ffhwwg.tvt +$ cd .. +$ cd ffhwwg +$ ls +dir qldfrhm +dir wjj +$ cd qldfrhm +$ ls +221567 qwqlzvzv +dir rsztzjcm +33248 vrdbrwp.dss +114731 wdqqqv.pcr +276521 zldbq +$ cd rsztzjcm +$ ls +268313 frdpwjsh.wsh +283644 jfgbmb.qsd +dir jhdcdm +84130 jvwfwrg.vsd +31286 jwl.rth +$ cd jhdcdm +$ ls +212461 qwqlzvzv +$ cd .. +$ cd .. +$ cd .. +$ cd wjj +$ ls +89758 jvwfwrg.mmg +163105 vrdbrwp.dss +$ cd .. +$ cd .. +$ cd mftm +$ ls +278389 bbth.bht +$ cd .. +$ cd mmgn +$ ls +28122 zldbq +$ cd .. +$ cd qqvzvw +$ ls +dir mjw +dir qldfrhm +dir zbmpdwtc +$ cd mjw +$ ls +dir cvn +dir dlgzc +10631 jfgbmb.qsd +206351 qldfrhm.hgf +136824 qwqlzvzv +116870 qwzczrcf +$ cd cvn +$ ls +146142 ggtd.jlp +$ cd .. +$ cd dlgzc +$ ls +130120 wpc +$ cd .. +$ cd .. +$ cd qldfrhm +$ ls +260238 bpf.jbv +270180 cwdnf +dir czwwzj +50884 jvwfwrg.fmq +dir mjpzgbww +252028 qtdzw.bmb +dir rzqj +$ cd czwwzj +$ ls +dir cpwwqrph +19863 jfgbmb.qsd +dir snl +83915 vrdbrwp.dss +$ cd cpwwqrph +$ ls +dir jvwfwrg +$ cd jvwfwrg +$ ls +176717 jfgbmb.qsd +$ cd .. +$ cd .. +$ cd snl +$ ls +dir btngcvt +dir ffhwwg +53428 gchwbc.jtf +220232 vrdbrwp.dss +$ cd btngcvt +$ ls +172859 jwl.smp +dir tgcqmbrn +$ cd tgcqmbrn +$ ls +149905 jvwfwrg +$ cd .. +$ cd .. +$ cd ffhwwg +$ ls +145399 qldfrhm.hbg +$ cd .. +$ cd .. +$ cd .. +$ cd mjpzgbww +$ ls +52355 bwzwfjtc.rmn +5856 ffhwwg.tnh +146211 scbdnlgl +$ cd .. +$ cd rzqj +$ ls +dir jmjszd +134481 vgr +187382 vrdbrwp.dss +$ cd jmjszd +$ ls +81971 qldfrhm +$ cd .. +$ cd .. +$ cd .. +$ cd zbmpdwtc +$ ls +193744 jvntrn +231403 qwqlzvzv +$ cd .. +$ cd .. +$ cd vtf +$ ls +2562 vzngcbq +$ cd .. +$ cd wrbwbdt +$ ls +dir brzwbmc +29261 hlw +$ cd brzwbmc +$ ls +dir ggh +174287 jfgbmb.qsd +dir jwl +247329 mpdz +dir thm +83546 vrdbrwp.dss +231269 wmwhhhmn.rms +$ cd ggh +$ ls +dir cqrmsltg +18724 mrlwwdv.ffb +$ cd cqrmsltg +$ ls +dir ffhwwg +dir jvwfwrg +41263 pmrmh.zmn +dir tcjr +$ cd ffhwwg +$ ls +dir hlw +$ cd hlw +$ ls +139875 ljnfchr.frr +$ cd .. +$ cd .. +$ cd jvwfwrg +$ ls +141078 zldbq +$ cd .. +$ cd tcjr +$ ls +dir hnngpdc +153685 jwl +93502 nwlsf.dbq +226658 qwqlzvzv +$ cd hnngpdc +$ ls +243203 gchtc.sst +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jwl +$ ls +62087 hfp.cfv +$ cd .. +$ cd thm +$ ls +228008 dfqgcd.frc +167850 glblcw +dir jvwfwrg +$ cd jvwfwrg +$ ls +dir ffhwwg +dir jgvf +170488 qmc.vgf +$ cd ffhwwg +$ ls +258985 jfgbmb.qsd +$ cd .. +$ cd jgvf +$ ls +150671 rdplqftl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd nqf +$ ls +dir rglrpqd +$ cd rglrpqd +$ ls +276785 nnbs.dhr +224128 qwqlzvzv +$ cd .. +$ cd .. +$ cd tvmqzwmn +$ ls +47655 jfgbmb.qsd +233646 qwqlzvzv +237245 rqfp +185131 whntfvm.sfz \ No newline at end of file diff --git a/day7/main.py b/day7/main.py new file mode 100644 index 0000000..47ca491 --- /dev/null +++ b/day7/main.py @@ -0,0 +1,115 @@ +from dataclasses import dataclass, field +from typing import Self + +from common.file import read_data + +DIRECTORY = "directory" +FILE = "file" + +test_data = [ + "$ cd /", + "$ ls", + "dir a", + "14848514 b.txt", + "8504156 c.dat", + "dir d", + "$ cd a", + "$ ls", + "dir e", + "29116 f", + "2557 g", + "62596 h.lst", + "$ cd e", + "$ ls", + "584 i", + "$ cd ..", + "$ cd ..", + "$ cd d", + "$ ls", + "4060174 j", + "8033020 d.log", + "5626152 d.ext", + "7214296 k", +] + + +@dataclass +class Node: + node_type: str + name: str + _size: int = 0 + children: list[Self] = field(default_factory=list) + parent: Self | None = None + + def add(self, leaf: Self): + leaf.parent = self + self.children.append(leaf) + + @property + def size(self) -> int: + if self.node_type == DIRECTORY: + self.size = sum(child.size for child in self.children) + return self._size + + @size.setter + def size(self, value: int): + self._size = value + + +def new_leaf(command: str) -> Node: + a, b = command.split(" ") + + if a == "dir": + node_type = DIRECTORY + name = b + size = 0 + else: + node_type = FILE + name = b + size = int(a) + + node = Node(node_type=node_type, name=name) + node.size = size + return node + + +def move_to(tree: Node, directory: str) -> Node: + if directory == "..": + return tree.parent + return [node for node in tree.children if node.name == directory][0] + + +def build_tree(data: list[str]) -> int: + root = Node(node_type=DIRECTORY, name="/") + current = root + + directories = [] + + i = 1 + while i < len(data[1:]): + if "$ cd" in data[i]: + _, _, directory = data[i].split(" ") + current = move_to(current, directory) + i += 1 + if "$ ls" in data[i]: + i += 1 + while i <= len(data[1:]) and data[i][0] != "$": + leaf = new_leaf(data[i]) + if leaf.node_type == DIRECTORY: + directories.append(leaf) + current.add(leaf) + i += 1 + return sum(directory.size for directory in directories if directory.size < 100_000) + + +def total_files_size(data: list[str]) -> int: + return build_tree(data) + + +if __name__ == "__main__": + dataset = read_data() + + res = total_files_size(test_data) + assert res == 95437, f"{res} is not the right value, want 95437" + + print(total_files_size(dataset))