From 7c73ed277e8ec8a54a62340825eba4adf20282cb Mon Sep 17 00:00:00 2001 From: Kevin Wilson Date: Thu, 8 Dec 2022 07:04:42 -0500 Subject: [PATCH] day 7 --- input/input07 | 1108 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 137 +++++- 2 files changed, 1244 insertions(+), 1 deletion(-) create mode 100644 input/input07 diff --git a/input/input07 b/input/input07 new file mode 100644 index 0000000..5eac444 --- /dev/null +++ b/input/input07 @@ -0,0 +1,1108 @@ +$ cd / +$ ls +dir fpljqj +171526 ghtzhjwf.nls +dir gsdsbld +dir hbmjtb +296801 mjfjqw.ccv +dir nfn +dir qmrsvfvw +102565 qqjnqb.chd +dir svgbqd +$ cd fpljqj +$ ls +153563 ghtzhjwf.nls +243252 gsvjgj.jsm +154134 hghnrbqg.rzb +$ cd .. +$ cd gsdsbld +$ ls +dir npmncvhh +dir qmrsvfvw +dir sqtnlr +dir vzndpc +$ cd npmncvhh +$ ls +81366 dwbgr.ztr +144577 fzjmcq +dir mphhrqf +dir rnmvggfd +276454 zfl.ghv +$ cd mphhrqf +$ ls +dir qlcfs +111207 shmcrf.wlr +dir zwsnwvnv +$ cd qlcfs +$ ls +283904 fpljqj.pdw +83520 hsclcqqt.pff +dir htwl +dir lqjhfdch +5842 mdjzmbc.qtv +dir nqfdhlcg +120167 twgqhvft.cgw +186998 zclhcr +dir zfl +dir zlqgr +$ cd htwl +$ ls +268134 hmwnn.htq +$ cd .. +$ cd lqjhfdch +$ ls +21479 tpdsgf.hgd +$ cd .. +$ cd nqfdhlcg +$ ls +dir dhjfqv +203675 ghtzhjwf.nls +39527 qfwdmzfv.ggd +$ cd dhjfqv +$ ls +135074 dqs.wht +$ cd .. +$ cd .. +$ cd zfl +$ ls +17334 wlsd +$ cd .. +$ cd zlqgr +$ ls +dir crs +dir whrm +dir zfl +$ cd crs +$ ls +220281 szft.bjb +$ cd .. +$ cd whrm +$ ls +279796 pcnl +dir pjzpqs +$ cd pjzpqs +$ ls +dir smz +192921 sqtst.lcz +21397 trst +154861 vtgdsnmv +$ cd smz +$ ls +249693 mjfjqw.ccv +dir tdwqpg +$ cd tdwqpg +$ ls +157907 wjv.rth +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd zfl +$ ls +dir hwlhshtr +$ cd hwlhshtr +$ ls +90468 srrv.jst +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd zwsnwvnv +$ ls +209371 ctmcqlz.nwh +154262 nqngjf +$ cd .. +$ cd .. +$ cd rnmvggfd +$ ls +dir czzrdvc +203540 dnwtwzd +dir fcsjqg +dir fwdprwpq +74582 qlnqwnzp.ttz +dir tvglfb +$ cd czzrdvc +$ ls +dir blplbjpw +$ cd blplbjpw +$ ls +2835 zfl.zvw +$ cd .. +$ cd .. +$ cd fcsjqg +$ ls +81548 ghtzhjwf.nls +$ cd .. +$ cd fwdprwpq +$ ls +11156 gsvjgj.jsm +75010 rgscvjq.zlw +122327 szft.bjb +dir zfl +268883 zpgcwvjf +$ cd zfl +$ ls +dir sphpbjt +$ cd sphpbjt +$ ls +31414 qfwdmzfv.ggd +$ cd .. +$ cd .. +$ cd .. +$ cd tvglfb +$ ls +118050 gsvjgj.jsm +265116 sdmldsd.hhm +$ cd .. +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +dir fpljqj +203965 szft.bjb +dir zcgfg +$ cd fpljqj +$ ls +dir gqvmv +$ cd gqvmv +$ ls +175614 mjfjqw.ccv +$ cd .. +$ cd .. +$ cd zcgfg +$ ls +110104 ddzcbm.qtb +dir fpglbth +148639 nqfdhlcg.fsz +dir pfjz +166120 wjrmgl +$ cd fpglbth +$ ls +58850 qlggch.tng +$ cd .. +$ cd pfjz +$ ls +197076 wplpj +$ cd .. +$ cd .. +$ cd .. +$ cd sqtnlr +$ ls +189086 gsvjgj.jsm +dir ngr +210016 pmprhlg.rsg +277979 szft.bjb +282202 tfqcnn.hlf +$ cd ngr +$ ls +dir fdhd +dir hwsqqt +dir qmrsvfvw +$ cd fdhd +$ ls +7384 fhhp +dir fpljqj +91732 nqfdhlcg.gwf +69137 ptcrmc.wwr +$ cd fpljqj +$ ls +282109 rtfhcbc.pqj +$ cd .. +$ cd .. +$ cd hwsqqt +$ ls +174820 zvmgv.tcd +$ cd .. +$ cd qmrsvfvw +$ ls +228739 qfwdmzfv.ggd +$ cd .. +$ cd .. +$ cd .. +$ cd vzndpc +$ ls +183360 fpljqj.nbh +$ cd .. +$ cd .. +$ cd hbmjtb +$ ls +219988 hlvjdg +dir lndmtm +107247 rtpvh.srl +dir sgt +dir tgjszvsg +166122 zrshs.phz +dir ztrvv +$ cd lndmtm +$ ls +dir fpljqj +dir hscwh +dir wjv +$ cd fpljqj +$ ls +102717 qmrsvfvw +$ cd .. +$ cd hscwh +$ ls +dir qgz +dir qmrsvfvw +$ cd qgz +$ ls +281901 szft.bjb +$ cd .. +$ cd qmrsvfvw +$ ls +102781 fpljqj.gtv +197014 gsvjgj.jsm +174895 pvz +$ cd .. +$ cd .. +$ cd wjv +$ ls +dir fpljqj +295195 mjfjqw.ccv +214886 qfwdmzfv.ggd +dir qmrsvfvw +72164 vncjvfhh +$ cd fpljqj +$ ls +dir fpljqj +dir fzbppql +dir jpqqr +dir lzq +dir pjjbmllm +dir qmrsvfvw +$ cd fpljqj +$ ls +260943 ghtzhjwf.nls +$ cd .. +$ cd fzbppql +$ ls +29399 wjv +$ cd .. +$ cd jpqqr +$ ls +45275 zcwbrvd +$ cd .. +$ cd lzq +$ ls +180833 szft.bjb +$ cd .. +$ cd pjjbmllm +$ ls +64063 mjfjqw.ccv +183683 vrfd.wlw +$ cd .. +$ cd qmrsvfvw +$ ls +226047 szft.bjb +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +dir bzcfzh +272794 lnzpvhj +dir vsrgqmlt +dir zbthlb +$ cd bzcfzh +$ ls +63095 mjfjqw.ccv +45335 nqfdhlcg +162307 vqqt.vbg +$ cd .. +$ cd vsrgqmlt +$ ls +dir nqfdhlcg +$ cd nqfdhlcg +$ ls +dir wjv +$ cd wjv +$ ls +81528 cslzgjtp.qzg +$ cd .. +$ cd .. +$ cd .. +$ cd zbthlb +$ ls +dir hjp +dir twfw +dir zfl +$ cd hjp +$ ls +20745 pvjwrzsl.pmg +$ cd .. +$ cd twfw +$ ls +dir csmmbjhp +$ cd csmmbjhp +$ ls +165998 nlsd +109132 vbjlnqt.lsd +$ cd .. +$ cd .. +$ cd zfl +$ ls +227633 jcs.vhj +dir tvfhvbp +$ cd tvfhvbp +$ ls +116169 lgv +175862 qfwdmzfv.ggd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd sgt +$ ls +151135 nqfdhlcg +226449 nqfdhlcg.sqp +148179 wjv.fgc +$ cd .. +$ cd tgjszvsg +$ ls +98354 szft.bjb +$ cd .. +$ cd ztrvv +$ ls +163992 blmmm.gcf +dir fghl +dir fpljqj +226325 nqfdhlcg +dir qmrsvfvw +284965 vhbffmcg.fwt +$ cd fghl +$ ls +dir lgpwpmzp +dir sqjqg +$ cd lgpwpmzp +$ ls +dir ctr +dir nqfdhlcg +279439 zfl.npd +$ cd ctr +$ ls +64805 jfflsd.gbc +163058 zbvpc.znm +$ cd .. +$ cd nqfdhlcg +$ ls +40180 jsqtwpt.qtq +87408 rmpbprz.lwr +$ cd .. +$ cd .. +$ cd sqjqg +$ ls +140444 bsglv +214121 crzdv.dcc +$ cd .. +$ cd .. +$ cd fpljqj +$ ls +56575 gqw.dzr +293957 gsvjgj.jsm +272507 jvd +dir tgfvcpp +178972 vndshbth.mzw +dir zwtz +$ cd tgfvcpp +$ ls +dir bhq +$ cd bhq +$ ls +111454 gvq +$ cd .. +$ cd .. +$ cd zwtz +$ ls +39290 nqfdhlcg +140517 qfwdmzfv.ggd +dir tcnv +177429 zlzsq.fph +$ cd tcnv +$ ls +286997 fpljqj.phd +$ cd .. +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +252862 jbznh +$ cd .. +$ cd .. +$ cd .. +$ cd nfn +$ ls +dir fqw +dir mpz +dir qmrsvfvw +dir zfl +$ cd fqw +$ ls +144372 ghtzhjwf.nls +100013 mqwwjbvz.scd +95547 vspwhq.dwn +$ cd .. +$ cd mpz +$ ls +dir gntjg +dir jfbhz +7835 nqfdhlcg +dir vpgpz +dir zfl +$ cd gntjg +$ ls +dir hdq +dir hvcdpzr +dir lth +27002 mjfjqw.ccv +dir qmrsvfvw +dir scncl +$ cd hdq +$ ls +dir jwhnt +$ cd jwhnt +$ ls +153428 fswqv.jpf +$ cd .. +$ cd .. +$ cd hvcdpzr +$ ls +dir fpljqj +135000 fpljqj.smw +275125 hrqwfjjz.rdj +dir pmcpnqrr +58960 qljhbczf.qfn +222912 szft.bjb +dir wvvzbt +$ cd fpljqj +$ ls +282896 nqfdhlcg.bjm +$ cd .. +$ cd pmcpnqrr +$ ls +dir nwjzld +dir tfdcg +dir vwlbtgnh +$ cd nwjzld +$ ls +74948 wjv.psf +$ cd .. +$ cd tfdcg +$ ls +77925 gsvjgj.jsm +dir lcdfdmlj +dir pthwnf +227063 qfwdmzfv.ggd +293860 qmwr.csp +154426 scwd.mdc +$ cd lcdfdmlj +$ ls +52503 mjfjqw.ccv +$ cd .. +$ cd pthwnf +$ ls +176935 fgcwjjz +$ cd .. +$ cd .. +$ cd vwlbtgnh +$ ls +dir nqfdhlcg +$ cd nqfdhlcg +$ ls +dir lnwtl +$ cd lnwtl +$ ls +252540 cgj.pdg +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wvvzbt +$ ls +25603 bvhd +24426 qzwgj.bmb +$ cd .. +$ cd .. +$ cd lth +$ ls +dir dmbf +60608 ghtzhjwf.nls +dir ndbcrvw +dir shsgqzn +27467 zfl.tdf +$ cd dmbf +$ ls +dir zpggmccr +$ cd zpggmccr +$ ls +238504 hqsgz.jfh +$ cd .. +$ cd .. +$ cd ndbcrvw +$ ls +289441 bwbdc +dir zfl +84967 zhwz +$ cd zfl +$ ls +95245 mjfjqw.ccv +$ cd .. +$ cd .. +$ cd shsgqzn +$ ls +184543 gqrthw.gwf +61456 wzbbsqrp +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +dir bql +247487 szvbjdjl +58312 wjv +241150 wjv.ltm +$ cd bql +$ ls +93199 fpljqj +$ cd .. +$ cd .. +$ cd scncl +$ ls +13092 clgmqlfl +dir dcldv +dir fsrznscl +21910 nqfdhlcg.lld +dir prcgb +$ cd dcldv +$ ls +271970 nqfdhlcg.dgv +$ cd .. +$ cd fsrznscl +$ ls +dir bcdrv +$ cd bcdrv +$ ls +96252 fpljqj.cdr +154325 tvf.vhv +$ cd .. +$ cd .. +$ cd prcgb +$ ls +69766 lnsvgqq.psj +$ cd .. +$ cd .. +$ cd .. +$ cd jfbhz +$ ls +286498 ssbmgts +$ cd .. +$ cd vpgpz +$ ls +63751 gsvjgj.jsm +220526 vvlvcs.dpc +$ cd .. +$ cd zfl +$ ls +dir fpljqj +182996 ghtzhjwf.nls +dir jcffb +dir jzl +dir nzlv +6752 snwmlr.glp +$ cd fpljqj +$ ls +dir btsdth +dir fwdw +dir nqfdhlcg +dir qmrsvfvw +203470 svsvcgj +$ cd btsdth +$ ls +176953 szft.bjb +$ cd .. +$ cd fwdw +$ ls +95939 crffczjt.gsq +dir mbgzf +dir rqdnjfdq +296397 zfl.fjb +$ cd mbgzf +$ ls +179220 mjfjqw.ccv +$ cd .. +$ cd rqdnjfdq +$ ls +204152 qmrsvfvw +$ cd .. +$ cd .. +$ cd nqfdhlcg +$ ls +194439 gwc.wdp +167934 qfwdmzfv.ggd +151571 sczw +$ cd .. +$ cd qmrsvfvw +$ ls +dir dwc +103919 gfzgg +6816 shpch.chl +$ cd dwc +$ ls +17813 gsvjgj.jsm +80522 hbhlv.pqh +dir htpt +dir hwg +dir nslrrrfg +dir psgw +231148 sdfvzdwm.wlz +102460 szft.bjb +dir wjlfgt +$ cd htpt +$ ls +162107 wjv +$ cd .. +$ cd hwg +$ ls +dir gslvrbt +dir qmrsvfvw +$ cd gslvrbt +$ ls +dir fjwn +107757 gsvjgj.jsm +$ cd fjwn +$ ls +268653 qptczjlq.prv +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +10557 nqfdhlcg +$ cd .. +$ cd .. +$ cd nslrrrfg +$ ls +112961 gsvjgj.jsm +dir hjgbgq +dir jctqdpq +dir qgfb +dir zfl +$ cd hjgbgq +$ ls +124947 mjfjqw.ccv +$ cd .. +$ cd jctqdpq +$ ls +203489 vgfhrl +$ cd .. +$ cd qgfb +$ ls +33980 nqfdhlcg.pqs +$ cd .. +$ cd zfl +$ ls +193098 gsvjgj.jsm +dir vmzghf +26070 zfnppjsz +$ cd vmzghf +$ ls +235035 szft.bjb +$ cd .. +$ cd .. +$ cd .. +$ cd psgw +$ ls +55808 gsvjgj.jsm +214300 wjv +$ cd .. +$ cd wjlfgt +$ ls +201399 qfwdmzfv.ggd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jcffb +$ ls +81815 szft.bjb +$ cd .. +$ cd jzl +$ ls +155651 gsvjgj.jsm +dir shrf +$ cd shrf +$ ls +57545 fpljqj.pcb +$ cd .. +$ cd .. +$ cd nzlv +$ ls +115992 bmmnj.ghw +100862 ghtzhjwf.nls +dir qwjpjw +dir rhbtbjp +dir rlmzs +60695 szft.bjb +3260 vzgwmrnc +dir wwnnj +17546 zdstndwj.lms +$ cd qwjpjw +$ ls +dir dpwp +dir fzdcjr +173669 gsvjgj.jsm +dir nmnbv +dir nqfdhlcg +dir plzdzdnm +135543 qfwdmzfv.ggd +246217 qmrsvfvw +dir sgpcqqm +141900 wvt.rfz +$ cd dpwp +$ ls +214058 wjv.zqs +94614 zrtbln +$ cd .. +$ cd fzdcjr +$ ls +236058 jcn.fzn +$ cd .. +$ cd nmnbv +$ ls +215145 lllgsbb +$ cd .. +$ cd nqfdhlcg +$ ls +61644 ghtzhjwf.nls +238094 qfwdmzfv.ggd +183057 szft.bjb +17501 wjv.pln +$ cd .. +$ cd plzdzdnm +$ ls +243979 fpljqj.lnj +$ cd .. +$ cd sgpcqqm +$ ls +262759 hjffwcls +100893 mvs.cgz +dir nzhlcl +170443 szft.bjb +dir zfl +$ cd nzhlcl +$ ls +dir wcfl +$ cd wcfl +$ ls +24502 mjfjqw.ccv +145029 zfl.lpp +$ cd .. +$ cd .. +$ cd zfl +$ ls +176653 ghtzhjwf.nls +$ cd .. +$ cd .. +$ cd .. +$ cd rhbtbjp +$ ls +dir nqfdhlcg +232788 pmj.cmm +dir zfvbc +$ cd nqfdhlcg +$ ls +72379 gqpcrtpw.nsm +$ cd .. +$ cd zfvbc +$ ls +19204 dqbs.ddg +$ cd .. +$ cd .. +$ cd rlmzs +$ ls +232862 fpljqj.rps +5558 lmgss.dtf +$ cd .. +$ cd wwnnj +$ ls +260417 cwjsrptm.hlm +216130 gsvjgj.jsm +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +dir fpljqj +dir hllbt +dir nqfdhlcg +$ cd fpljqj +$ ls +dir qmrsvfvw +dir tdnp +dir vhtwd +dir wnlzd +dir ztttgd +$ cd qmrsvfvw +$ ls +218325 fpljqj.zhj +220402 qfwdmzfv.ggd +$ cd .. +$ cd tdnp +$ ls +46936 fmgz +dir gdsqdtw +dir tpcbjc +dir zfl +$ cd gdsqdtw +$ ls +128812 cnfpsb.qjr +185390 ghtzhjwf.nls +147220 qfwdmzfv.ggd +$ cd .. +$ cd tpcbjc +$ ls +188075 szft.bjb +243171 zdprcqs.qwf +$ cd .. +$ cd zfl +$ ls +dir nnschfl +dir qmrsvfvw +dir szlbls +109443 wjv +$ cd nnschfl +$ ls +139948 szft.bjb +$ cd .. +$ cd qmrsvfvw +$ ls +dir bdn +dir fpljqj +168508 fpljqj.ljd +dir hwgbwqmm +dir nwhl +224059 qfwdmzfv.ggd +$ cd bdn +$ ls +179118 jdrdjf.ppn +74443 lpp +$ cd .. +$ cd fpljqj +$ ls +125033 mtfgm.pjf +$ cd .. +$ cd hwgbwqmm +$ ls +133673 vrvhgbz.ttb +$ cd .. +$ cd nwhl +$ ls +187017 fpljqj +149238 mjfjqw.ccv +dir mqzrmjr +dir vdjgqfc +dir zfzfbq +$ cd mqzrmjr +$ ls +dir fpljqj +131712 jmnsst.bmv +289722 ppdhjswn +dir qmrsvfvw +30641 zfl.trb +$ cd fpljqj +$ ls +286985 lwmfmsr.tln +253325 mjfjqw.ccv +194077 vgb.glm +38905 wjv.vgs +$ cd .. +$ cd qmrsvfvw +$ ls +87468 nngbnwds.qcn +$ cd .. +$ cd .. +$ cd vdjgqfc +$ ls +122025 rnvwf.mrp +261944 wdwgml +$ cd .. +$ cd zfzfbq +$ ls +dir bwmrf +dir zfl +$ cd bwmrf +$ ls +222502 gsvjgj.jsm +$ cd .. +$ cd zfl +$ ls +10297 ghtzhjwf.nls +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd szlbls +$ ls +10968 bqbclc.nfl +$ cd .. +$ cd .. +$ cd .. +$ cd vhtwd +$ ls +188024 dgz +$ cd .. +$ cd wnlzd +$ ls +201028 gsvjgj.jsm +dir mcnnsv +dir mctpdbs +47879 szft.bjb +dir tjqfts +164406 zpdmdrw +$ cd mcnnsv +$ ls +dir lclgj +dir sjdbnbqw +dir tlj +dir wjv +$ cd lclgj +$ ls +192605 fgpjczr.grp +65758 hhrf.fgg +$ cd .. +$ cd sjdbnbqw +$ ls +89058 ghtzhjwf.nls +191742 rlmwjg.dpl +179479 zswc.snt +$ cd .. +$ cd tlj +$ ls +183447 fpljqj.fgf +$ cd .. +$ cd wjv +$ ls +1517 pbwr +189647 szft.bjb +$ cd .. +$ cd .. +$ cd mctpdbs +$ ls +120327 szft.bjb +$ cd .. +$ cd tjqfts +$ ls +dir crvw +$ cd crvw +$ ls +289523 lcshtlgf.lrv +169176 szft.bjb +$ cd .. +$ cd .. +$ cd .. +$ cd ztttgd +$ ls +247914 sqqv.cvm +$ cd .. +$ cd .. +$ cd hllbt +$ ls +298155 cqnb.fgc +224277 hjf +220312 jhnpv +7421 qmrsvfvw +dir qsg +$ cd qsg +$ ls +dir drm +dir fpljqj +dir wjv +$ cd drm +$ ls +dir fhzr +$ cd fhzr +$ ls +dir nqfdhlcg +$ cd nqfdhlcg +$ ls +125578 nqfdhlcg +$ cd .. +$ cd .. +$ cd .. +$ cd fpljqj +$ ls +dir jmfqmdcm +dir nqfdhlcg +48942 qfwdmzfv.ggd +dir swrdzl +18483 szft.bjb +254012 zjcnz.pls +$ cd jmfqmdcm +$ ls +130015 bvrmp.vwg +157978 gsvjgj.jsm +54571 hmhldqr.ctt +169263 qgccqrqs +261388 szft.bjb +$ cd .. +$ cd nqfdhlcg +$ ls +213466 fpljqj.wbp +31434 jhsb.lbb +144357 qfwdmzfv.ggd +$ cd .. +$ cd swrdzl +$ ls +dir fgmtnt +dir pnmz +280186 qmrsvfvw.mrb +$ cd fgmtnt +$ ls +95823 gsvjgj.jsm +127258 qmrsvfvw +$ cd .. +$ cd pnmz +$ ls +110479 ghtzhjwf.nls +$ cd .. +$ cd .. +$ cd .. +$ cd wjv +$ ls +51754 jpwhctfd +174007 mgqplvv.hlt +45041 mtrfs.bhj +153169 vhjw.vbg +$ cd .. +$ cd .. +$ cd .. +$ cd nqfdhlcg +$ ls +dir htnw +280499 tdwzsgqh.zsh +$ cd htnw +$ ls +203521 ggfpmb.pmz +$ cd .. +$ cd .. +$ cd .. +$ cd zfl +$ ls +36860 mlbcw +dir rgvgqqd +239962 rpv.qhp +64500 zfl.mvw +$ cd rgvgqqd +$ ls +26778 qnhpfr +$ cd .. +$ cd .. +$ cd .. +$ cd qmrsvfvw +$ ls +290013 gsvjgj.jsm +$ cd .. +$ cd svgbqd +$ ls +69927 bjc.vdh diff --git a/src/main.rs b/src/main.rs index 02c0df5..a6a79ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{HashSet, HashMap}; use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; @@ -263,6 +263,140 @@ fn day06() { } } +enum ReadState { + COMMAND = 0, + LS = 1, +} + +struct Node { + name: String, + + parent_idx: usize, + idx: usize, + child_idxs: Vec, + + size: u32, + + is_dir: bool, + is_file: bool, +} + +fn get_node_size(cache: &mut HashMap, node_idx: usize, nodes: &Vec) -> u32 { + if nodes[node_idx].is_file { + return nodes[node_idx].size; + } + match cache.get(&node_idx) { + Some(size) => { *size }, + None => { + let size: u32 = nodes[node_idx].child_idxs.iter().map(|x| get_node_size(cache, *x, nodes)).sum(); + cache.insert(node_idx, size); + size + } + } +} + +fn day07() { + let mut read_state = ReadState::COMMAND; + + let root = Node { + name: "/".to_string(), + parent_idx: 0, + idx: 0, + child_idxs: vec![], + size: 0, + is_dir: true, + is_file: false, + }; + let mut nodes: Vec = vec![root]; + let mut current_node_idx: usize = 0; + + if let Ok(lines) = read_lines("./input/input07") { + for line in lines { + if let Ok(ip) = line { + let tip = ip.trim_end(); + let mut stip = tip.split(" ").collect_vec(); + + if stip[0] == "$" { + read_state = ReadState::COMMAND; + } + + match read_state { + ReadState::COMMAND => { + match stip[1] { + "cd" => { + match stip[2] { + "/" => { + current_node_idx = 0; + }, + ".." => { + current_node_idx = nodes[current_node_idx].parent_idx; + }, + file_name => { + let idx = nodes.len(); + let this_node = Node { + name: file_name.to_string(), + parent_idx: current_node_idx, + idx, + child_idxs: vec![], + size: 0, + is_dir: true, + is_file: false, + }; + nodes[current_node_idx].child_idxs.push(idx); + current_node_idx = idx; + nodes.push(this_node); + } + } + }, + "ls" => { + read_state = ReadState::LS; + }, + _ => panic!("This shouldn't happen"), + } + }, + ReadState::LS => { + if stip[0] == "dir" { + // We don't actually care about dirs + } else { + let file_size: u32 = stip[0].parse().unwrap(); + let file_name: String = stip[0].parse().unwrap(); + let idx = nodes.len(); + let this_node = Node { + name: file_name, + parent_idx: current_node_idx, + idx, + child_idxs: vec![], + size: file_size, + is_dir: false, + is_file: true, + }; + nodes[current_node_idx].child_idxs.push(idx); + nodes.push(this_node); + } + }, + } + } + } + } + let mut cache = HashMap::new(); + let dir_sizes: Vec = nodes.iter() + .filter(|node| node.is_dir) + .map(|node| get_node_size(&mut cache, node.idx, &nodes)) + .collect(); + + let ans1: u32 = dir_sizes.iter().filter(|x| **x <= 100000).sum(); + println!("The answer to part 1 is: {}", ans1); + + let total_space: u32 = 70000000; + let need_space: u32 = 30000000; + let used_space = get_node_size(&mut cache, 0, &nodes); + let avail_space = total_space - used_space; + let find_space: u32 = need_space - avail_space; + + let ans2 = dir_sizes.iter().filter(|size| **size >= find_space).min().unwrap(); + println!("The answer to part 2 is: {}", ans2); +} + fn main() { day01(); day02(); @@ -270,4 +404,5 @@ fn main() { day04(); day05(); day06(); + day07(); }