From: Greg Burri Date: Wed, 7 Dec 2022 22:38:59 +0000 (+0100) Subject: Day 7 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=e6e2d50f20b767e0213726935333ba9fa536573b;p=advent_of_code_2022.git Day 7 --- diff --git a/Cargo.toml b/Cargo.toml index 0d0f199..3e0b2fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,7 @@ edition = "2021" [dependencies] itertools = "0.10" -threadpool = "1.8" regex = "1" -num = "0.4" -num_enum = "0.5" [profile.release] opt-level = 3 diff --git a/data/day07.input b/data/day07.input new file mode 100644 index 0000000..6862721 --- /dev/null +++ b/data/day07.input @@ -0,0 +1,1032 @@ +$ cd / +$ ls +dir bfqzjjct +dir cgcqpjpn +dir frrjh +dir jjl +293559 jztrccm.hvd +dir mqvn +$ cd bfqzjjct +$ ls +dir cgcqpjpn +dir frrjh +dir gzpbchq +12679 lzrgqrgc +dir mzm +dir phslrcw +240839 phslrcw.ljl +dir qftq +dir qlpg +169962 vntqgq.tps +114950 vzq.qvv +$ cd cgcqpjpn +$ ls +305598 hjrv +134990 jlqcf.sjc +dir phslrcw +dir qsdlvr +129733 rznmd.snf +dir sgcg +dir swfsblvv +dir vprjf +dir wchtzbdv +dir wfvj +229649 zdm.rpl +$ cd phslrcw +$ ls +dir jnzjq +dir lfd +79014 lzrgqrgc.sqh +209222 wfnmqrtr.zbq +$ cd jnzjq +$ ls +163133 qsdcrvz.zmn +287008 zdm.rpl +$ cd .. +$ cd lfd +$ ls +4442 cgcqpjpn +61281 jmr +212696 jztrccm.hvd +dir mlt +dir qsdlvr +$ cd mlt +$ ls +183050 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +dir nzjfwdt +dir qsdlvr +$ cd nzjfwdt +$ ls +49044 jdqz.bqd +$ cd .. +$ cd qsdlvr +$ ls +13838 fpfmhq.rbp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +167042 jdqz.bqd +132914 lzrgqrgc +$ cd .. +$ cd sgcg +$ ls +dir bjwzfr +111567 jdqz.bqd +33253 jztrccm.hvd +75760 qsdlvr +$ cd bjwzfr +$ ls +268763 npjd.glz +dir phslrcw +121486 vbfsrwf.mbc +dir vtwtmnvl +$ cd phslrcw +$ ls +114641 cgsrlq +235130 npjd.glz +$ cd .. +$ cd vtwtmnvl +$ ls +7771 hnrpgsn +290862 rtrljz.vbf +220102 rztn.vqq +$ cd .. +$ cd .. +$ cd .. +$ cd swfsblvv +$ ls +231282 vclmcf.vpq +16727 vjhmb.ftn +$ cd .. +$ cd vprjf +$ ls +dir fppsf +56173 npjd.glz +dir phslrcw +dir pwjdcdff +dir qsdlvr +dir qvh +dir sbmtlz +$ cd fppsf +$ ls +dir hltnf +dir qrmggjs +dir qsdlvr +$ cd hltnf +$ ls +201448 npjd.glz +23616 zdm.rpl +$ cd .. +$ cd qrmggjs +$ ls +dir blhwqwd +dir szf +$ cd blhwqwd +$ ls +142887 vzq.qvv +$ cd .. +$ cd szf +$ ls +225491 tvsf.tpv +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +92209 brmrwznr +dir frrjh +212979 qsdlvr.bwh +dir vpvnb +$ cd frrjh +$ ls +143811 qvwmpj.gnc +$ cd .. +$ cd vpvnb +$ ls +dir wwsw +$ cd wwsw +$ ls +13671 qsdlvr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +dir bpfhc +dir fjvgwbj +127950 hjngrw.rtr +37045 lfpjb.ftz +dir qsn +$ cd bpfhc +$ ls +232351 frrjh.szf +14824 jztrccm.hvd +162270 vzq.qvv +$ cd .. +$ cd fjvgwbj +$ ls +dir cgcqpjpn +4290 jrfsmghw.vqr +dir lqnpdd +$ cd cgcqpjpn +$ ls +208104 btrcq.vpn +149676 bwvp.jlf +76056 gvvrvqdf.zdc +272686 pqhtjpv +dir zjntbw +$ cd zjntbw +$ ls +140092 jdqz.bqd +$ cd .. +$ cd .. +$ cd lqnpdd +$ ls +dir brf +197899 jdqz.bqd +dir phslrcw +236823 qcvgnlv.ngn +dir szrfcwtw +88974 vzq.qvv +$ cd brf +$ ls +112059 jdqz.bqd +217412 vzq.qvv +$ cd .. +$ cd phslrcw +$ ls +84285 jnzf +282660 zdm.rpl +$ cd .. +$ cd szrfcwtw +$ ls +84415 gcp +163787 wffb.wbq +$ cd .. +$ cd .. +$ cd .. +$ cd qsn +$ ls +139676 zdm.rpl +$ cd .. +$ cd .. +$ cd pwjdcdff +$ ls +dir cgcqpjpn +54163 lzrgqrgc +dir qsdlvr +51246 qsdlvr.cmq +5082 rvmv.tqj +112712 vzq.qvv +$ cd cgcqpjpn +$ ls +92895 dmf.bsv +dir lhqzc +$ cd lhqzc +$ ls +13443 hwlsmmjn.wpj +232141 tlsn +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +216144 dghbz.lnl +241311 jztrccm.hvd +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +109764 qsdlvr.tbj +$ cd .. +$ cd qvh +$ ls +dir frrjh +51428 hdtmm.zzl +$ cd frrjh +$ ls +271661 brff.mgp +$ cd .. +$ cd .. +$ cd sbmtlz +$ ls +277255 jztrccm.hvd +$ cd .. +$ cd .. +$ cd wchtzbdv +$ ls +198310 cgcqpjpn +300478 fcfvgpdr.lzv +145860 jdqz.bqd +20145 mggg +$ cd .. +$ cd wfvj +$ ls +270522 phslrcw.mwt +213919 tczvr +19226 zdm.rpl +$ cd .. +$ cd .. +$ cd frrjh +$ ls +dir cgcqpjpn +246391 hnc.bzl +dir lzrgqrgc +dir mnrbzwr +239337 npjd.glz +dir qsdlvr +$ cd cgcqpjpn +$ ls +2423 pflhrgjg.gch +$ cd .. +$ cd lzrgqrgc +$ ls +265708 vzq.qvv +107406 zwnjb +$ cd .. +$ cd mnrbzwr +$ ls +287289 jgs.bdb +$ cd .. +$ cd qsdlvr +$ ls +dir qsdlvr +$ cd qsdlvr +$ ls +71204 gnfz +$ cd .. +$ cd .. +$ cd .. +$ cd gzpbchq +$ ls +dir cmqgph +dir fwf +146975 jztrccm.hvd +284515 lzrgqrgc.qsf +27455 qbsbvscc +$ cd cmqgph +$ ls +177740 zdm.rpl +$ cd .. +$ cd fwf +$ ls +210658 dnmvdgwn.wzt +22013 rqcspw.qmf +244013 zdm.rpl +$ cd .. +$ cd .. +$ cd mzm +$ ls +262805 gnfsbdcd.tvp +$ cd .. +$ cd phslrcw +$ ls +74949 wncqjc.tzd +$ cd .. +$ cd qftq +$ ls +dir jrds +$ cd jrds +$ ls +dir dvht +dir qsdlvr +$ cd dvht +$ ls +57354 phslrcw +57174 qsdlvr.ftm +$ cd .. +$ cd qsdlvr +$ ls +132030 fbsmfspp +$ cd .. +$ cd .. +$ cd .. +$ cd qlpg +$ ls +dir cgcqpjpn +dir frrjh +dir jjsfmg +186178 jmvcvw +dir qgjhlnmf +100234 qltp.vdr +dir tbwt +dir vjjvfbdw +dir wjz +162428 zlbnsg +$ cd cgcqpjpn +$ ls +dir gvmgcftp +dir hsgvgc +dir nps +dir phslrcw +dir scdglb +$ cd gvmgcftp +$ ls +31869 dsddwbbp +dir fbsrjgzj +53280 tzn.fvp +$ cd fbsrjgzj +$ ls +89267 qsdlvr.lhs +$ cd .. +$ cd .. +$ cd hsgvgc +$ ls +dir lzrgqrgc +210774 wpspf.vrd +$ cd lzrgqrgc +$ ls +289086 gfhwwslq.tlz +$ cd .. +$ cd .. +$ cd nps +$ ls +74713 fnqzcr +dir srjvfq +94736 wbrcn.nng +dir wmddln +dir zsgnrmcc +$ cd srjvfq +$ ls +dir grqft +dir pjnjlf +254618 wmfnmfrc.wng +$ cd grqft +$ ls +3742 zmpzwh.nvz +$ cd .. +$ cd pjnjlf +$ ls +dir mlrsrs +$ cd mlrsrs +$ ls +167149 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd wmddln +$ ls +222831 cgcqpjpn.dcv +140188 gsvngqp +174466 sfngrqgj.gvz +$ cd .. +$ cd zsgnrmcc +$ ls +143043 vzq.qvv +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +dir cdfsrvnc +103523 hzsjc +239089 jvwdjbq +229835 npjd.glz +189778 wwcccwmb.bdp +$ cd cdfsrvnc +$ ls +dir vzsvcnlz +$ cd vzsvcnlz +$ ls +dir wwn +$ cd wwn +$ ls +60279 frngbwm.wwf +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd scdglb +$ ls +176557 vzq.qvv +$ cd .. +$ cd .. +$ cd frrjh +$ ls +130811 jdqz.bqd +$ cd .. +$ cd jjsfmg +$ ls +dir nffq +22842 vzt.nwh +$ cd nffq +$ ls +dir wmq +$ cd wmq +$ ls +196322 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd qgjhlnmf +$ ls +dir jhqqjt +dir phslrcw +246296 pzvmcq +219308 tlhqgtcl.twb +dir vfgb +300258 zdm.rpl +221663 zsqh.dph +$ cd jhqqjt +$ ls +90068 fctpjdlm.ggg +dir frrjh +dir ngmzbqb +dir phslrcw +64341 qsdlvr.pnq +dir vlgpcg +$ cd frrjh +$ ls +49247 qrrhdhld.sjr +$ cd .. +$ cd ngmzbqb +$ ls +247800 cgcqpjpn.wgv +$ cd .. +$ cd phslrcw +$ ls +dir qsdlvr +dir vjrrrm +$ cd qsdlvr +$ ls +255302 ddvc +182817 dzd.trp +$ cd .. +$ cd vjrrrm +$ ls +38983 hjbsqd.pdl +$ cd .. +$ cd .. +$ cd vlgpcg +$ ls +dir frrjh +83387 jztrccm.hvd +$ cd frrjh +$ ls +dir rmlc +$ cd rmlc +$ ls +273584 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +96286 jztrccm.hvd +224425 npjd.glz +$ cd .. +$ cd vfgb +$ ls +dir fgl +dir fllsgwjt +dir frrjh +dir qsdlvr +$ cd fgl +$ ls +61972 lzrgqrgc.qrs +$ cd .. +$ cd fllsgwjt +$ ls +dir fqchdcpd +214477 zcr.spz +$ cd fqchdcpd +$ ls +156000 jccqlw.twl +$ cd .. +$ cd .. +$ cd frrjh +$ ls +210987 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +dir phslrcw +dir znnsfvz +$ cd phslrcw +$ ls +214655 ddw +$ cd .. +$ cd znnsfvz +$ ls +126028 zpq.bgs +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tbwt +$ ls +dir ltjjhr +$ cd ltjjhr +$ ls +158069 vplwrz.rzp +$ cd .. +$ cd .. +$ cd vjjvfbdw +$ ls +277114 fmndb.rth +241524 frrjh +212152 frrjh.dlq +61370 jztrccm.hvd +dir srvqm +237264 znt +$ cd srvqm +$ ls +dir fbzmddc +61958 jdqz.bqd +dir phslrcw +173063 vwvmhwzn.tvg +$ cd fbzmddc +$ ls +dir ccvtbpzw +201647 lzrgqrgc.sjs +192449 rhfgcgz +224557 sbcpcbpd +$ cd ccvtbpzw +$ ls +dir phslrcw +dir prpllzcn +$ cd phslrcw +$ ls +290780 lbl.qmm +$ cd .. +$ cd prpllzcn +$ ls +242024 bhq +118347 frrjh.tsz +dir hcqs +$ cd hcqs +$ ls +104706 csnllvcf +270940 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +244909 czfjsrns.fdm +dir frrjh +68252 lzrgqrgc +146640 npjd.glz +290259 pbzpthgs.bgf +99657 rmfpfmd +dir zllf +$ cd frrjh +$ ls +dir cgcqpjpn +65295 jdqz.bqd +dir phslrcw +$ cd cgcqpjpn +$ ls +dir mwfwrd +$ cd mwfwrd +$ ls +26481 sngtgc +dir zfzddl +$ cd zfzddl +$ ls +47854 fzfdrmbb.ngm +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +251021 fwfpd.vrt +230149 fwjznjtz +140480 gbsmhj +112221 lzrgqrgc.gcr +139564 npjd.glz +dir pvmvm +dir tpzr +$ cd pvmvm +$ ls +73785 tnbgmgt.wqg +$ cd .. +$ cd tpzr +$ ls +16759 tmcmp +$ cd .. +$ cd .. +$ cd .. +$ cd zllf +$ ls +281402 dzrsnzh.smb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wjz +$ ls +266826 cgcqpjpn.spr +dir frvbfjj +111126 jdqz.bqd +257951 pbn.gdc +dir vjs +dir zrnvnb +$ cd frvbfjj +$ ls +257181 glfdplrm +$ cd .. +$ cd vjs +$ ls +dir jmpbwhtj +159996 lzrgqrgc.zcb +192135 phw +dir tdfz +dir wtm +$ cd jmpbwhtj +$ ls +dir fln +dir frrjh +dir nrtcmj +$ cd fln +$ ls +284574 frrjh.scb +$ cd .. +$ cd frrjh +$ ls +10020 npjd.glz +295395 vzq.qvv +$ cd .. +$ cd nrtcmj +$ ls +dir htvm +$ cd htvm +$ ls +dir lwh +$ cd lwh +$ ls +162494 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tdfz +$ ls +dir gtvlbhhw +dir hvssbj +dir phslrcw +$ cd gtvlbhhw +$ ls +172627 sjvcfz +dir tfnhsrc +76460 vvgcv +$ cd tfnhsrc +$ ls +dir hjrjmrwc +$ cd hjrjmrwc +$ ls +41459 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd hvssbj +$ ls +64052 phslrcw +$ cd .. +$ cd phslrcw +$ ls +dir wtdpf +dir zctdqwbc +$ cd wtdpf +$ ls +75802 jztrccm.hvd +$ cd .. +$ cd zctdqwbc +$ ls +127648 vqzcf.wbl +$ cd .. +$ cd .. +$ cd .. +$ cd wtm +$ ls +265433 jztrccm.hvd +20952 mplbrzlp +123701 nwr.tjg +58492 qsdlvr.qzq +$ cd .. +$ cd .. +$ cd zrnvnb +$ ls +228205 ghllsc.fqn +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd cgcqpjpn +$ ls +dir cmnsbsqp +dir hzsn +221975 jdqz.bqd +177439 jztrccm.hvd +dir mqjrcw +dir tdp +211065 zdm.rpl +$ cd cmnsbsqp +$ ls +282903 qzlc +$ cd .. +$ cd hzsn +$ ls +221345 wtdnp.grd +$ cd .. +$ cd mqjrcw +$ ls +156244 gznmpr.dlr +45213 hdb.fjb +202993 pdt +dir znvdz +$ cd znvdz +$ ls +183973 jztrccm.hvd +$ cd .. +$ cd .. +$ cd tdp +$ ls +68564 nmntwl +259117 vzq.qvv +$ cd .. +$ cd .. +$ cd frrjh +$ ls +276034 lzrgqrgc.fgq +dir psqczq +dir qsdlvr +dir snrw +$ cd psqczq +$ ls +158415 jdqz.bqd +127684 lhctqnnz.hgn +123191 phslrcw.prl +dir wrftp +$ cd wrftp +$ ls +234327 qdbbl.czd +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +179591 chbns.sqf +88327 jdqz.bqd +dir mrf +$ cd mrf +$ ls +229849 phslrcw.rdr +$ cd .. +$ cd .. +$ cd snrw +$ ls +dir cbvr +dir lbbrtc +134786 lzrgqrgc.vrt +131987 vzq.qvv +$ cd cbvr +$ ls +120330 htmgqj.nbq +162963 lzrgqrgc.rwq +$ cd .. +$ cd lbbrtc +$ ls +dir tnqdj +$ cd tnqdj +$ ls +72261 lhqnwns.nhj +235520 qsdlvr.dtr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jjl +$ ls +dir fcfpptw +dir frrjh +dir gmdwgs +221258 jdqz.bqd +dir lzrgqrgc +dir mwnmjjj +297139 smjtpl.zzs +$ cd fcfpptw +$ ls +167264 frrjh +101587 hsmhhc.lvb +$ cd .. +$ cd frrjh +$ ls +268280 jztrccm.hvd +127939 nbqgvd +247875 nmnrsqq +dir phslrcw +dir rmgwbq +288264 znrrdmw +$ cd phslrcw +$ ls +dir cgcqpjpn +dir nbwjw +$ cd cgcqpjpn +$ ls +249503 phslrcw.dmf +$ cd .. +$ cd nbwjw +$ ls +dir phslrcw +$ cd phslrcw +$ ls +193336 sqpgwtgp.fpb +$ cd .. +$ cd .. +$ cd .. +$ cd rmgwbq +$ ls +90116 jztrccm.hvd +303005 phslrcw +$ cd .. +$ cd .. +$ cd gmdwgs +$ ls +dir lzrgqrgc +dir mwrj +dir mzqn +104570 qsqqbfg.njt +dir zmj +$ cd lzrgqrgc +$ ls +dir cmn +dir qsdlvr +dir rtz +$ cd cmn +$ ls +20043 bgm.tvn +41301 hqdl.rnr +32026 tsmmr.bfl +$ cd .. +$ cd qsdlvr +$ ls +dir dcsdmqt +$ cd dcsdmqt +$ ls +107938 zdm.rpl +$ cd .. +$ cd .. +$ cd rtz +$ ls +308963 jbwvpjvj.fgn +31573 jdqz.bqd +97775 phslrcw +dir qsdlvr +225929 wwznl.bpd +$ cd qsdlvr +$ ls +52056 bddqnlmg.hjm +236614 qsdlvr.vcz +$ cd .. +$ cd .. +$ cd .. +$ cd mwrj +$ ls +dir dfsm +dir mtfqs +$ cd dfsm +$ ls +32797 jztrccm.hvd +dir mthjq +241875 vzq.qvv +$ cd mthjq +$ ls +147247 zdm.rpl +$ cd .. +$ cd .. +$ cd mtfqs +$ ls +dir crpfs +dir phslrcw +$ cd crpfs +$ ls +24073 jdqz.bqd +269270 jztrccm.hvd +89807 lzrgqrgc.ddj +$ cd .. +$ cd phslrcw +$ ls +124727 cgcqpjpn.zfz +dir gttptlwj +dir phslrcw +dir qsdlvr +$ cd gttptlwj +$ ls +295546 frrjh.tmq +$ cd .. +$ cd phslrcw +$ ls +234122 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +131491 zdm.rpl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mzqn +$ ls +16352 qjllmbh.jfm +36853 zdm.rpl +$ cd .. +$ cd zmj +$ ls +dir dvrwbrbt +dir frrjh +217060 mftczjfs.zch +dir znhsrsfb +$ cd dvrwbrbt +$ ls +dir lzssrrq +$ cd lzssrrq +$ ls +dir qsdlvr +$ cd qsdlvr +$ ls +108975 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd frrjh +$ ls +dir phslrcw +dir zjstpvl +$ cd phslrcw +$ ls +244682 pgn.jqm +$ cd .. +$ cd zjstpvl +$ ls +300736 bsw.wzn +102742 jdqz.bqd +$ cd .. +$ cd .. +$ cd znhsrsfb +$ ls +121093 qsdlvr +$ cd .. +$ cd .. +$ cd .. +$ cd lzrgqrgc +$ ls +50942 jdqz.bqd +26201 ljqgqqrr +71462 lrrfvs.ghl +274208 tqhcl.hcl +$ cd .. +$ cd mwnmjjj +$ ls +184865 mzwmp.jbw +68016 thbh.nbg +$ cd .. +$ cd .. +$ cd mqvn +$ ls +287623 spfzctll \ No newline at end of file diff --git a/src/day02.rs b/src/day02.rs index cebdb21..1094862 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -52,7 +52,7 @@ where let s1: Shape = Shape::parse(l[0]); let s2 = match l[1] { "X" => match s1 { Shape::Rock => Shape::Scissors, Shape::Paper => Shape::Rock, Shape::Scissors => Shape::Paper, }, // Need to lose. - "Z" => match s1 { Shape::Rock => Shape::Paper, Shape::Paper => Shape::Scissors, Shape::Scissors => Shape::Rock, }, // Need to win + "Z" => match s1 { Shape::Rock => Shape::Paper, Shape::Paper => Shape::Scissors, Shape::Scissors => Shape::Rock, }, // Need to win. _ => s1, // Draw. }; diff --git a/src/day07.rs b/src/day07.rs new file mode 100644 index 0000000..c3f5fb1 --- /dev/null +++ b/src/day07.rs @@ -0,0 +1,125 @@ +use std::str::Lines; + +#[derive(Debug)] +pub struct Dir { + files: Vec, + dirs: Vec, +} + +impl Dir { + fn new() -> Dir { + Dir { files: Vec::new(), dirs: Vec::new() } + } + + fn get_file_size(&self) -> i64 { + self.files.iter().sum() + } + + pub fn dir_sizes

(&self, predicate: P, result: &mut Vec) -> i64 + where + P: Fn(i64) -> bool + Copy, + { + let size = self.get_file_size() + self.dirs.iter().map(|dir| dir.dir_sizes(predicate, result)).sum::(); + if predicate(size) { + result.push(size) + } + size + } +} + +pub fn parse(input: &str) -> Dir { + fn create_dir(lines: &mut Lines) -> Dir { + let mut dir = Dir::new(); + while let Some(l) = lines.next() { + let l: Vec<&str> = l.split(' ').collect(); + if l[0] == "$" { + if l[1] == "cd" { + if l[2] == ".." { + return dir; + } else { + let child = create_dir(lines); + dir.dirs.push(child); + } + } + } else if l[0] != "dir" { + dir.files.push(l[0].parse().unwrap()); + } + } + dir + } + let mut lines = input.lines(); + lines.next(); // First line is always the root. + create_dir(&mut lines) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1() { + let commands = "$ 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"; + let root = parse(commands); + let mut sizes: Vec = Vec::new(); + root.dir_sizes(|size| size <= 100_000, &mut sizes); + assert_eq!(sizes.iter().sum::(), 95_437); + } + + #[test] + fn part2() { + let commands = "$ 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"; + let root = parse(commands); + + let root_size = root.dir_sizes(|size| size <= 100_000, &mut Vec::new()); + + let required = root_size - (70_000_000 - 30_000_000); + let mut sizes: Vec = Vec::new(); + root.dir_sizes(|size| size >= required, &mut sizes); + + assert_eq!(*sizes.iter().min().unwrap(), 24933642); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 056e7f5..91e1c7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod day03; mod day04; mod day05; mod day06; +mod day07; fn day01() -> String { let f = fs::File::open("data/day01.input").unwrap(); @@ -45,6 +46,24 @@ fn day06() -> String { format!("part1: {}, part2: {}", day06::first_marker_pos(&signals, 4), day06::first_marker_pos(&signals, 14)) } +fn day07() -> String { + let root = day07::parse(&fs::read_to_string("data/day07.input").unwrap()); + + let (root_size, sum_part1 ) = { + let mut sizes: Vec = Vec::new(); + (root.dir_sizes(|size| size <= 100_000, &mut sizes), sizes.iter().sum::()) + }; + + let min_part2 = { + let to_free = root_size - (70_000_000 - 30_000_000); + let mut sizes: Vec = Vec::new(); + root.dir_sizes(|size| size >= to_free, &mut sizes); + *sizes.iter().min().unwrap() + }; + + format!("part1: {}, part2: {}", sum_part1, min_part2) +} + fn format_micros(t: u128) -> String { if t < 10_000 { format!("{} μs", t) @@ -70,6 +89,7 @@ fn main() { day04, day05, day06, + day07, ); let args: Vec = env::args().skip(1).collect();