M .gitignore => .gitignore +2 -0
@@ 1,1 1,3 @@
*/main
+*/Cargo.lock
+*/target
M README.md => README.md +5 -0
@@ 23,3 23,8 @@ rust-gdb main # run program with gdb debugger
- fix with `elf1.rsplit('-').rev().collect()`
- https://brookbach.com/blog/2016/11/30/reverse-iterator-in-rust.html
+## Day 7
+
+```rust
+cd day7 && cargo build && ./target/debug/aoc_day7
+```
A day7/Cargo.toml => day7/Cargo.toml +7 -0
@@ 0,0 1,7 @@
+[package]
+name = "aoc_day7"
+version = "0.0.1"
+authors = [ "Anjandev Momi <anjan@momi.ca>" ]
+
+[dependencies]
+indextree = "4.5.0"
A day7/input => day7/input +1027 -0
@@ 0,0 1,1027 @@
+$ cd /
+$ ls
+268495 jgfbgjdb
+dir ltcqgnc
+272455 pct.bbd
+200036 phthcq
+174378 qld
+dir rbmstsf
+130541 trhbvp.fmm
+dir twjcmp
+$ cd ltcqgnc
+$ ls
+227634 brjmpbfg.hjh
+dir cmdzcq
+dir dnbf
+203609 fpj
+dir frt
+288222 lcr.nlr
+dir ngm
+dir ngsrlzc
+5927 phrnnw.dzj
+dir shjv
+dir wsvfbb
+$ cd cmdzcq
+$ ls
+37316 cfvhc.qsw
+41839 mgwlr
+dir pfmbt
+281659 tlpqzz
+dir vmd
+189667 zdvj.sbl
+$ cd pfmbt
+$ ls
+120151 bhzsnw.gft
+45035 cfvhc.zpp
+$ cd ..
+$ cd vmd
+$ ls
+dir dnc
+dir rgftffp
+$ cd dnc
+$ ls
+dir dvndwjzs
+216199 fzwrr.ndp
+dir gwhtzlpg
+dir lsfn
+dir lzcv
+237080 qld
+131509 rwhffw.ldj
+84024 wph
+$ cd dvndwjzs
+$ ls
+264750 htjvzrv.plg
+$ cd ..
+$ cd gwhtzlpg
+$ ls
+dir cfvhc
+285900 fpj
+112604 gqng.zww
+188180 phthcq
+dir prgbvj
+$ cd cfvhc
+$ ls
+136528 bbsmm
+$ cd ..
+$ cd prgbvj
+$ ls
+180538 hfgg.fbm
+$ cd ..
+$ cd ..
+$ cd lsfn
+$ ls
+133375 fzwrr.ndp
+242940 gcftj.nlb
+$ cd ..
+$ cd lzcv
+$ ls
+121565 plnqnqq.zwr
+100302 wdmqc
+$ cd ..
+$ cd ..
+$ cd rgftffp
+$ ls
+259406 rwhffw
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd dnbf
+$ ls
+dir bft
+219468 czjvl.brj
+130503 fpj
+120522 rwhffw.hrn
+92338 sngz.qsd
+dir vpmvnmfj
+$ cd bft
+$ ls
+dir zdvj
+$ cd zdvj
+$ ls
+50020 sngz.qsd
+$ cd ..
+$ cd ..
+$ cd vpmvnmfj
+$ ls
+78395 dlch.vfb
+270867 hnmq
+172551 pct.bbd
+186790 phthcq
+65750 sjjlnf.bjn
+$ cd ..
+$ cd ..
+$ cd frt
+$ ls
+dir bbsmm
+dir cfvhc
+22983 dpvmzlv
+dir fptlqh
+290097 fzwrr.ndp
+dir hvwdzbsw
+dir nlm
+57046 pct.bbd
+208565 qld.mms
+261556 wcfvpmz
+$ cd bbsmm
+$ ls
+dir bbsmm
+238522 hmjvg.pfn
+228873 nvvsml.bcg
+dir rwhffw
+dir sgbnzbqt
+$ cd bbsmm
+$ ls
+dir lnz
+dir msmpndgh
+dir ncpbn
+dir wfr
+$ cd lnz
+$ ls
+99970 fjbmpmrz.ldw
+$ cd ..
+$ cd msmpndgh
+$ ls
+dir sfsqwrhg
+$ cd sfsqwrhg
+$ ls
+dir vfqg
+$ cd vfqg
+$ ls
+17855 bbsmm
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ncpbn
+$ ls
+dir nltglc
+$ cd nltglc
+$ ls
+247577 qld.vjz
+$ cd ..
+$ cd ..
+$ cd wfr
+$ ls
+135424 bbps
+243274 hsvjddff.nfw
+dir prg
+158111 qld.zff
+dir rwhffw
+$ cd prg
+$ ls
+214834 lfpqlt.bdt
+dir nclpwh
+104870 phthcq
+dir trrtpgd
+$ cd nclpwh
+$ ls
+104441 mjjftn.sqt
+$ cd ..
+$ cd trrtpgd
+$ ls
+137542 qld
+$ cd ..
+$ cd ..
+$ cd rwhffw
+$ ls
+dir glsrf
+$ cd glsrf
+$ ls
+189772 phthcq
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rwhffw
+$ ls
+dir bbsmm
+$ cd bbsmm
+$ ls
+104690 cfvhc.nwc
+$ cd ..
+$ cd ..
+$ cd sgbnzbqt
+$ ls
+191204 cfvhc.jnv
+127263 vwfbr
+$ cd ..
+$ cd ..
+$ cd cfvhc
+$ ls
+72894 bbsmm.dns
+$ cd ..
+$ cd fptlqh
+$ ls
+dir bbsmm
+dir bdvp
+162052 vnnjfh.stw
+dir zshvq
+$ cd bbsmm
+$ ls
+dir bbsmm
+dir pchdq
+dir rwhffw
+$ cd bbsmm
+$ ls
+174590 bbsmm
+45636 fpj
+102831 qld
+dir vpwcmgq
+229136 zdvj.rtw
+dir znw
+$ cd vpwcmgq
+$ ls
+dir cfvhc
+dir jvd
+dir zdvj
+$ cd cfvhc
+$ ls
+55907 ccvdlc
+272581 fpj
+29765 phthcq
+$ cd ..
+$ cd jvd
+$ ls
+dir cfvhc
+$ cd cfvhc
+$ ls
+289471 cjzwdvs.gtn
+$ cd ..
+$ cd ..
+$ cd zdvj
+$ ls
+98507 cmldvpnc.qtl
+$ cd ..
+$ cd ..
+$ cd znw
+$ ls
+dir zdvj
+$ cd zdvj
+$ ls
+46012 wrgvln.lmf
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd pchdq
+$ ls
+dir cbssm
+98102 cfvhc
+259488 pct.bbd
+dir rwhffw
+7541 sngz.qsd
+167971 wvgrzn.bds
+$ cd cbssm
+$ ls
+45025 bbsmm
+22500 fpj
+$ cd ..
+$ cd rwhffw
+$ ls
+dir fncszbm
+166012 phthcq
+165925 rbs
+171350 tsbv.zws
+$ cd fncszbm
+$ ls
+dir wzrlzvm
+$ cd wzrlzvm
+$ ls
+191811 qzzs
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rwhffw
+$ ls
+218844 dnbmcbr.wrc
+201091 mmpvsgjv.wgs
+dir mzt
+223628 pct.bbd
+dir qftlpdt
+56441 sngz.qsd
+33730 zdvj.jfs
+$ cd mzt
+$ ls
+25768 sngz.qsd
+283843 vjrmbw.jcs
+$ cd ..
+$ cd qftlpdt
+$ ls
+287536 mzhmjrg
+223797 pcp
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd bdvp
+$ ls
+dir lnvvwlm
+$ cd lnvvwlm
+$ ls
+210345 zll.scl
+$ cd ..
+$ cd ..
+$ cd zshvq
+$ ls
+251997 cfvhc.mcr
+220501 pct.bbd
+$ cd ..
+$ cd ..
+$ cd hvwdzbsw
+$ ls
+dir cfvhc
+21816 fpj
+251732 vcfcwjh.pjh
+187137 zdvj.bzd
+dir zgprrg
+$ cd cfvhc
+$ ls
+99722 zgq.bbh
+$ cd ..
+$ cd zgprrg
+$ ls
+dir brzpsnjl
+$ cd brzpsnjl
+$ ls
+dir qld
+$ cd qld
+$ ls
+30055 phqdjs.zwv
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd nlm
+$ ls
+176700 fzwrr.ndp
+256184 pct.bbd
+25442 qhngw.jwm
+dir rwhffw
+160900 wqr.wdn
+$ cd rwhffw
+$ ls
+240085 pcjjgjcr
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ngm
+$ ls
+118461 bbsmm
+dir czjzwl
+dir gjwws
+43114 nwvltczv.jpf
+dir qjpgzsj
+145909 rjdzrjzc.fdr
+dir tvmpgrq
+75554 zdmfhpwz.rhc
+dir zdvj
+196704 zdvj.nfm
+$ cd czjzwl
+$ ls
+dir btnsjtl
+dir cfvhc
+100911 cfvhc.mqg
+dir gljbgjq
+247017 jmrq
+dir qld
+$ cd btnsjtl
+$ ls
+76658 pmgnqr
+$ cd ..
+$ cd cfvhc
+$ ls
+189214 jvrcttm
+265333 mmgd.sbn
+$ cd ..
+$ cd gljbgjq
+$ ls
+dir ccqsnp
+dir fng
+104525 fzwrr.ndp
+31517 gmqhmwj.rbs
+122232 htddmr.wjm
+51362 rtlnr.mwh
+42769 rvddl
+$ cd ccqsnp
+$ ls
+192021 cfvhc.lww
+dir mbmsfgtb
+222388 nvgppmv
+dir qbpnbsv
+$ cd mbmsfgtb
+$ ls
+104492 jfwl.jlv
+273739 phthcq
+238815 rwhffw.tjp
+$ cd ..
+$ cd qbpnbsv
+$ ls
+176577 tlqnmmsc.gpt
+$ cd ..
+$ cd ..
+$ cd fng
+$ ls
+243298 cfvhc.pvb
+54364 fbrtb.hqd
+dir hvhwh
+dir rtcsp
+$ cd hvhwh
+$ ls
+87079 zdvj.gfq
+$ cd ..
+$ cd rtcsp
+$ ls
+dir zdvj
+$ cd zdvj
+$ ls
+288617 rwhffw.lzl
+161000 sngz.qsd
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qld
+$ ls
+113942 bbsmm.msn
+239442 cchgv.mtd
+181920 nnlfh.gwz
+274766 rqtvc.frp
+$ cd ..
+$ cd ..
+$ cd gjwws
+$ ls
+dir bpzsc
+dir cfvhc
+dir fsnf
+dir zdvj
+$ cd bpzsc
+$ ls
+88033 cndwmvj.pzm
+275297 hslg.smz
+dir qld
+25067 qvntlcd.nzs
+$ cd qld
+$ ls
+240593 bbsmm.bgn
+dir dpqgcc
+277540 fpj
+dir qld
+$ cd dpqgcc
+$ ls
+dir rhqvcfd
+$ cd rhqvcfd
+$ ls
+199386 fzwrr.ndp
+$ cd ..
+$ cd ..
+$ cd qld
+$ ls
+269177 cjrc.dvq
+21631 fpj
+263874 pct.bbd
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd cfvhc
+$ ls
+dir nzd
+$ cd nzd
+$ ls
+273225 qld.jmn
+$ cd ..
+$ cd ..
+$ cd fsnf
+$ ls
+17887 fzwrr.ndp
+$ cd ..
+$ cd zdvj
+$ ls
+160076 rwhffw.trf
+$ cd ..
+$ cd ..
+$ cd qjpgzsj
+$ ls
+227586 njqbnz.hwl
+239840 zdvj.qcp
+$ cd ..
+$ cd tvmpgrq
+$ ls
+dir hdrsmnvc
+dir psrjr
+$ cd hdrsmnvc
+$ ls
+dir rjm
+dir zdvj
+$ cd rjm
+$ ls
+244666 bzvmdcm
+$ cd ..
+$ cd zdvj
+$ ls
+132943 gnz.ntd
+$ cd ..
+$ cd ..
+$ cd psrjr
+$ ls
+dir nfvllp
+dir plfgfs
+94173 qmdhr
+3133 swjh.rbg
+dir zmp
+$ cd nfvllp
+$ ls
+39707 zbsm.vsd
+$ cd ..
+$ cd plfgfs
+$ ls
+186603 cfvhc.sgb
+183833 ljn
+$ cd ..
+$ cd zmp
+$ ls
+123313 fzwrr.ndp
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd zdvj
+$ ls
+dir hzzs
+230054 nhcvn.dfl
+dir rgwfsqdc
+dir zwtjr
+$ cd hzzs
+$ ls
+58585 cbfhrf.vqn
+dir cfvhc
+dir fbzpmc
+dir fdtjp
+28182 fzwrr.ndp
+35581 nzwndl.zzs
+dir pmrv
+253508 qgdnnl.tqq
+$ cd cfvhc
+$ ls
+256186 nlgzqw.frl
+$ cd ..
+$ cd fbzpmc
+$ ls
+dir cfvhc
+$ cd cfvhc
+$ ls
+193322 rwhffw.vwc
+$ cd ..
+$ cd ..
+$ cd fdtjp
+$ ls
+dir bbsmm
+140927 fzwrr.ndp
+dir lcmpbs
+94413 mpnfgpsm
+dir qvrd
+247202 tlp
+$ cd bbsmm
+$ ls
+206896 clsqp.hbr
+$ cd ..
+$ cd lcmpbs
+$ ls
+dir bbsmm
+dir cfvhc
+dir rplqgm
+dir tmh
+dir zdvj
+$ cd bbsmm
+$ ls
+235479 sngz.qsd
+$ cd ..
+$ cd cfvhc
+$ ls
+285029 fpj
+29405 mhpr.czj
+$ cd ..
+$ cd rplqgm
+$ ls
+dir bbsmm
+dir cfvhc
+dir mwhf
+$ cd bbsmm
+$ ls
+257187 hsldvt.jdt
+186098 pct.bbd
+$ cd ..
+$ cd cfvhc
+$ ls
+dir rfw
+$ cd rfw
+$ ls
+254454 fzf.ltl
+$ cd ..
+$ cd ..
+$ cd mwhf
+$ ls
+3915 dtcjptnd
+116218 fzwrr.ndp
+$ cd ..
+$ cd ..
+$ cd tmh
+$ ls
+49016 ljsvgl.npd
+258257 mlfq.rrr
+$ cd ..
+$ cd zdvj
+$ ls
+dir jjj
+154441 lcpvwhp.mpq
+$ cd jjj
+$ ls
+31731 bbsmm.mhb
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qvrd
+$ ls
+199473 pgqbpq
+$ cd ..
+$ cd ..
+$ cd pmrv
+$ ls
+120831 cfvhc.btv
+$ cd ..
+$ cd ..
+$ cd rgwfsqdc
+$ ls
+281099 cfvhc.trc
+119483 mntqrvw.qwz
+275572 zhphps
+$ cd ..
+$ cd zwtjr
+$ ls
+187404 gwjsj
+230074 qmr
+212641 wdhl.dfl
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ngsrlzc
+$ ls
+164729 lnc.mdg
+33748 pct.bbd
+119803 rfst.blw
+dir zdvj
+$ cd zdvj
+$ ls
+dir bbsmm
+251550 sngz.qsd
+120940 zlmcvg
+$ cd bbsmm
+$ ls
+206509 bsddjdv
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd shjv
+$ ls
+17754 lqqz.qmf
+265175 rdmsqztj.lnt
+$ cd ..
+$ cd wsvfbb
+$ ls
+dir bhbb
+271856 fzwrr.ndp
+114188 hgfjq.jvn
+dir jgnl
+dir qnddp
+51700 rwdnzwhv.vtr
+$ cd bhbb
+$ ls
+107036 rwhffw.pld
+$ cd ..
+$ cd jgnl
+$ ls
+dir qld
+$ cd qld
+$ ls
+39294 rbsvwh
+$ cd ..
+$ cd ..
+$ cd qnddp
+$ ls
+dir vzmrb
+58723 wcbz.dwh
+$ cd vzmrb
+$ ls
+203361 rvrbgd.cpw
+167876 zgpdcb.rql
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rbmstsf
+$ ls
+dir bbsmm
+dir cfvhc
+63250 hcdlfmt.ccg
+dir mgzl
+dir ngflwbmp
+dir shvjprl
+123258 spqchmj.wpv
+$ cd bbsmm
+$ ls
+dir cfvhc
+dir gtvszgs
+6558 nzdhg
+121160 qld
+dir rfjfp
+dir rwhffw
+dir sbfshph
+$ cd cfvhc
+$ ls
+dir btmbjrw
+dir pzq
+133178 sngz.qsd
+$ cd btmbjrw
+$ ls
+dir ftpvz
+dir tjflfshs
+dir wvncjmf
+$ cd ftpvz
+$ ls
+dir lqlrh
+$ cd lqlrh
+$ ls
+114380 pcn.cbn
+$ cd ..
+$ cd ..
+$ cd tjflfshs
+$ ls
+274555 bbsmm.wpr
+$ cd ..
+$ cd wvncjmf
+$ ls
+dir nnrqcfmm
+$ cd nnrqcfmm
+$ ls
+217188 pct.bbd
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd pzq
+$ ls
+89797 sngz.qsd
+255100 thg
+$ cd ..
+$ cd ..
+$ cd gtvszgs
+$ ls
+87661 sngz.qsd
+$ cd ..
+$ cd rfjfp
+$ ls
+130164 cfvhc.rbt
+dir lhf
+dir qld
+20799 qld.gts
+dir qwzj
+150977 sngz.qsd
+$ cd lhf
+$ ls
+125848 lvgv.jtg
+81087 mdcw.rbv
+80091 qld
+dir rmsggp
+49093 thf.jnh
+dir tmf
+dir zdvj
+$ cd rmsggp
+$ ls
+dir fznsmbq
+dir lhghnn
+dir zdvj
+$ cd fznsmbq
+$ ls
+dir wqzwsspq
+dir zdvj
+$ cd wqzwsspq
+$ ls
+dir gbzfpfq
+$ cd gbzfpfq
+$ ls
+185410 pct.bbd
+$ cd ..
+$ cd ..
+$ cd zdvj
+$ ls
+140384 pct.bbd
+$ cd ..
+$ cd ..
+$ cd lhghnn
+$ ls
+36868 fpj
+181925 pct.bbd
+$ cd ..
+$ cd zdvj
+$ ls
+256916 bbsmm.mtr
+dir rwhffw
+75846 rwztfcj.qns
+$ cd rwhffw
+$ ls
+234834 bbsmm.gtp
+140315 pbwqrps
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd tmf
+$ ls
+88583 dhndg
+52796 qld.mlp
+$ cd ..
+$ cd zdvj
+$ ls
+8822 pct.bbd
+221658 sngz.qsd
+$ cd ..
+$ cd ..
+$ cd qld
+$ ls
+119026 dfc.bgr
+87684 fzwrr.ndp
+dir zdvj
+212120 zdvj.jhw
+$ cd zdvj
+$ ls
+dir cfvhc
+169434 mhlpvf.vjz
+$ cd cfvhc
+$ ls
+243919 brtvv
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qwzj
+$ ls
+101395 qcjvs.mjs
+$ cd ..
+$ cd ..
+$ cd rwhffw
+$ ls
+94012 fzwrr.ndp
+205816 hqrgfm.wwt
+dir lzbmzw
+155203 qjv
+$ cd lzbmzw
+$ ls
+dir dfrdczdj
+$ cd dfrdczdj
+$ ls
+126459 tphtshwc
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd sbfshph
+$ ls
+229389 ccbgcv
+135997 cfvhc.ctl
+119490 rwhffw.pqp
+94530 vjtjz.cfs
+$ cd ..
+$ cd ..
+$ cd cfvhc
+$ ls
+dir cmr
+dir jvh
+dir mjs
+$ cd cmr
+$ ls
+227026 fpj
+$ cd ..
+$ cd jvh
+$ ls
+dir qld
+$ cd qld
+$ ls
+96968 phthcq
+$ cd ..
+$ cd ..
+$ cd mjs
+$ ls
+98117 qld.ftd
+$ cd ..
+$ cd ..
+$ cd mgzl
+$ ls
+198793 nfvnfqn.zwd
+$ cd ..
+$ cd ngflwbmp
+$ ls
+dir bbsmm
+dir jthnmqs
+137176 nrn
+80344 ntbq.ssw
+133166 phthcq
+210691 sngz.qsd
+255501 vgwn.gqr
+dir zdvj
+$ cd bbsmm
+$ ls
+98501 npwwzjgt
+134593 sngz.qsd
+46020 tjszcpm.brs
+213591 zdvj.sgj
+$ cd ..
+$ cd jthnmqs
+$ ls
+dir bbsmm
+dir cfdbp
+dir dpwqmbw
+67233 lzz.tmg
+21344 pzvjlt.qdb
+dir rwhffw
+dir sdtntw
+268059 vwhqh
+dir zdvj
+$ cd bbsmm
+$ ls
+263767 fpj
+46528 tpq.wtb
+$ cd ..
+$ cd cfdbp
+$ ls
+dir grczc
+$ cd grczc
+$ ls
+241829 vnnjnz.nww
+$ cd ..
+$ cd ..
+$ cd dpwqmbw
+$ ls
+287899 pct.bbd
+$ cd ..
+$ cd rwhffw
+$ ls
+41741 ffsqr.cwc
+dir ffzc
+dir nrs
+279620 qchg
+$ cd ffzc
+$ ls
+10583 pct.bbd
+$ cd ..
+$ cd nrs
+$ ls
+195239 zdvj.npp
+$ cd ..
+$ cd ..
+$ cd sdtntw
+$ ls
+245601 fpj
+9552 lgftw.ntj
+$ cd ..
+$ cd zdvj
+$ ls
+dir smjvcql
+$ cd smjvcql
+$ ls
+dir qcqljj
+$ cd qcqljj
+$ ls
+286791 fhm
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd zdvj
+$ ls
+dir bcghd
+57663 fzwrr.ndp
+177473 gzpfmsz.tnw
+53543 pswpr
+251188 sngz.qsd
+dir snqjl
+$ cd bcghd
+$ ls
+106732 sjvhjd
+dir vtgsnb
+$ cd vtgsnb
+$ ls
+dir gtj
+$ cd gtj
+$ ls
+151689 nncsjvn.vhd
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd snqjl
+$ ls
+104993 bjjqd.pqv
+233367 fzwrr.ndp
+11942 sngz.qsd
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd shvjprl
+$ ls
+130539 jpzsdjw.hlz
+$ cd ..
+$ cd ..
+$ cd twjcmp
+$ ls
+86053 pct.bbd
A day7/src/main.rs => day7/src/main.rs +186 -0
@@ 0,0 1,186 @@
+use indextree::*;
+
+const UNKNOWN_SIZE:i32 = -1;
+const MAX_SIZE:i32 = 100000;
+
+
+#[derive(PartialEq)]
+enum FileType {
+ File,
+ Dir,
+}
+
+struct Node {
+ name: String,
+ size: i32,
+ ftype: FileType,
+}
+
+extern crate indextree;
+
+use std::fs::File;
+use std::io::{self, BufRead};
+use std::path::Path;
+
+// The output is wrapped in a Result to allow matching on errors
+// Returns an Iterator to the Reader of the lines of the file.
+fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
+where P: AsRef<Path>, {
+ let file = File::open(filename)?;
+ Ok(io::BufReader::new(file).lines())
+}
+
+fn mkdir(name: &str) -> Node {
+
+ return Node {
+ name: name.to_string(),
+ size: UNKNOWN_SIZE,
+ ftype: FileType::Dir,
+ };
+}
+
+
+fn mkfile (name: &str, num_str: &str) -> Node {
+ return Node {
+ name: name.to_string(),
+ size: int_str_to_int(num_str),
+ ftype: FileType::File,
+ };
+}
+
+fn main() {
+ let root = Node {
+ name: "/".to_string(),
+ size: UNKNOWN_SIZE,
+ ftype: FileType::Dir,
+ };
+
+ let arena = &mut Arena::new();
+
+ let mut last_node = arena.new_node(root);
+ let root_node = last_node;
+
+ if let Ok(lines) = read_lines("./input") {
+ // Consumes the iterator, returns an (Optional) String
+ for line in lines {
+ if let Ok(inst) = line {
+ let inst_split:Vec<&str> = inst.rsplit(' ').rev().collect();
+ if inst_split[0] == "$" {
+ // command
+ if inst_split[1] == "cd" {
+ // TODO: write handler for cd .., and
+ if inst_split[2] == "/" {
+ //cur_node = root.borrow();
+ last_node = root_node;
+ } else if inst_split[2] == ".." {
+ last_node = arena[last_node].parent().unwrap();
+ } else {
+ // println!("{}", last_node.children(arena).count());
+ //for child in last_node.children(arena) {
+ // println!("{}", child);
+ // println!("{}", arena[child].get().name);
+ //}
+ //
+ // cd DIR-NAME
+ for child in last_node.children(arena) {
+ if arena[child].get().name == inst_split[2] {
+ last_node = child;
+ }
+ }
+ }
+ }
+ } else {
+ // Output of ls
+ if inst_split[0] == "dir" {
+ let new_dir = arena.new_node(mkdir(&inst_split[1]));
+ last_node.append(new_dir, arena);
+ } else {
+ let new_file = arena.new_node(mkfile(&inst_split[1],&inst_split[0]));
+ last_node.append(new_file, arena);
+ }
+ }
+ }
+ }
+ }
+
+ let mut cwd = root_node;
+ let mut stack: Vec<NodeId> = Vec::new();
+ let mut pushed_parent;
+ stack.push(cwd);
+
+ while !stack.is_empty() {
+ cwd = stack.pop().unwrap();
+ pushed_parent = false;
+
+ let mut dir_tot:i32 = 0;
+ for child in cwd.children(arena) {
+ if arena[child].get().ftype == FileType::Dir &&
+ arena[child].get().size == UNKNOWN_SIZE {
+ stack.push(cwd);
+ stack.push(child);
+ pushed_parent = true;
+ } else if arena[child].get().size != UNKNOWN_SIZE {
+ dir_tot += arena[child].get().size;
+ }
+ }
+
+ if !pushed_parent {
+ arena[cwd].get_mut().size = dir_tot;
+ }
+ }
+
+ let mut cwd = root_node;
+ let mut stack: Vec<NodeId> = Vec::new();
+ stack.push(cwd);
+ let mut less_than_max:i32 = 0;
+
+ while !stack.is_empty() {
+ cwd = stack.pop().unwrap();
+ for child in cwd.children(arena) {
+ if arena[child].get().ftype == FileType::Dir {
+ stack.push(child);
+ if arena[child].get().size <= MAX_SIZE {
+ less_than_max += arena[child].get().size;
+ }
+ }
+ }
+ }
+ println!("Part 1: {}", less_than_max);
+
+
+ const TOTAL_DISK:i32 = 70000000;
+ const NEED_UNUSED:i32 = 30000000;
+
+ let unused_before:i32 = TOTAL_DISK - arena[root_node].get().size;
+ let del_atleast:i32 = NEED_UNUSED-unused_before;
+
+ let mut cwd = root_node;
+ let mut stack: Vec<NodeId> = Vec::new();
+ stack.push(cwd);
+ let mut min_so_far = i32::MAX;
+
+ while !stack.is_empty() {
+ cwd = stack.pop().unwrap();
+ for child in cwd.children(arena) {
+ if arena[child].get().ftype == FileType::Dir {
+ stack.push(child);
+ if arena[child].get().size >= del_atleast {
+ if arena[child].get().size <= min_so_far {
+ min_so_far = arena[child].get().size;
+ }
+ }
+ }
+ }
+ }
+ println!("Part 2: {}", min_so_far);
+
+}
+
+
+fn int_str_to_int(num_str:&str) -> i32 {
+
+ match num_str.parse::<i32>() {
+ Ok(n) => return n,
+ _ => panic!("failed to parse {}", num_str),
+ }
+}