From 8905c1db37bb1698430ce3d25ff89cc9487dce91 Mon Sep 17 00:00:00 2001 From: Jasper Bok Date: Fri, 16 Dec 2022 19:34:27 +0100 Subject: [PATCH] Add solution for day 7 --- day7/input.txt | 1013 ++++++++++++++++++++++++++++++++++++++++++++ day7/part1.py | 139 ++++++ day7/part2.py | 64 +++ day7/testinput.txt | 23 + day7/utils.py | 90 ++++ 5 files changed, 1329 insertions(+) create mode 100644 day7/input.txt create mode 100644 day7/part1.py create mode 100644 day7/part2.py create mode 100644 day7/testinput.txt create mode 100644 day7/utils.py diff --git a/day7/input.txt b/day7/input.txt new file mode 100644 index 0000000..c988c21 --- /dev/null +++ b/day7/input.txt @@ -0,0 +1,1013 @@ +$ cd / +$ ls +dir jmtrrrp +dir jssnn +dir lbrmb +11968 pcccp +$ cd jmtrrrp +$ ls +77968 chq.jvb +dir fmgsql +$ cd fmgsql +$ ls +dir dbnsfp +dir vvp +$ cd dbnsfp +$ ls +51021 crlq.lrj +186829 dhcrzvbr.wmn +16232 fvhn.fqm +54150 qpbqqj.rpg +$ cd .. +$ cd vvp +$ ls +179105 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd .. +$ cd jssnn +$ ls +dir bphfqs +dir dbnsfp +dir pcccp +dir snr +dir zjbvwsnv +$ cd bphfqs +$ ls +110077 dhcrzvbr.wmn +$ cd .. +$ cd dbnsfp +$ ls +dir hgvh +dir jtqdcmsz +154197 rrcsndz.tzp +$ cd hgvh +$ ls +dir qjnbg +$ cd qjnbg +$ ls +dir bqzfpr +$ cd bqzfpr +$ ls +124394 wjsbsp +$ cd .. +$ cd .. +$ cd .. +$ cd jtqdcmsz +$ ls +275597 dbnsfp.fpg +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir cnbd +85621 cqzvwl +dir dbnsfp +114355 hbhp.cfv +dir mcgq +dir pcccp +dir qpbqqj +224038 rrcsndz.tzp +dir zcsm +27570 zjbvwsnv.fjt +$ cd cnbd +$ ls +dir jrbz +dir pphv +$ cd jrbz +$ ls +dir dwvlwfq +$ cd dwvlwfq +$ ls +32237 fwclr.rnb +$ cd .. +$ cd .. +$ cd pphv +$ ls +180370 dhcrzvbr.wmn +50154 dzvwdwl.gbt +123965 mlsv.hlw +163116 wnhtwr.mwl +$ cd .. +$ cd .. +$ cd dbnsfp +$ ls +252181 btv.mpv +dir hwncj +dir pcccp +$ cd hwncj +$ ls +51410 jbd.fcm +$ cd .. +$ cd pcccp +$ ls +258123 chq.jvb +$ cd .. +$ cd .. +$ cd mcgq +$ ls +206506 qpbqqj.bbb +$ cd .. +$ cd pcccp +$ ls +193219 ddhtnql.hmb +134114 hjbpzqzb.rwn +108927 lznndn.nqd +73241 ncdrv +$ cd .. +$ cd qpbqqj +$ ls +dir crdt +dir tgchdnc +$ cd crdt +$ ls +205710 chq.jvb +$ cd .. +$ cd tgchdnc +$ ls +dir bdw +dir dpl +dir jssnn +dir pcccp +dir plpzbm +$ cd bdw +$ ls +211300 dbnsfp.tjm +$ cd .. +$ cd dpl +$ ls +287744 rsbjqwm +$ cd .. +$ cd jssnn +$ ls +dir jssnn +$ cd jssnn +$ ls +9644 hmjhshg.vbv +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir jssnn +85888 pcccp.hdj +dir qpbqqj +dir rmscmwtv +$ cd jssnn +$ ls +129698 crlq.lrj +7499 dhcrzvbr.wmn +283607 qpbqqj.djr +234874 wqnrhll +$ cd .. +$ cd qpbqqj +$ ls +184229 qqpb.ftd +$ cd .. +$ cd rmscmwtv +$ ls +188048 dhcrzvbr.wmn +dir jwtpgbnt +$ cd jwtpgbnt +$ ls +209946 hgg +$ cd .. +$ cd .. +$ cd .. +$ cd plpzbm +$ ls +32627 tlb.qmc +$ cd .. +$ cd .. +$ cd .. +$ cd zcsm +$ ls +dir lczflft +dir zjbvwsnv +dir zmh +$ cd lczflft +$ ls +40043 dzgnvlw.scr +dir lrnb +$ cd lrnb +$ ls +109881 bjpbs +dir jssnn +46901 npmw +$ cd jssnn +$ ls +9216 sgrp +$ cd .. +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +214676 jssnn.hgn +$ cd .. +$ cd zmh +$ ls +dir jdt +dir rggpltr +$ cd jdt +$ ls +147387 jhhsv +90052 jssnn.wns +53105 qpbqqj.dzq +$ cd .. +$ cd rggpltr +$ ls +121454 dbnsfp.dzt +dir gcc +$ cd gcc +$ ls +dir zbqwl +dir zjbvwsnv +$ cd zbqwl +$ ls +260297 pcccp.jrw +$ cd .. +$ cd zjbvwsnv +$ ls +248709 pcccp.tph +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd snr +$ ls +152569 chq.jvb +1437 crlq.lrj +$ cd .. +$ cd zjbvwsnv +$ ls +dir cqhb +53235 ghhtl.bhv +199640 npcfdw +136346 qpbqqj.lmv +dir zjbvwsnv +$ cd cqhb +$ ls +24712 sqqf +$ cd .. +$ cd zjbvwsnv +$ ls +dir gzqg +dir hfbfvn +dir srsphr +dir vgvdcvc +$ cd gzqg +$ ls +dir jjw +$ cd jjw +$ ls +240052 zdcjjz.pmg +$ cd .. +$ cd .. +$ cd hfbfvn +$ ls +278190 bfgndw.pvf +$ cd .. +$ cd srsphr +$ ls +42591 zjbvwsnv.hgh +$ cd .. +$ cd vgvdcvc +$ ls +120322 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd lbrmb +$ ls +dir bjhpdj +42241 crlq.lrj +dir dbnsfp +244610 dhcrzvbr.wmn +dir hppb +dir mcnzs +dir npntsr +13625 tpjpcsgp.dlz +219424 vvpbt.zvf +dir zjbvwsnv +191467 zjbvwsnv.htn +$ cd bjhpdj +$ ls +dir bqjvst +204722 dbnsfp +dir dhltrqqq +226082 dmdqcjp +dir fcqwgzp +dir jssnn +6453 jssnn.ndh +23799 jssnn.zqn +dir nwglfhpl +dir pcccp +dir pdnj +269246 shzqns.nws +dir sjstqlcb +dir zssln +$ cd bqjvst +$ ls +202793 dbnsfp.pjj +259783 jssnn +dir rbvbhnvs +30683 rvddnjmb.tlz +dir tzhslnv +$ cd rbvbhnvs +$ ls +86934 vrtrf.htt +$ cd .. +$ cd tzhslnv +$ ls +76278 mghcwdlr.tsc +$ cd .. +$ cd .. +$ cd dhltrqqq +$ ls +dir mfd +dir pcccp +dir smmb +251164 wsdnsgtt.lhr +191876 zvr.bbz +$ cd mfd +$ ls +51017 crlq.lrj +99213 rjtbnnnq.hgd +$ cd .. +$ cd pcccp +$ ls +160487 dhcrzvbr.wmn +dir nhdrnthj +dir qpbqqj +$ cd nhdrnthj +$ ls +181291 bbn.wtm +186551 fnw.tnn +23622 rrcsndz.tzp +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +227547 dhcrzvbr.wmn +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +212353 crlq.lrj +170195 dhcrzvbr.wmn +dir ttvp +$ cd ttvp +$ ls +185994 tgjcfgjv +$ cd .. +$ cd .. +$ cd .. +$ cd smmb +$ ls +dir dbnsfp +85354 dbnsfp.zpn +80665 dfmmjbm.rnr +135989 dhcrzvbr.wmn +93718 lrbzr.nfs +dir mjpfnfns +dir nsdpfnhb +dir pmnssvd +32270 qpbqqj.vtd +$ cd dbnsfp +$ ls +31796 gzs.rgv +64506 vbjncw.bpz +181659 vjlfrdp.tqh +$ cd .. +$ cd mjpfnfns +$ ls +231611 chq.jvb +17518 cmnlrzq.hvh +144795 dbnsfp +162194 jssnn.wjz +29305 vdqnlw.fzf +$ cd .. +$ cd nsdpfnhb +$ ls +281844 chq.jvb +$ cd .. +$ cd pmnssvd +$ ls +165816 dfvl.czb +144561 gbn +150785 lnzdwrmb +111214 rrcsndz.tzp +164156 tzgdb.hht +$ cd .. +$ cd .. +$ cd .. +$ cd fcqwgzp +$ ls +199161 dhcrzvbr.wmn +34251 rrcsndz.tzp +198345 vjlfrdp.tqh +167001 zjbvwsnv.bsd +$ cd .. +$ cd jssnn +$ ls +dir ccblfvl +103180 dhcrzvbr.wmn +dir prw +dir tzqfn +dir zjbvwsnv +166467 zjbvwsnv.tdt +$ cd ccblfvl +$ ls +159752 crlq.lrj +20805 jssnn.dvb +243040 lct.zll +27492 qbh +27174 vjlfrdp.tqh +dir zvfwq +$ cd zvfwq +$ ls +135126 chq.jvb +41664 gphw.vzd +dir hmrdghbr +dir jssnn +dir qzzb +dir tmdlcv +$ cd hmrdghbr +$ ls +dir jvgpwrbs +$ cd jvgpwrbs +$ ls +dir wzdv +$ cd wzdv +$ ls +26834 qpbqqj.njf +$ cd .. +$ cd .. +$ cd .. +$ cd jssnn +$ ls +90199 jqqmqddf.qnz +$ cd .. +$ cd qzzb +$ ls +dir mgpql +dir src +dir zvdgc +$ cd mgpql +$ ls +141852 qpbqqj +$ cd .. +$ cd src +$ ls +204425 lqmcbndm.jrj +75571 qsbrsv.jcm +$ cd .. +$ cd zvdgc +$ ls +268742 ffjmrmmz.lhg +18385 rvmp.hjv +$ cd .. +$ cd .. +$ cd tmdlcv +$ ls +182587 sfwvjrj.mzl +$ cd .. +$ cd .. +$ cd .. +$ cd prw +$ ls +207429 dbnsfp.rqf +dir ptgn +dir pzgpqp +252902 rbt +169694 trg.rsh +$ cd ptgn +$ ls +dir jssnn +dir qpbqqj +dir rpd +$ cd jssnn +$ ls +189316 dbnsfp.bqc +$ cd .. +$ cd qpbqqj +$ ls +167937 zjbvwsnv.bhz +$ cd .. +$ cd rpd +$ ls +8775 crlq.lrj +$ cd .. +$ cd .. +$ cd pzgpqp +$ ls +dir pcccp +$ cd pcccp +$ ls +51496 pcccp +$ cd .. +$ cd .. +$ cd .. +$ cd tzqfn +$ ls +dir cbpfvdp +285700 crlq.lrj +7426 dbnsfp.fsd +dir gdl +141367 jssnn.hmw +184482 sczphnp.vnc +126288 vjlfrdp.tqh +dir wndpdj +$ cd cbpfvdp +$ ls +dir cvfr +dir qpbqqj +$ cd cvfr +$ ls +dir jfrnvts +dir qpbqqj +$ cd jfrnvts +$ ls +dir vwdn +$ cd vwdn +$ ls +236936 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +254275 bqd +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +dir jssnn +201960 qpbqqj +$ cd jssnn +$ ls +131127 jssnn +$ cd .. +$ cd .. +$ cd .. +$ cd gdl +$ ls +225146 hsgzmtp.wcs +204436 jssnn.lhh +64007 mjzjgfg.jsb +$ cd .. +$ cd wndpdj +$ ls +245412 bvcq +211386 dbnsfp.tqd +186962 fql.mww +dir hlmhtfz +117446 smvjvcn.lcp +$ cd hlmhtfz +$ ls +150152 lrdhbq.rvm +$ cd .. +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +179703 fvmbz +87552 qtz.ccw +129764 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd nwglfhpl +$ ls +66039 crlq.lrj +dir cwq +dir dlgrsw +267814 frhlttn.nmd +dir hmprt +dir qpbqqj +dir wnfzznfh +$ cd cwq +$ ls +77655 cpjnwzh +dir pcccp +dir zjbvwsnv +dir zzhjfmnr +$ cd pcccp +$ ls +dir pcccp +$ cd pcccp +$ ls +245309 bggzbrg.flf +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +196915 gnmfb.dzq +dir ngqbdqp +$ cd ngqbdqp +$ ls +355 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd zzhjfmnr +$ ls +dir dbnsfp +$ cd dbnsfp +$ ls +223184 chq.jvb +$ cd .. +$ cd .. +$ cd .. +$ cd dlgrsw +$ ls +181906 chq.jvb +5636 dbnsfp +219889 jbr.slc +dir zrntbl +$ cd zrntbl +$ ls +61864 brnpgpwt +138980 qpbqqj +$ cd .. +$ cd .. +$ cd hmprt +$ ls +90249 dbnsfp.mbd +$ cd .. +$ cd qpbqqj +$ ls +290377 crlq.lrj +$ cd .. +$ cd wnfzznfh +$ ls +83022 hclmps +64095 zhm +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir rdzntr +dir rvccq +$ cd rdzntr +$ ls +239028 rrcsndz.tzp +$ cd .. +$ cd rvccq +$ ls +22746 chq.jvb +288752 jjvppq.swt +dir msgwsnjq +dir pggz +228469 vjlfrdp.tqh +$ cd msgwsnjq +$ ls +102522 lwgqc.mhv +25239 ndm.llf +$ cd .. +$ cd pggz +$ ls +dir cnjqsqj +$ cd cnjqsqj +$ ls +229407 shpnq +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pdnj +$ ls +193069 rwnhgttz.pvp +$ cd .. +$ cd sjstqlcb +$ ls +263295 chq.jvb +224091 jss.wtr +$ cd .. +$ cd zssln +$ ls +5859 ncdlcr.dll +$ cd .. +$ cd .. +$ cd dbnsfp +$ ls +271252 dhcrzvbr.wmn +$ cd .. +$ cd hppb +$ ls +259968 jssnn +81292 qpqqb.clj +$ cd .. +$ cd mcnzs +$ ls +170903 crlq.lrj +59482 dhcrzvbr.wmn +dir dqzwzbgm +dir gnrztn +286736 jssnn.jcm +32791 phqsgl +dir pzjnrwt +197323 vjlfrdp.tqh +dir wvnwbpct +$ cd dqzwzbgm +$ ls +78575 qpbqqj +251546 qpbqqj.slb +$ cd .. +$ cd gnrztn +$ ls +158603 hdnwmd.rhj +dir nbfdtwzr +178239 ptnchzpg +40517 rrcsndz.tzp +dir smvb +198007 vjlfrdp.tqh +$ cd nbfdtwzr +$ ls +200354 crlq.lrj +$ cd .. +$ cd smvb +$ ls +163921 zjbvwsnv.brz +$ cd .. +$ cd .. +$ cd pzjnrwt +$ ls +33891 lwrll +259646 pcccp.sfn +106835 pqfzthjq +189673 rrcsndz.tzp +$ cd .. +$ cd wvnwbpct +$ ls +234188 dhcrzvbr.wmn +dir gmtpsgv +86379 jssnn +146663 sfpmdbbd.jvt +25795 vjlfrdp.tqh +$ cd gmtpsgv +$ ls +18642 chq.jvb +3046 cznlwtw +26335 ddgpngrc +116455 vnnls.hsg +$ cd .. +$ cd .. +$ cd .. +$ cd npntsr +$ ls +dir cccjdcvb +206657 chq.jvb +280518 crlq.lrj +dir dbnsfp +dir jphnn +dir jssnn +dir mpl +195193 rrcsndz.tzp +dir rztc +dir znwp +$ cd cccjdcvb +$ ls +192965 mcr.sfq +$ cd .. +$ cd dbnsfp +$ ls +dir gfns +173317 jssnn.tjq +dir mgr +68817 mvwcwfcr.zmz +dir pqfht +108571 swfl.dtj +10398 tvvvv +dir vzg +174361 zjbvwsnv +$ cd gfns +$ ls +203999 zjbvwsnv.hfg +$ cd .. +$ cd mgr +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +26871 tqlgcf.jrn +$ cd .. +$ cd .. +$ cd pqfht +$ ls +199590 clpvscl.rlm +dir dwlhv +dir vhzfzhrb +$ cd dwlhv +$ ls +130761 qpbqqj +242752 rrcsndz.tzp +$ cd .. +$ cd vhzfzhrb +$ ls +dir njdgcbvm +$ cd njdgcbvm +$ ls +dir snjfqg +$ cd snjfqg +$ ls +dir qpwh +$ cd qpwh +$ ls +153353 qsjpj +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vzg +$ ls +dir pcccp +$ cd pcccp +$ ls +dir jfbtl +$ cd jfbtl +$ ls +209199 dbnsfp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jphnn +$ ls +52305 crlq.lrj +193480 gmms.whz +59354 nmq.dww +64638 qpbqqj +47072 rrcsndz.tzp +$ cd .. +$ cd jssnn +$ ls +69168 crlq.lrj +1549 dhcrzvbr.wmn +219596 hdmczg.lmm +108063 jssnn +24327 vjlfrdp.tqh +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +189952 chq.jvb +$ cd .. +$ cd .. +$ cd mpl +$ ls +144856 bqrrzm +249487 crlq.lrj +dir ffqgpgfg +93632 flqwtn.nsz +dir mwpcqr +195910 pdqwn.lcg +$ cd ffqgpgfg +$ ls +66459 dbnsfp +200500 lcmt.zmz +207093 qpbqqj +77042 vjlfrdp.tqh +57109 wwzv.hbn +$ cd .. +$ cd mwpcqr +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +166393 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd .. +$ cd rztc +$ ls +57788 chq.jvb +$ cd .. +$ cd znwp +$ ls +164627 chq.jvb +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +dir dgrrl +71529 jssnn +198617 pcccp.qqh +dir phggn +56842 zjbvwsnv.vqd +$ cd dgrrl +$ ls +dir czm +dir fhhlbdlz +dir gstjw +dir qpbqqj +dir stgb +$ cd czm +$ ls +dir jssnn +$ cd jssnn +$ ls +162335 chq.jvb +30099 mfdgdw +96389 pcdsd.wmw +251423 tmz.lcb +205979 vpltdt.gtv +$ cd .. +$ cd .. +$ cd fhhlbdlz +$ ls +dir qpbqqj +dir vdjs +dir zgz +$ cd qpbqqj +$ ls +285561 chq.jvb +263924 lbqcfdrs +138854 pcccp.dtn +$ cd .. +$ cd vdjs +$ ls +32688 chq.jvb +223233 tbn.blt +$ cd .. +$ cd zgz +$ ls +92804 bqltmv.wzb +$ cd .. +$ cd .. +$ cd gstjw +$ ls +151784 fvfszzzn.cbh +$ cd .. +$ cd qpbqqj +$ ls +dir blztqf +dir plgnh +$ cd blztqf +$ ls +195097 wlvmtz +$ cd .. +$ cd plgnh +$ ls +dir dbnsfp +246221 dhcrzvbr.wmn +271121 jhwmmzls.mhw +170162 pcccp.dpp +37872 qpbqqj +$ cd dbnsfp +$ ls +dir dhpnr +$ cd dhpnr +$ ls +152837 pcccp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd stgb +$ ls +248436 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd phggn +$ ls +284602 dhcrzvbr.wmn +dir lctr +dir rjmc +66651 rrcsndz.tzp +117525 vth.fgw +156877 wcqnjzbq.dgf +7803 zpsrzclh.bzw +$ cd lctr +$ ls +212339 jssnn.whp +dir jzhcqb +99974 pcccp.zhs +111354 pmc +104899 vjlfrdp.tqh +93496 zhwmbw +$ cd jzhcqb +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +146807 rbrg +$ cd .. +$ cd .. +$ cd .. +$ cd rjmc +$ ls +dir fvbmsc +139747 glwmr.lrg +dir gvnnz +102023 tbj.qmz +dir vsztsjfh +$ cd fvbmsc +$ ls +136838 vpvbz.qtw +$ cd .. +$ cd gvnnz +$ ls +95498 zjbvwsnv +$ cd .. +$ cd vsztsjfh +$ ls +215479 ffwlcrwb diff --git a/day7/part1.py b/day7/part1.py new file mode 100644 index 0000000..6200421 --- /dev/null +++ b/day7/part1.py @@ -0,0 +1,139 @@ +""" +--- 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: + +$ 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: + + $ 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 of 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: + +- / (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? +""" +from utils import Dir, parse_commands_to_tree, print_tree + + +def main(): + with open("input.txt", "r", encoding="utf-8") as input_file: + tree = parse_commands_to_tree(input_file.read().split("\n")) + + print_tree(tree) + + def find_large_dirs(directory): + total_size = 0 + + for thing in directory.contents: + if isinstance(thing, Dir): + if thing.size < 100000: + total_size += thing.size + total_size += find_large_dirs(thing) + + return total_size + + total_size = find_large_dirs(tree) + print(f"Total size: {total_size}") + + +if __name__ == "__main__": + main() diff --git a/day7/part2.py b/day7/part2.py new file mode 100644 index 0000000..cbd229d --- /dev/null +++ b/day7/part2.py @@ -0,0 +1,64 @@ +""" +--- 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? +""" +from utils import Dir, parse_commands_to_tree, print_tree + + +DISK_SIZE = 70000000 +REQUIRED_SPACE = 30000000 + + +def main(): + with open("input.txt", "r", encoding="utf-8") as input_file: + tree = parse_commands_to_tree(input_file.read().split("\n")) + + used_disk_space = tree.size + additional_required_space = REQUIRED_SPACE - (DISK_SIZE - used_disk_space) + + def find_candidates(directory): + candidates = [] + + for thing in directory.contents: + if isinstance(thing, Dir): + if thing.size >= additional_required_space: + candidates.append(thing) + candidates.extend(find_candidates(thing)) + + return candidates + + candidates = find_candidates(tree) + smallest_size = min([candidate.size for candidate in candidates]) + print(f"The smallest size is {smallest_size}") + + +if __name__ == "__main__": + main() diff --git a/day7/testinput.txt b/day7/testinput.txt new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/day7/testinput.txt @@ -0,0 +1,23 @@ +$ 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 diff --git a/day7/utils.py b/day7/utils.py new file mode 100644 index 0000000..862e042 --- /dev/null +++ b/day7/utils.py @@ -0,0 +1,90 @@ +class File: + def __init__(self, name: str, size: int): + self.name = name + self.size = size + self.parent = None + + def __str__(self): + return self.name + + +class Dir: + def __init__(self, name: str): + self.name = name + self.contents = [] + self.parent = None + + def __str__(self): + return self.name + + def add(self, thing): + thing.parent = self + self.contents.append(thing) + + def get_dir(self, name: str): + for thing in self.contents: + if isinstance(thing, Dir) and thing.name == name: + return thing + return None + + @property + def size(self): + return sum([thing.size for thing in self.contents]) + + +def parse_commands_to_tree(commands): + listing_contents = False + + root = Dir("") + cur_dir = root + + for line in commands: + if not line.split(): + continue + + words = line.split() + if words[0] == "$": + listing_contents = False + + command = words[1] + try: + arg = words[2] + except IndexError: + arg = "" + + if command == "cd": + if arg == "..": + cur_dir = cur_dir.parent + elif arg == ".": + continue + else: + if not cur_dir.get_dir(arg): + cur_dir.add(Dir(name=arg)) + + cur_dir = cur_dir.get_dir(arg) + elif command == "ls": + listing_contents = True + elif listing_contents: + dir_or_size, name = words + if dir_or_size == "dir": + cur_dir.add(Dir(name=name)) + else: + cur_dir.add(File(name=name, size=int(dir_or_size))) + else: + raise Exception(f"Unknown command: {line}") + + return root + + +def print_tree(tree): + output = [] + + def parse_directory(directory, depth): + for thing in directory.contents: + output.append("{}{} ({})".format(depth * 2 * " ", thing.name, thing.size)) + if isinstance(thing, Dir): + parse_directory(thing, depth + 1) + + parse_directory(tree, 1) + + print("\n".join(output))