From cf3867f3551684df7db34e2b9eb889008d90552b Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 7 Dec 2022 21:08:30 -0800 Subject: [PATCH 01/10] Day 7. Wow that was not great --- day7/Cargo.lock | 7 + day7/Cargo.toml | 8 + day7/data.txt | 956 +++++++++++++++++++++++++++++++++++++++++++++++ day7/src/main.rs | 115 ++++++ day7/test.txt | 23 ++ 5 files changed, 1109 insertions(+) create mode 100644 day7/Cargo.lock create mode 100644 day7/Cargo.toml create mode 100644 day7/data.txt create mode 100644 day7/src/main.rs create mode 100644 day7/test.txt diff --git a/day7/Cargo.lock b/day7/Cargo.lock new file mode 100644 index 0000000..7af9b93 --- /dev/null +++ b/day7/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day7" +version = "0.1.0" diff --git a/day7/Cargo.toml b/day7/Cargo.toml new file mode 100644 index 0000000..b170ccb --- /dev/null +++ b/day7/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day7" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day7/data.txt b/day7/data.txt new file mode 100644 index 0000000..7b779d3 --- /dev/null +++ b/day7/data.txt @@ -0,0 +1,956 @@ +$ cd / +$ ls +dir gqlg +dir hchrwstr +dir lswlpt +189381 mzsnhlf +dir plmdrbn +dir rjwmjd +dir stqq +93174 zfn +dir zjhqnlrr +dir zssdlnc +$ cd gqlg +$ ls +187654 cfrdsjf +100589 ntvmgbw +46922 zcmfcjhf.tzw +$ cd .. +$ cd hchrwstr +$ ls +227766 cljjlcp.pjh +102770 rdbrcf.pjl +9887 rfnjn.lqn +dir rzsrp +dir shlhgj +dir slwwgc +$ cd rzsrp +$ ls +dir rzsrp +$ cd rzsrp +$ ls +dir dvzvmsjz +208001 tnflwcsn +dir zfn +$ cd dvzvmsjz +$ ls +149823 hvgbhm.zsc +$ cd .. +$ cd zfn +$ ls +292424 gqlg.tzc +$ cd .. +$ cd .. +$ cd .. +$ cd shlhgj +$ ls +61753 fzpzwjdt.jvl +195204 jjhc.tzr +288094 zcmfcjhf.tzw +$ cd .. +$ cd slwwgc +$ ls +118185 jjhc.tzr +291916 jwnw.wqv +116377 ptlz +300849 pzjpw.hwg +28679 zfn.grz +$ cd .. +$ cd .. +$ cd lswlpt +$ ls +124160 dbnlt.rqz +154412 sfd +$ cd .. +$ cd plmdrbn +$ ls +dir fmzjhrq +224144 rlc.qns +2985 zcmfcjhf.tzw +$ cd fmzjhrq +$ ls +9749 lqj.szc +dir rrjjsvm +$ cd rrjjsvm +$ ls +66652 zcmfcjhf.tzw +$ cd .. +$ cd .. +$ cd .. +$ cd rjwmjd +$ ls +dir twcrf +125294 zpfws.ctc +dir zwmmrqj +$ cd twcrf +$ ls +229364 tnflwcsn +262716 zcmfcjhf.tzw +$ cd .. +$ cd zwmmrqj +$ ls +11466 gqlg.ftg +220272 lflcgss.jrm +79574 tnflwcsn +$ cd .. +$ cd .. +$ cd stqq +$ ls +185160 cwjqldbf.cnm +266439 dpg.pwn +dir hpgbcvtb +139357 jvps +dir llbthqc +250148 mctn.dqw +dir rzsrp +22196 tnflwcsn +dir whmdbn +$ cd hpgbcvtb +$ ls +231579 fvjjtd.wdb +168118 gjdtscqs +113434 ldbqbg +dir mqjfvg +182214 nnnr +112389 pqqdt.wnn +$ cd mqjfvg +$ ls +170328 gqlg.jjc +$ cd .. +$ cd .. +$ cd llbthqc +$ ls +25611 bftwd.qrr +91550 tnflwcsn +$ cd .. +$ cd rzsrp +$ ls +272315 wrsfh.pzd +123877 zcmfcjhf.tzw +$ cd .. +$ cd whmdbn +$ ls +29628 jjhc.tzr +65193 mrhb +$ cd .. +$ cd .. +$ cd zjhqnlrr +$ ls +dir bzzvj +dir ddzmvh +dir dsjd +180806 fqz.dch +dir nnzvl +109161 rptmqt +6463 rzsrp.sjf +111549 rzsrp.wwz +241532 zfn +$ cd bzzvj +$ ls +278759 gqlg.nsn +174223 mvggzqrq +156518 tnflwcsn +dir wjtlg +dir zfn +$ cd wjtlg +$ ls +52198 fvqbtm.tch +44881 tnflwcsn +$ cd .. +$ cd zfn +$ ls +22926 tnflwcsn +$ cd .. +$ cd .. +$ cd ddzmvh +$ ls +dir cpvfln +180764 gqlg.bjq +dir hgrnmh +186254 hgrnmh.tpr +dir hjpgf +dir mghmb +dir pvdwhmcr +256578 rzsrp +269955 shlhgj +172163 vrr.nnm +$ cd cpvfln +$ ls +87025 hgrnmh.lvr +187453 rwmq.dph +dir sthhc +$ cd sthhc +$ ls +289580 fdzwmfnf +57504 hgrnmh.vqj +277639 mzcfchr.mch +dir slsj +dir vsvvprg +94836 zqglfrl +$ cd slsj +$ ls +178013 hdjgtqt +$ cd .. +$ cd vsvvprg +$ ls +102783 fvqbtm.tch +$ cd .. +$ cd .. +$ cd .. +$ cd hgrnmh +$ ls +dir mrnmbbt +240216 rzsrp.nfg +$ cd mrnmbbt +$ ls +245579 gqlg +$ cd .. +$ cd .. +$ cd hjpgf +$ ls +257544 cjfd.wdq +296077 jjhc.tzr +195752 zcmfcjhf.tzw +$ cd .. +$ cd mghmb +$ ls +dir bgrcsdc +dir btjq +dir gqlg +dir gtwdqw +dir hgrnmh +dir jts +dir jvp +dir jvrjcv +206272 pgf.rgj +130746 tnflwcsn +256698 zfn.dhl +$ cd bgrcsdc +$ ls +235762 lflcgss.jrm +$ cd .. +$ cd btjq +$ ls +dir bvrcqb +dir drslln +dir dwzpblb +dir gqlg +dir hgrnmh +dir hvb +dir rqtc +dir vsdz +dir wrnftqh +$ cd bvrcqb +$ ls +51508 gjjg.msw +dir gqlg +13206 pdwn +10427 vbw +6909 zfn.ztq +$ cd gqlg +$ ls +20816 cmgcvlh.mzs +10358 fvqbtm.tch +215142 rzsrp.dns +192153 wdjdppzm +$ cd .. +$ cd .. +$ cd drslln +$ ls +258102 tnflwcsn +$ cd .. +$ cd dwzpblb +$ ls +302466 fvqbtm.tch +$ cd .. +$ cd gqlg +$ ls +dir hnr +dir mdzdht +$ cd hnr +$ ls +dir fjd +159298 fvqbtm.tch +46324 gchdll.nrm +287907 lflcgss.jrm +dir mqp +140272 twdff +47399 zfn +$ cd fjd +$ ls +227109 fvqbtm.tch +223413 wdsfwwwm.rmd +$ cd .. +$ cd mqp +$ ls +dir rzsrp +$ cd rzsrp +$ ls +dir pgwng +$ cd pgwng +$ ls +218271 hgrnmh.gnf +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mdzdht +$ ls +dir gqlg +dir tvd +$ cd gqlg +$ ls +dir gfjqbdp +108335 jjhc.tzr +$ cd gfjqbdp +$ ls +143536 fzvn.jtv +$ cd .. +$ cd .. +$ cd tvd +$ ls +238491 wsjmrlqv.bwb +$ cd .. +$ cd .. +$ cd .. +$ cd hgrnmh +$ ls +265289 lflcgss.jrm +111283 thb +$ cd .. +$ cd hvb +$ ls +dir hgrnmh +dir smmwg +$ cd hgrnmh +$ ls +dir wpvssts +84939 zcmfcjhf.tzw +$ cd wpvssts +$ ls +305634 whcmr +$ cd .. +$ cd .. +$ cd smmwg +$ ls +36722 cvdbwg.rll +16840 nnhbcbwz +dir rzsrp +9405 zfn.jzr +$ cd rzsrp +$ ls +dir fzsngr +$ cd fzsngr +$ ls +142687 zcmfcjhf.tzw +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rqtc +$ ls +dir pbzhfsg +dir rzsrp +dir shlhgj +$ cd pbzhfsg +$ ls +41865 hpshz.sdf +$ cd .. +$ cd rzsrp +$ ls +dir gqlg +dir mbsjjzft +$ cd gqlg +$ ls +dir gqlg +dir nvjqp +$ cd gqlg +$ ls +21860 tnflwcsn +$ cd .. +$ cd nvjqp +$ ls +dir gcq +$ cd gcq +$ ls +dir mslhm +dir rzsrp +$ cd mslhm +$ ls +41304 rpgvllw.zfj +$ cd .. +$ cd rzsrp +$ ls +290199 ddsnt +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mbsjjzft +$ ls +47727 ggpmg.pps +dir stgszvmj +275954 tnflwcsn +248613 vbgnwh.sjz +$ cd stgszvmj +$ ls +dir dthcrbmr +dir shlhgj +206580 zcmfcjhf.tzw +dir zfn +$ cd dthcrbmr +$ ls +dir lhp +dir npf +dir qbgrjr +$ cd lhp +$ ls +37925 hgrnmh.lvm +$ cd .. +$ cd npf +$ ls +77162 bqvd +$ cd .. +$ cd qbgrjr +$ ls +48767 fvqbtm.tch +$ cd .. +$ cd .. +$ cd shlhgj +$ ls +dir wtlngn +35422 zfn +$ cd wtlngn +$ ls +dir gqlg +$ cd gqlg +$ ls +64761 fwtqw +$ cd .. +$ cd .. +$ cd .. +$ cd zfn +$ ls +47247 dmlr.flv +259570 htvn.rbz +149238 qrdw +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd shlhgj +$ ls +dir cjmv +dir tns +$ cd cjmv +$ ls +140919 jjhc.tzr +$ cd .. +$ cd tns +$ ls +248127 fvqbtm.tch +216202 pwg +dir qhhtgsvz +$ cd qhhtgsvz +$ ls +68979 fvqbtm.tch +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vsdz +$ ls +224491 hgrnmh +dir jsrqjjtc +dir shlhgj +123062 zfn.gss +$ cd jsrqjjtc +$ ls +245991 phc +$ cd .. +$ cd shlhgj +$ ls +284208 ctwmwlp.lnm +11067 vwsrmlf.dvz +$ cd .. +$ cd .. +$ cd wrnftqh +$ ls +237658 lldrh +14804 nszc.wdf +$ cd .. +$ cd .. +$ cd gqlg +$ ls +211474 jjhc.tzr +$ cd .. +$ cd gtwdqw +$ ls +92613 fvqbtm.tch +206813 ppzhmnb.grd +$ cd .. +$ cd hgrnmh +$ ls +249351 bftm +$ cd .. +$ cd jts +$ ls +132190 gqlg.vnn +208091 ldnq +202593 zcmfcjhf.tzw +$ cd .. +$ cd jvp +$ ls +258220 lflcgss.jrm +$ cd .. +$ cd jvrjcv +$ ls +20148 dzzlwp.bdj +289774 hgrnmh.bvc +296981 jwscd.cvj +16617 tbnwvcl.tlv +$ cd .. +$ cd .. +$ cd pvdwhmcr +$ ls +dir bhmmsbmb +dir bjwbvcq +dir hgrnmh +dir ltvtrtth +dir prtqcb +dir rmn +dir shlhgj +dir tmzcmb +dir vlbqvps +dir wmmfp +$ cd bhmmsbmb +$ ls +dir nccjwf +$ cd nccjwf +$ ls +dir drwhjfjn +206638 lflcgss.jrm +$ cd drwhjfjn +$ ls +291772 zcmfcjhf.tzw +$ cd .. +$ cd .. +$ cd .. +$ cd bjwbvcq +$ ls +dir zfn +$ cd zfn +$ ls +303808 wrbldbd +$ cd .. +$ cd .. +$ cd hgrnmh +$ ls +142765 fvqbtm.tch +159497 scqf.scf +270819 zcmfcjhf.tzw +$ cd .. +$ cd ltvtrtth +$ ls +dir fwtwqvn +156894 jnmqdq.zgp +297860 lflcgss.jrm +dir mcgcc +dir zfn +179328 zfn.vnn +dir zzsnz +$ cd fwtwqvn +$ ls +dir rqndc +dir rzsrp +124999 rzsrp.tvr +dir shlhgj +$ cd rqndc +$ ls +45124 zrcv +$ cd .. +$ cd rzsrp +$ ls +74041 rzsrp +$ cd .. +$ cd shlhgj +$ ls +117219 qptfpz.cgc +281188 shlhgj.bhd +121750 sptql +173637 vqp.dbs +$ cd .. +$ cd .. +$ cd mcgcc +$ ls +18295 gqlg +dir ngvwhnvf +177283 rjsrn.rlq +54239 rzsrp.tfq +$ cd ngvwhnvf +$ ls +207616 jjhc.tzr +$ cd .. +$ cd .. +$ cd zfn +$ ls +dir cwzbljh +$ cd cwzbljh +$ ls +134952 hmchpgn.nwf +$ cd .. +$ cd .. +$ cd zzsnz +$ ls +273014 shlhgj.pls +$ cd .. +$ cd .. +$ cd prtqcb +$ ls +dir dtr +dir rzsrp +199012 tnflwcsn +$ cd dtr +$ ls +dir gqlg +dir grwpbs +dir hgrnmh +115701 lflcgss.jrm +247838 tnflwcsn +268721 zcmfcjhf.tzw +15320 zwcd.dqb +$ cd gqlg +$ ls +285009 jjhc.tzr +125691 nhnlcjjt.zlj +54424 rzsrp.rll +$ cd .. +$ cd grwpbs +$ ls +59390 rzsrp.cms +$ cd .. +$ cd hgrnmh +$ ls +185747 lvtnqqdh.nzq +$ cd .. +$ cd .. +$ cd rzsrp +$ ls +268631 hvvpfd.smn +263890 tcrctpb.pcc +$ cd .. +$ cd .. +$ cd rmn +$ ls +dir gtnvc +205894 hqb.fwh +280887 lvvmjb.scj +dir rsnqlfsz +$ cd gtnvc +$ ls +28523 jjhc.tzr +$ cd .. +$ cd rsnqlfsz +$ ls +dir hgrnmh +dir nsfhtthc +$ cd hgrnmh +$ ls +183216 fvqbtm.tch +$ cd .. +$ cd nsfhtthc +$ ls +284747 jspmbh.rvh +$ cd .. +$ cd .. +$ cd .. +$ cd shlhgj +$ ls +144391 dzf +dir lrths +178083 nhlmsd.mnc +dir rwn +173833 shlhgj.qss +39183 tnflwcsn +125029 zfn.hjr +$ cd lrths +$ ls +dir hgrnmh +dir pfc +86563 rzsrp.gbc +280631 tmhnc.hmc +28181 ttsnjc.rhm +98060 vdmr.czg +dir vlshvmvj +dir wsn +210337 zfn.pqp +$ cd hgrnmh +$ ls +dir mvrlcvj +$ cd mvrlcvj +$ ls +121034 vssbr.qjm +$ cd .. +$ cd .. +$ cd pfc +$ ls +dir gqlg +$ cd gqlg +$ ls +305519 hdhn.wds +$ cd .. +$ cd .. +$ cd vlshvmvj +$ ls +56641 lflcgss.jrm +29190 rzsrp.vsh +36663 zcmfcjhf.tzw +$ cd .. +$ cd wsn +$ ls +137307 gmdfbfjf.crs +dir hgtqj +297924 lflcgss.jrm +dir shlhgj +$ cd hgtqj +$ ls +214202 qnlszftb.bzg +$ cd .. +$ cd shlhgj +$ ls +dir pmldlbbh +$ cd pmldlbbh +$ ls +263722 zwfptl.wsh +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rwn +$ ls +279589 ttqg.qct +$ cd .. +$ cd .. +$ cd tmzcmb +$ ls +138557 jjhc.tzr +$ cd .. +$ cd vlbqvps +$ ls +dir nmfnf +123659 rzsrp +$ cd nmfnf +$ ls +dir jhpnd +50537 trzvzvgz.bqp +41828 vps +195245 wlwt.ftj +$ cd jhpnd +$ ls +257519 shlhgj.fwz +32963 wzmbdtz +$ cd .. +$ cd .. +$ cd .. +$ cd wmmfp +$ ls +94749 lflcgss.jrm +$ cd .. +$ cd .. +$ cd .. +$ cd dsjd +$ ls +284923 bjvwg.phz +dir bwjp +dir gqlg +dir hgrnmh +8771 jjhc.tzr +dir jntt +271995 qbtn.vps +299501 shlhgj.crl +dir vmph +dir zvg +$ cd bwjp +$ ls +117739 vtrg +$ cd .. +$ cd gqlg +$ ls +dir dwhr +159876 hgrnmh.jvw +dir rzsrp +$ cd dwhr +$ ls +193289 gqlg +171323 lflcgss.jrm +$ cd .. +$ cd rzsrp +$ ls +dir pmsgd +$ cd pmsgd +$ ls +12648 lflcgss.jrm +dir wbppspwq +$ cd wbppspwq +$ ls +38834 tnflwcsn +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd hgrnmh +$ ls +76649 cfdcbvdl.gfs +257249 gmmzf +83045 lhrtfcz +$ cd .. +$ cd jntt +$ ls +286866 lhqvt.lst +$ cd .. +$ cd vmph +$ ls +dir fnvpsr +dir gfnvsznj +19765 zcmfcjhf.tzw +204319 zzsnj.dcp +$ cd fnvpsr +$ ls +dir flz +173924 fvqbtm.tch +248297 mjrhs.scq +dir rzsrp +130580 zfn +$ cd flz +$ ls +106168 brsgg +$ cd .. +$ cd rzsrp +$ ls +221882 hgrnmh.nsl +dir smd +$ cd smd +$ ls +dir drsld +131686 fvqbtm.tch +dir gqlg +288283 hcw +$ cd drsld +$ ls +85885 zfn.wdf +$ cd .. +$ cd gqlg +$ ls +8466 dzjtj.zbc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd gfnvsznj +$ ls +dir fqb +265075 gqlg +dir hdhmbtj +74385 hpz +dir lzdq +dir rlvqmwv +dir rzsrp +$ cd fqb +$ ls +55182 ccjgpg +245930 ppdgwcm.zfb +164364 sgtgqdn.tfp +$ cd .. +$ cd hdhmbtj +$ ls +dir gqlg +68609 jjhc.tzr +dir rzsrp +dir vrmrnt +66988 zcmfcjhf.tzw +$ cd gqlg +$ ls +156776 tnflwcsn +dir vdcbg +$ cd vdcbg +$ ls +261780 lflcgss.jrm +$ cd .. +$ cd .. +$ cd rzsrp +$ ls +27818 zfn +$ cd .. +$ cd vrmrnt +$ ls +273664 msmzfnj.llc +145156 svzmwnrq.chh +$ cd .. +$ cd .. +$ cd lzdq +$ ls +214363 fms +190021 lfzcgh.dsq +21357 qtvvmj.zqw +$ cd .. +$ cd rlvqmwv +$ ls +dir gqlg +198296 gqlg.sdz +842 gshcswc.dcb +dir shlhgj +199828 snng +$ cd gqlg +$ ls +183087 zfn.nsj +$ cd .. +$ cd shlhgj +$ ls +73040 tzgjrqmb +$ cd .. +$ cd .. +$ cd rzsrp +$ ls +229576 vtftgng.wfz +$ cd .. +$ cd .. +$ cd .. +$ cd zvg +$ ls +85701 shlhgj.dvf +$ cd .. +$ cd .. +$ cd nnzvl +$ ls +dir wwzsf +$ cd wwzsf +$ ls +105177 tvbdz +$ cd .. +$ cd .. +$ cd .. +$ cd zssdlnc +$ ls +dir nfgh +166004 twdcrh.zld +dir zfn +$ cd nfgh +$ ls +102281 fvqbtm.tch +$ cd .. +$ cd zfn +$ ls +dir fbmww +dir jsst +206602 shlhgj.cln +$ cd fbmww +$ ls +179734 fll +$ cd .. +$ cd jsst +$ ls +dir flp +$ cd flp +$ ls +32274 gctgt.stn +67650 ggvj.bwz diff --git a/day7/src/main.rs b/day7/src/main.rs new file mode 100644 index 0000000..6e764f2 --- /dev/null +++ b/day7/src/main.rs @@ -0,0 +1,115 @@ +use std::cell::RefCell; +use std::collections::HashMap; +use std::rc::Rc; + +#[derive(Debug)] +struct File { + name: String, + size: usize, +} + +#[derive(Debug)] +struct Directory { + name: String, + files: Vec, + directories: Vec>>, + parent: Option>>, +} + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let lines: Vec<&str> = contents.lines().collect(); + let root = parse(&lines); + + let mut sizes: HashMap = HashMap::new(); + set_sizes(root, &mut sizes); + + // Part 1 + let sum = sizes + .to_owned() + .into_values() + .filter(|&x| x <= 100000usize) + .sum::(); + println!("Sum: {:?}", sum); + + // Part 2 + let total = 70000000; + let needed = 30000000; + let used = sizes.get("").unwrap(); + let unused = total - used; + let target = needed - unused; + let delete = sizes + .values() + .fold(total, |acc, &x| if x < acc && x > target { x } else { acc }); + println!("Delete: {:?}", delete); +} + +fn parse(lines: &Vec<&str>) -> Rc> { + let root = Rc::new(RefCell::new(Directory { + name: String::from("/"), + files: Vec::new(), + directories: Vec::new(), + parent: None, + })); + + let mut dir = Rc::clone(&root); + + for line in lines.get(1..lines.len()).unwrap() { + let parts = line.split(" ").collect::>(); + + match parts.as_slice() { + ["$", "ls"] => (), // ignore. we'll match on dirs and files below + ["dir", _] => (), // ignore. we'll add it when we cd into it + [size, name] => { + dir.borrow_mut().files.push(File { + name: String::from(*name), + size: size.parse().unwrap(), + }); + } + ["$", "cd", ".."] => { + // go up a directory by setting dir to the parent + let temp = Rc::clone(&dir); + dir = Rc::clone(temp.borrow().parent.as_ref().unwrap()); + } + ["$", "cd", path] => { + // go down a directory by creating one and pointing dir at it + let new_dir = Rc::new(RefCell::new(Directory { + name: String::from(*path), + files: Vec::new(), + directories: Vec::new(), + parent: Some(Rc::clone(&dir)), + })); + let temp = Rc::clone(&new_dir); + dir.borrow_mut().directories.push(temp); + dir = new_dir; + } + _ => (), + }; + } + + return root; +} + +fn get_path(dir: Rc>) -> String { + match &dir.borrow().parent { + Some(d) => dir.borrow().name.to_string() + "/" + &get_path(d.to_owned()), + None => "".to_string(), + } +} + +fn set_sizes(dir: Rc>, hash: &mut HashMap) -> usize { + let d = dir.borrow(); + let mut size = 0; + + // add all the file sizes to this dir's size + d.files.iter().for_each(|file| size += file.size); + + // add all the sub dirs' size to this dir's size + d.directories + .iter() + .for_each(|child| size += set_sizes(Rc::clone(child), hash)); + + let path = get_path(dir.to_owned()); + hash.insert(path, size); + size +} diff --git a/day7/test.txt b/day7/test.txt new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/day7/test.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 From 273561c79fde890fead6a4e8547b188cacc8bf79 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 8 Dec 2022 12:27:36 -0800 Subject: [PATCH 02/10] Day 8. So much easier than 7 whewwww --- day8/Cargo.lock | 7 ++++ day8/Cargo.toml | 8 ++++ day8/data.txt | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ day8/src/main.rs | 56 +++++++++++++++++++++++++++ day8/test.txt | 5 +++ 5 files changed, 175 insertions(+) create mode 100644 day8/Cargo.lock create mode 100644 day8/Cargo.toml create mode 100644 day8/data.txt create mode 100644 day8/src/main.rs create mode 100644 day8/test.txt diff --git a/day8/Cargo.lock b/day8/Cargo.lock new file mode 100644 index 0000000..0a523ad --- /dev/null +++ b/day8/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day8" +version = "0.1.0" diff --git a/day8/Cargo.toml b/day8/Cargo.toml new file mode 100644 index 0000000..4879259 --- /dev/null +++ b/day8/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day8" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day8/data.txt b/day8/data.txt new file mode 100644 index 0000000..e6d0149 --- /dev/null +++ b/day8/data.txt @@ -0,0 +1,99 @@ +201111211202111301212201403141040230323033312212134523125000140404300230341330300221220012110101011 +011220103113221111202122124010033421404432343233535151335133353340034134034332440001110102010010220 +122121120300202230411332402004044235424325455343231124521355323541403402031010313440121321321322221 +200021211323122312440342411331012332252341254144425451345233324423152201041200212200331121111210011 +022213121203213301221100041021415245132424534145512125411151335224315551123141404223410032032000022 +101131310332214402243114310442325141344331525541232153453225442351235123130234413214313330121121101 +101102011001014411302320032155533351421345311554341111544153414344211242545240114004311012303003230 +101023122212322231421422241433315213542415532226334465642351151321144451152132311230002433230001110 +101102012000130221414322332331245251153242654525425363234533655232423135443124134230202313201000123 +113023100223214000232555442143243135556634652423636245645335623323351235221252344243002420033131123 +021023012400222133115232154155255455226363244263663662564362455342612241224452135301204334403311211 +011011120014313211333124154235235522553465256455435352362343343356452125515224121432404101240133203 +011310103034221123212123555245622666563625344356454344325522453265262433154414231333142413311331323 +303301320412010454251154421242542562366466364652264364325544554624545256254413534434331434344310311 +211332131030004243315521542363644424356453643345544752562524552255353324654134525543231434112230100 +002102403432244535421254566353326346446265345654366646566543552562536326635315453214125414141214102 +331140031313041444123423526426422252527375577546753455777737547626364645566461554234123523430311313 +330401323101512253253516526653653542734767336644345445665364743533364355534236521342413154132413122 +330411204212311445244535522655343573747374364345564574375763553465534526662642244443311121403103124 +023332003431144254543626226245566455744465356337347475347374637637653442626233624414255134423423042 +244210400021232153266652252325756653575474576355747475663555354556777566436342344532415443212134234 +243022342335541213565253544236755753776564447733743446775775654547533666422423326624134345443023202 +400214112533143341263455565263566576767643378645664845674353474533736575624522664521123334133420203 +314412035353435253634446462463763443376476847467648547566465453356757464475343435464134531133123324 +210413325433315134452365563547564744658868746684566644877466576346553654766362254362221413424430422 +440400434542543525543663633667763537556667668747746756785587458767563547637623326226661151144334010 +203031334152125642466635547657766584664687848767577545486757587488654373354446324524564152231113112 +241001452244343336664255646743677756848545865758665674685886656454447657464556245442364333553250131 +014023224515325454455574567445744885857785756585855854887668646555746475564455722536445351532451133 +240034522142434662453346666663547675448567854897757695674874464454475574464737776424553231541143102 +202432344512662423437374345345446848546655697769865799957684557848684543374647545532223321334341333 +123325111441445225523673536445447568774877677796777678697975965546774547536673437544656344512135444 +310254211535525232233446556788846647746897588857688579889788855477655648566666635426236522424112342 +034313254213653456477557533774786647669769587699976668986696667747555648883667447465462526444143342 +143141533334542466354344755484874754698685556789559989587957999587645467546553365742323424612353420 +023355513126235334753777564744657489776767789956675687857787669856547584455737646446352333233414322 +223531421366552557666573477756875578695968796778976887859959556598778688876655735777332545512434212 +324553233365452565557437476777687565788999986896686778796755558896856684686773556344634525262453452 +213242455253625376773454787557859959899988899999978889797885876979769647857487767764545335245255213 +153212131552226337374543645684765767796896876966989866686867979876788867784675754744525622655431425 +043231154325334367553646774578457959887979898666866967789799775569887978674668454463732263426513135 +335542344226645477333775676444769565755967887779666696689698896656597875884556557743462633443124152 +335323556552426765767775874764689686976988996666779796996969887796587577847457535454643262525413534 +031342356243536356663654658854697858887697788666879779897996867856867654685767655656766443625531132 +335345116666525747477647458845785958767699688899778878776776988777668657478884857465753336533142152 +431315146645532647447566887678656986686996889897797878877768979656986556466744563333546332255135135 +345455244545432335674458658587555587989897879999798888988976986779787768754855836367363664566413353 +512151454564534653477764765466595799679979878789789879998767887865797755765458674344355656253343334 +223351266325633575663774564489796879687898688978997788798999779796767956656764647364636656442223522 +453333263535357446366584455459557799968998879988898977978787696768578657875454473547567326532334111 +522431356454444356554374544846975859978878977879979897898976787967789578746656874774547443245541214 +524451243233255555453685476745875568898798878799987879899778979895889689545784464464664644546653534 +351343243662255667567757864679695778779676778997998897779866887675677768667848643753754363345434313 +422125253344236737334386654576687898997799677887987777887686867895579866465457665345774366464351334 +421312154235324535653777678786976887577869769887898799897698686878966689468464555357752255363554413 +134231353356532647544747466746997955968966989988799777786898667996595997676454734577542423644111423 +254152156456563434474788846574786769698798686897989989968667786695865858667776755455566654264132134 +325554124554345334573567875686789766777879789968978788797868997657598864755765764646452636426312235 +054421142246245654657458647846877765696688798786967788769769989568865887586878654537663362654432212 +313445116364256444775744655676857696698998799776867668888686999587568854476648764345346236246232241 +143114554233652245375455887678668996669686996999796887877777686757966588885786556444465552442533324 +251331534446564265464364486744686986675876988887767766688896987878765756544754746565355566534235144 +043312335432626344536643484678579666899596766779666899678676979556965588747766665377565544465543531 +242445211324566636637644588748444588767957766968968978786788586989657745475556775355542555362121442 +243253334442556446446767754567476657566957796656988796979699968699856846446535677455225423345132114 +431231245536522344674653635685865849787997986689665576586668667768645854645567336572252526524445114 +431352413545636624334373564776844687557987796967657766857786797968874766656443453336264564435235253 +320554134146445446354475753477858558775599755977987865757897668588588666485457344365453335234122310 +333244132156433233245637373688448784459857795676588655998585696485786865554473364636554335355224153 +113345451542245623453737457745456844757575897756758966757557585554868888643374344325656234154414322 +444045515145356432335647545655765786587886758778866688968984877775888576477747477332355221552413204 +111132223523345626324747765577376687656748476986565687576768448558884573773436752362454612221523223 +022004333435234546566255554533348546676766478456868467444674448586545655467537436553554433123421120 +333231125332152663266433667535537778555667876464765456758647764446877656465367323254523253352510413 +122420553331452433333353464676537646864677748867574756565588454444657764464332235454635225432522004 +120442052444321222226423365375645435468656658447866466574856466886677654746522644365264535531343440 +421404315233111265423463525657764565658784687768567656464684688763534555373553346542234345145442244 +331231242152133344456244633655545655745345664668565885467456447677744435752654334432232553354400240 +203040023155425242246555225235473556554656638646764676455766756555576576336522336454124215215404311 +020211430251454452662234222644467675755747776474545447746753537773655567623542436262213453434423110 +121222320253353333233652252225333343646436546477634345533376676557363636362664225322253234514301234 +131331122021145511555626563643364773337345554777664437674344775376757362452454652245212143131222333 +333033033132243135213222343542265476344656453763774355443644566737723253264252451243142121312244244 +002204430244435144434515354453332632665637375363445456667774576662453443646454532431524140202312242 +121314024424022223215424644426642356264655357667436346735353466356245254264535315425323522230030301 +021012030013043135124451434556465446663353754754774574457663344656553456246613524313152023413220330 +323211122442404125421353336334324324563564625653556556652432462453235325362525324531133444231304313 +221212242244240214314234323556334553353656452662622464552256465443444565421332134313540102100220131 +003121311143322334241133533143446663652225433332652353542523664466644655215235421122322101243201202 +302132313044423014553344334444135543546462343625633335635554425246463354552555544450021401441212013 +211320002042341321404351332421424422345645623225243263532425463422453534224512413420003024103010003 +121321233203044310220544225434555523322652465244626362265436622244421244514121221412000233313302001 +033330113222134212123014455534453323452146635634333522662322621122442141151325121244124021100331010 +003211203200004004334013134312421412135211541122652523655533352215155345414442033214440410020130131 +000310132331000133341101412343222324131145425545545422415232421245514432554132410203120223330322031 +010222121001201110014203011212115523421525414334222144411533424243442552424243301203331330320011211 +202210333132013102411304342331452231412212555243242135532222244343234442033031110220213320300213112 +021110333231201113044040042034211335553321154444412432243122252145522223420412102042311030021122200 +212222233011232310420321434331332422434543133323345512355241515513414041433311311240302022331021120 diff --git a/day8/src/main.rs b/day8/src/main.rs new file mode 100644 index 0000000..971bd52 --- /dev/null +++ b/day8/src/main.rs @@ -0,0 +1,56 @@ +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let grid = contents // make it a grid of u32 + .lines() + .map(|l| { + l.chars() + .map(|c| c.to_digit(10).unwrap()) + .collect::>() + }) + .collect::>>(); + + let height = grid.len(); + + let mut visible = 0; + let mut high_score = 0; + + for (row, line) in grid.iter().enumerate() { + for (col, tree) in line.iter().enumerate() { + let width = line.len(); + + let north = (0..row).map(|r| grid[r][col]).rev().collect::>(); + let south = (row + 1..height) + .map(|r| grid[r][col]) + .collect::>(); + let west = (0..col).map(|c| grid[row][c]).rev().collect::>(); + let east = (col + 1..width).map(|c| grid[row][c]).collect::>(); + + let dirs = vec![north, south, west, east]; + + // Part 1 + if row == 0 || col == 0 || row == height - 1 || col == width - 1 { + visible += 1; // if it is on the edge, it is visible + } else { + if dirs.iter().any(|dir| dir.iter().all(|t| tree > t)) { + visible += 1; + } + } + + // Part 2 + let score = dirs.iter().fold(1, |acc, dir| { + let position = dir.into_iter().position(|x| tree <= x); + acc * match position { + Some(x) => x + 1, + None => dir.len(), // hit an edge. return the distance to the edge + } + }); + + if score > high_score { + high_score = score; + } + } + } + + println!("Number Visible: {:?}", visible); + println!("Highest Score: {:?}", high_score); +} diff --git a/day8/test.txt b/day8/test.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/day8/test.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 From 7fc1ca6ed41bd3a390e17ca265d79bbda85159cf Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Fri, 9 Dec 2022 13:16:33 -0800 Subject: [PATCH 03/10] Day 9. Pretty straight forward. The borrow checker must have felt bad for me and did not give me any problems today --- day9/Cargo.lock | 7 + day9/Cargo.toml | 8 + day9/data.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++++ day9/src/main.rs | 103 +++ day9/test.txt | 8 + day9/test2.txt | 8 + 6 files changed, 2134 insertions(+) create mode 100644 day9/Cargo.lock create mode 100644 day9/Cargo.toml create mode 100644 day9/data.txt create mode 100644 day9/src/main.rs create mode 100644 day9/test.txt create mode 100644 day9/test2.txt diff --git a/day9/Cargo.lock b/day9/Cargo.lock new file mode 100644 index 0000000..f5578cf --- /dev/null +++ b/day9/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day9" +version = "0.1.0" diff --git a/day9/Cargo.toml b/day9/Cargo.toml new file mode 100644 index 0000000..b5e5273 --- /dev/null +++ b/day9/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day9" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day9/data.txt b/day9/data.txt new file mode 100644 index 0000000..67c0d6c --- /dev/null +++ b/day9/data.txt @@ -0,0 +1,2000 @@ +L 1 +D 2 +U 1 +L 1 +R 2 +L 2 +U 1 +D 2 +R 2 +L 2 +U 1 +D 2 +R 1 +D 2 +L 2 +D 1 +L 2 +R 1 +U 2 +R 2 +U 2 +L 2 +U 1 +L 2 +R 1 +D 1 +R 1 +L 1 +R 1 +L 1 +R 1 +U 1 +R 1 +L 1 +D 2 +L 2 +D 2 +L 1 +R 1 +D 1 +R 1 +D 2 +L 1 +D 2 +L 1 +R 2 +D 2 +R 2 +U 1 +R 1 +U 2 +R 1 +U 2 +R 1 +D 2 +L 1 +D 2 +U 1 +R 2 +D 2 +L 1 +U 2 +R 2 +L 2 +D 2 +L 2 +D 2 +R 1 +U 2 +L 2 +U 1 +R 1 +D 1 +L 1 +D 1 +U 1 +D 2 +L 1 +R 1 +D 1 +R 1 +L 2 +R 1 +L 1 +U 2 +D 2 +U 1 +L 1 +U 2 +R 1 +D 2 +U 1 +D 1 +L 1 +D 2 +U 2 +D 1 +U 2 +R 2 +L 2 +D 1 +R 2 +D 1 +L 2 +D 2 +L 2 +R 1 +L 1 +R 2 +L 2 +R 2 +U 1 +R 1 +U 3 +D 2 +U 3 +L 1 +R 1 +U 3 +D 1 +L 1 +R 3 +D 2 +L 3 +R 3 +D 2 +R 2 +U 3 +R 3 +U 3 +R 2 +U 3 +D 2 +L 3 +U 3 +R 1 +U 1 +R 3 +U 2 +L 1 +R 1 +U 1 +L 3 +R 3 +U 3 +D 2 +U 1 +L 1 +D 3 +R 3 +U 2 +L 1 +R 3 +L 3 +D 3 +U 3 +R 2 +L 1 +U 1 +L 3 +R 3 +U 1 +R 3 +L 2 +R 2 +D 3 +U 1 +D 3 +U 3 +R 3 +D 2 +L 2 +U 2 +D 3 +L 3 +U 3 +R 3 +D 2 +R 3 +U 1 +D 1 +U 2 +R 1 +D 3 +R 1 +L 2 +U 3 +D 1 +L 1 +D 3 +U 2 +L 3 +R 3 +L 1 +D 2 +L 3 +U 3 +L 1 +D 2 +L 1 +D 1 +L 2 +U 2 +D 2 +U 2 +L 2 +D 3 +L 3 +D 3 +R 3 +U 1 +R 3 +U 1 +R 3 +D 3 +U 2 +D 1 +R 2 +L 2 +R 2 +D 2 +R 2 +D 1 +R 3 +D 1 +U 2 +L 4 +D 1 +L 2 +R 1 +U 1 +D 3 +U 4 +D 2 +L 3 +R 2 +D 3 +R 4 +U 1 +D 1 +U 1 +L 3 +U 1 +D 3 +R 3 +D 3 +R 1 +U 4 +R 1 +U 1 +R 2 +L 4 +U 3 +D 2 +R 2 +D 1 +U 2 +L 3 +D 4 +U 4 +D 4 +U 4 +L 3 +U 3 +D 3 +R 3 +L 4 +R 3 +D 2 +L 2 +R 1 +D 1 +L 4 +R 3 +D 2 +U 1 +L 2 +R 1 +U 2 +L 4 +R 4 +L 3 +U 4 +D 2 +U 4 +D 4 +L 1 +U 4 +L 3 +U 2 +D 2 +R 2 +D 3 +R 4 +D 1 +U 4 +D 1 +U 1 +D 3 +L 3 +D 2 +R 4 +D 4 +L 2 +U 2 +L 3 +R 4 +L 2 +R 2 +D 3 +L 2 +D 4 +U 3 +D 4 +L 3 +R 2 +U 3 +D 4 +R 2 +U 3 +R 1 +D 1 +R 1 +D 2 +R 3 +D 4 +L 1 +U 3 +L 3 +U 4 +R 1 +U 3 +L 4 +D 5 +R 2 +D 2 +U 2 +D 1 +R 4 +D 1 +L 4 +D 4 +L 1 +R 1 +D 4 +U 4 +L 1 +D 4 +L 4 +U 5 +R 4 +D 4 +R 5 +U 3 +L 5 +U 1 +R 4 +L 1 +R 3 +L 2 +D 5 +R 1 +D 4 +R 2 +U 5 +L 3 +R 5 +U 4 +R 1 +D 4 +U 3 +R 3 +L 3 +R 3 +L 3 +U 2 +D 4 +U 2 +L 3 +D 2 +R 3 +U 3 +D 2 +U 1 +R 5 +L 4 +R 4 +L 4 +D 3 +R 3 +U 2 +R 1 +D 3 +U 3 +D 2 +L 1 +D 2 +U 1 +L 3 +U 5 +L 1 +D 1 +U 5 +D 3 +U 4 +D 4 +L 5 +R 3 +D 4 +R 2 +L 1 +D 1 +L 3 +U 3 +R 1 +L 2 +D 4 +L 5 +U 1 +L 4 +R 4 +U 2 +D 1 +R 5 +D 3 +U 2 +D 3 +U 4 +D 4 +U 1 +L 2 +D 4 +L 3 +U 4 +L 1 +R 1 +L 2 +R 3 +L 2 +U 1 +D 2 +U 5 +R 2 +D 4 +U 1 +D 4 +U 4 +D 5 +L 6 +D 3 +U 2 +D 1 +L 3 +U 4 +R 4 +D 4 +R 3 +L 4 +U 6 +D 1 +U 4 +R 2 +L 1 +D 2 +U 4 +D 5 +U 3 +D 6 +L 1 +U 3 +L 1 +U 1 +D 6 +L 3 +U 6 +L 3 +D 6 +L 1 +D 6 +U 2 +D 5 +U 2 +L 5 +D 3 +U 2 +D 1 +U 6 +L 1 +D 4 +L 6 +R 3 +D 3 +L 3 +D 2 +R 3 +D 5 +R 3 +D 2 +L 1 +R 4 +L 6 +D 4 +R 4 +U 1 +R 6 +D 3 +U 1 +R 1 +D 1 +L 3 +D 1 +R 3 +U 5 +L 5 +D 2 +L 3 +D 2 +L 5 +U 3 +L 1 +U 5 +L 3 +D 4 +U 5 +D 1 +L 6 +D 1 +R 6 +U 4 +R 1 +D 1 +L 2 +U 5 +R 2 +U 6 +L 5 +U 5 +L 6 +U 6 +D 3 +L 1 +R 3 +L 3 +U 4 +R 5 +L 6 +D 5 +L 4 +R 6 +L 6 +R 5 +D 2 +R 4 +L 2 +U 1 +L 3 +U 6 +L 4 +D 6 +U 6 +D 4 +L 2 +R 7 +U 4 +R 1 +L 5 +D 2 +R 3 +U 4 +L 5 +U 7 +L 2 +R 1 +U 4 +L 2 +R 5 +L 6 +D 5 +L 4 +U 5 +L 7 +U 1 +L 5 +U 1 +L 5 +D 3 +U 3 +R 7 +U 7 +R 7 +L 6 +D 4 +U 7 +R 2 +D 3 +R 3 +U 4 +D 4 +U 5 +D 6 +L 5 +R 6 +U 4 +R 6 +D 1 +R 4 +U 1 +D 1 +R 1 +L 4 +U 5 +L 3 +D 5 +L 5 +D 3 +U 2 +R 5 +D 7 +U 7 +D 1 +U 5 +R 6 +U 4 +R 6 +L 7 +U 5 +R 1 +L 1 +U 1 +R 1 +D 7 +L 2 +U 5 +R 4 +U 2 +D 6 +U 6 +L 7 +U 3 +D 2 +L 3 +R 3 +U 2 +L 1 +R 2 +L 5 +D 6 +R 2 +U 6 +L 1 +D 7 +R 4 +D 3 +L 2 +U 5 +R 4 +L 2 +R 5 +D 1 +U 1 +R 6 +D 5 +U 6 +R 2 +U 2 +L 2 +U 6 +L 5 +U 1 +L 1 +D 7 +R 6 +U 5 +D 3 +L 5 +R 3 +D 3 +U 2 +D 7 +U 7 +L 1 +D 5 +L 5 +U 4 +L 5 +D 1 +U 6 +D 5 +L 7 +D 8 +U 8 +R 6 +U 5 +D 8 +U 1 +R 2 +U 6 +D 6 +L 4 +D 2 +R 3 +U 7 +R 4 +U 4 +R 5 +U 6 +R 1 +U 4 +R 1 +U 4 +D 5 +U 3 +L 7 +U 8 +R 1 +D 1 +U 8 +L 8 +U 3 +L 1 +D 4 +R 2 +D 4 +U 2 +L 4 +D 8 +R 7 +D 5 +U 4 +L 5 +U 5 +D 8 +R 8 +U 3 +R 8 +L 4 +U 5 +R 6 +U 7 +D 3 +L 1 +U 5 +D 2 +R 5 +U 4 +D 8 +L 4 +R 1 +D 6 +R 1 +L 6 +D 6 +L 7 +D 4 +L 4 +D 8 +R 8 +D 7 +L 6 +R 1 +D 4 +L 5 +D 2 +U 3 +D 2 +U 6 +R 1 +U 7 +D 7 +R 2 +U 7 +D 5 +L 6 +U 1 +L 8 +U 1 +L 1 +R 9 +D 1 +U 6 +D 5 +L 1 +R 9 +D 2 +L 2 +D 6 +U 6 +D 7 +L 3 +R 5 +L 6 +U 5 +R 1 +U 1 +R 3 +L 9 +U 6 +L 6 +U 2 +D 6 +R 9 +U 4 +D 3 +L 8 +U 5 +D 7 +L 1 +U 7 +R 1 +L 6 +D 8 +L 5 +R 7 +L 2 +R 6 +L 5 +D 3 +L 8 +D 7 +U 8 +R 6 +L 6 +D 7 +U 8 +D 1 +U 2 +D 5 +U 7 +D 8 +L 1 +D 1 +L 1 +U 1 +R 3 +L 5 +R 9 +D 2 +L 8 +R 6 +D 6 +U 1 +R 7 +L 4 +U 4 +D 1 +L 5 +U 2 +R 9 +D 7 +U 8 +L 8 +R 4 +D 1 +L 1 +U 7 +R 8 +L 3 +D 2 +U 8 +R 1 +D 5 +L 1 +R 7 +D 6 +R 2 +U 9 +D 2 +L 5 +U 4 +D 9 +L 7 +D 9 +L 2 +U 9 +D 5 +L 7 +D 6 +U 5 +D 9 +R 3 +U 8 +L 7 +U 1 +L 3 +U 3 +D 1 +L 6 +R 9 +U 2 +L 7 +R 9 +U 5 +R 2 +L 3 +R 4 +D 9 +L 4 +R 9 +U 4 +L 10 +R 9 +D 9 +U 10 +L 2 +R 4 +U 4 +R 9 +D 2 +R 1 +L 4 +R 7 +D 3 +L 6 +U 1 +L 9 +D 6 +U 10 +D 1 +R 10 +D 9 +L 6 +D 10 +U 3 +R 10 +L 8 +U 9 +D 1 +L 1 +D 10 +L 1 +D 5 +L 9 +U 3 +L 4 +U 4 +D 1 +U 3 +R 7 +L 3 +R 3 +L 3 +R 8 +D 9 +R 4 +L 6 +D 7 +R 10 +U 6 +R 2 +D 2 +L 3 +R 9 +D 9 +U 7 +D 2 +R 6 +U 10 +R 8 +U 5 +R 5 +U 10 +R 6 +U 7 +L 7 +U 7 +D 4 +L 9 +U 2 +R 7 +L 8 +R 4 +L 1 +U 9 +R 8 +D 4 +R 5 +D 4 +L 1 +D 10 +R 8 +D 10 +U 9 +D 3 +R 1 +U 10 +R 1 +D 10 +R 1 +L 6 +D 3 +U 8 +R 4 +U 9 +D 8 +U 3 +D 2 +R 3 +L 5 +U 4 +L 3 +R 4 +D 6 +L 9 +R 1 +D 9 +R 2 +D 11 +R 8 +D 5 +R 8 +L 2 +D 2 +L 7 +U 11 +D 10 +L 6 +R 2 +U 9 +L 10 +D 4 +L 5 +R 7 +D 4 +L 1 +U 3 +L 9 +D 7 +R 4 +D 10 +L 5 +U 9 +L 10 +D 5 +L 11 +R 11 +U 8 +R 11 +U 2 +R 7 +L 10 +R 2 +D 8 +L 2 +D 8 +R 11 +L 6 +R 3 +L 2 +U 2 +R 2 +U 3 +D 3 +U 5 +L 8 +U 9 +R 10 +D 1 +U 4 +D 8 +R 3 +D 11 +R 4 +D 4 +R 7 +U 6 +L 11 +U 11 +D 8 +L 5 +R 2 +L 11 +U 7 +D 5 +U 5 +D 9 +L 2 +U 3 +R 10 +U 1 +R 1 +L 11 +D 3 +L 5 +R 2 +L 10 +R 4 +L 1 +U 1 +L 4 +R 5 +U 2 +D 6 +R 11 +U 8 +D 5 +R 5 +D 3 +L 2 +U 7 +D 10 +L 11 +D 10 +U 11 +R 8 +L 2 +D 9 +R 9 +L 11 +R 10 +U 3 +R 4 +L 5 +R 12 +U 5 +D 5 +L 6 +D 11 +R 8 +D 12 +U 6 +L 10 +R 9 +U 1 +L 12 +D 1 +R 8 +U 9 +D 9 +R 6 +D 3 +L 1 +R 3 +L 8 +R 6 +U 12 +L 1 +U 8 +D 7 +U 5 +D 12 +L 4 +R 11 +U 9 +D 12 +R 8 +D 2 +R 7 +U 8 +R 10 +U 3 +R 4 +D 4 +U 9 +R 7 +L 9 +R 7 +U 8 +L 9 +U 6 +R 4 +L 11 +D 11 +R 6 +U 2 +D 1 +R 2 +L 2 +D 11 +R 5 +U 1 +R 1 +D 3 +U 2 +L 3 +U 12 +R 10 +U 12 +R 9 +D 11 +U 9 +L 2 +R 7 +U 5 +R 7 +U 9 +R 3 +D 4 +L 9 +D 4 +R 12 +L 3 +U 12 +L 9 +U 3 +L 11 +R 7 +U 11 +D 2 +L 11 +R 2 +L 12 +U 10 +L 6 +R 8 +D 6 +R 12 +L 7 +R 4 +U 5 +D 10 +R 8 +L 11 +D 4 +U 1 +R 9 +U 12 +D 3 +L 10 +R 11 +U 1 +L 5 +U 11 +L 7 +U 1 +D 13 +U 10 +L 12 +U 9 +D 8 +L 5 +R 4 +L 1 +R 2 +L 7 +D 1 +L 11 +U 9 +R 5 +D 12 +L 4 +U 7 +L 6 +R 7 +U 1 +L 7 +U 2 +D 4 +R 4 +D 7 +L 3 +D 9 +U 3 +D 3 +U 12 +R 2 +L 12 +R 9 +L 8 +D 10 +U 1 +R 11 +L 13 +U 1 +D 3 +U 12 +L 3 +R 7 +L 7 +D 7 +U 7 +D 7 +U 10 +D 1 +L 7 +D 11 +R 3 +D 6 +R 9 +D 5 +U 12 +D 6 +R 8 +D 3 +R 7 +L 6 +D 10 +U 8 +R 9 +D 4 +R 9 +D 2 +U 4 +R 4 +U 7 +L 12 +R 2 +D 10 +U 13 +R 12 +L 12 +D 1 +R 11 +L 2 +U 13 +L 12 +D 7 +L 2 +U 4 +L 4 +D 8 +L 9 +R 3 +D 4 +L 9 +U 9 +L 9 +R 10 +L 4 +R 4 +U 10 +L 11 +D 8 +U 8 +L 13 +R 1 +L 8 +U 13 +L 9 +U 1 +R 13 +U 7 +L 3 +R 8 +L 1 +D 5 +R 7 +U 8 +D 5 +L 12 +R 11 +U 11 +D 10 +U 5 +R 6 +D 11 +R 1 +D 1 +R 9 +L 8 +D 4 +L 11 +U 9 +D 4 +U 3 +D 13 +U 13 +L 6 +D 6 +R 11 +L 9 +U 3 +R 7 +D 9 +R 5 +L 9 +D 14 +R 8 +L 14 +R 6 +L 6 +U 6 +R 7 +L 11 +R 1 +L 6 +D 10 +R 1 +D 14 +R 12 +U 4 +R 2 +L 9 +R 14 +L 3 +D 9 +U 10 +R 11 +L 5 +R 14 +U 3 +D 8 +R 9 +L 13 +U 4 +L 2 +R 4 +D 6 +L 13 +D 4 +R 2 +L 2 +D 10 +L 10 +R 4 +D 2 +R 2 +U 6 +D 7 +U 12 +R 5 +U 13 +L 6 +U 12 +L 3 +D 11 +R 3 +L 7 +U 11 +D 4 +L 13 +U 14 +R 10 +D 12 +U 2 +L 1 +U 12 +D 6 +U 2 +R 12 +L 3 +D 9 +U 9 +R 14 +L 12 +R 5 +L 11 +R 11 +L 5 +R 2 +L 4 +D 8 +U 3 +R 4 +D 12 +R 10 +L 10 +D 10 +U 10 +R 9 +L 14 +R 3 +L 1 +D 1 +R 15 +D 15 +R 12 +L 15 +D 12 +L 10 +R 3 +U 14 +D 4 +U 7 +R 11 +L 10 +U 4 +D 15 +L 3 +U 1 +R 2 +L 15 +U 6 +L 6 +D 3 +L 14 +D 12 +U 1 +L 15 +R 13 +U 7 +D 6 +R 11 +U 7 +L 2 +D 11 +L 1 +R 7 +D 1 +L 4 +R 11 +U 5 +L 4 +R 13 +U 1 +D 4 +L 6 +R 10 +L 15 +U 7 +D 9 +L 10 +R 8 +U 10 +D 8 +L 14 +D 15 +R 14 +U 10 +D 4 +U 6 +D 2 +R 6 +L 6 +D 4 +U 12 +L 3 +R 6 +U 9 +R 3 +U 11 +R 3 +U 15 +D 1 +U 2 +D 3 +U 11 +L 3 +U 2 +L 14 +D 3 +R 9 +D 5 +L 10 +U 7 +L 6 +D 8 +L 8 +U 8 +L 14 +R 9 +L 14 +U 10 +L 8 +U 5 +L 2 +U 4 +D 5 +R 2 +L 1 +U 14 +R 3 +L 4 +U 9 +D 7 +U 14 +D 11 +R 9 +U 5 +R 8 +L 5 +U 3 +D 8 +R 8 +L 11 +R 7 +L 3 +D 16 +L 11 +U 7 +D 6 +R 5 +L 4 +U 9 +L 12 +R 9 +L 10 +D 13 +L 3 +D 13 +U 6 +R 14 +U 2 +L 5 +R 4 +L 15 +U 9 +D 5 +L 6 +R 7 +U 10 +L 3 +D 5 +U 8 +D 12 +L 11 +U 14 +D 12 +U 2 +D 9 +U 11 +D 5 +L 5 +R 10 +L 14 +U 1 +R 8 +D 5 +R 6 +U 6 +L 3 +U 11 +L 8 +R 3 +U 5 +L 1 +R 16 +D 10 +U 12 +R 11 +D 2 +U 4 +D 15 +U 16 +D 2 +L 9 +R 10 +L 3 +U 8 +R 13 +U 11 +D 12 +U 15 +D 1 +L 14 +R 3 +U 13 +D 7 +R 15 +L 3 +R 1 +U 7 +R 6 +D 12 +L 2 +R 13 +D 3 +R 6 +D 10 +U 2 +R 4 +D 12 +L 7 +R 7 +L 8 +R 14 +L 15 +D 8 +L 6 +U 14 +L 16 +U 10 +D 7 +R 6 +D 12 +R 16 +U 11 +R 2 +U 10 +L 1 +R 6 +U 14 +D 13 +U 7 +L 10 +D 4 +L 7 +R 11 +U 17 +D 6 +U 6 +R 6 +U 12 +L 16 +U 5 +R 15 +L 17 +D 13 +U 12 +L 1 +D 7 +L 13 +R 7 +U 2 +L 5 +U 12 +R 10 +U 7 +R 11 +U 17 +D 15 +U 15 +L 10 +D 3 +L 15 +U 8 +R 8 +D 10 +L 9 +U 6 +R 9 +L 1 +U 5 +R 6 +L 11 +D 17 +L 7 +U 3 +D 17 +R 17 +U 8 +L 15 +R 3 +D 2 +U 17 +L 1 +R 13 +U 13 +L 10 +R 2 +D 12 +L 12 +R 13 +L 14 +R 16 +D 12 +R 4 +U 14 +L 15 +R 10 +D 7 +R 12 +U 6 +L 8 +U 7 +R 1 +D 3 +R 3 +L 11 +D 2 +R 15 +U 17 +L 13 +U 17 +D 4 +U 11 +L 17 +D 12 +R 3 +L 12 +U 8 +D 2 +L 14 +U 16 +D 6 +U 7 +L 12 +R 17 +L 7 +D 1 +L 16 +D 16 +R 17 +D 5 +U 16 +D 2 +L 13 +U 9 +D 13 +R 3 +U 1 +D 12 +R 4 +D 11 +L 8 +D 8 +R 13 +D 16 +R 1 +D 10 +R 18 +U 6 +D 4 +U 17 +R 12 +L 14 +U 5 +L 15 +U 18 +R 4 +D 17 +L 4 +R 18 +U 18 +D 4 +L 1 +U 4 +L 2 +U 17 +R 16 +L 7 +U 10 +R 11 +U 9 +R 11 +L 16 +U 3 +R 3 +L 18 +U 8 +R 1 +U 1 +L 5 +U 5 +D 7 +L 1 +R 2 +U 16 +R 17 +D 4 +U 16 +L 12 +R 3 +L 16 +D 5 +U 5 +R 18 +L 7 +U 1 +L 7 +U 16 +R 14 +L 4 +R 13 +L 3 +U 4 +L 11 +U 3 +L 12 +U 16 +L 14 +U 3 +L 9 +R 18 +D 8 +L 15 +R 10 +D 2 +L 13 +D 11 +U 4 +L 13 +D 7 +L 12 +U 7 +D 17 +U 9 +D 5 +L 10 +R 2 +D 7 +L 14 +U 10 +D 17 +L 3 +R 17 +U 16 +D 13 +R 13 +U 14 +R 9 +L 3 +U 14 +L 7 +U 4 +D 1 +R 18 +U 11 +D 9 +U 8 +R 15 +L 5 +D 9 +L 2 +D 18 +U 12 +D 3 +U 14 +D 9 +U 13 +R 16 +U 8 +R 16 +U 4 +L 8 +D 14 +R 15 +D 6 +R 17 +U 6 +D 1 +L 2 +U 9 +D 15 +L 7 +D 3 +R 1 +L 1 +R 17 +D 12 +R 19 +U 4 +R 19 +L 2 +D 17 +U 11 +D 1 +L 9 +R 18 +L 7 +D 13 +U 6 +D 16 +L 6 +R 13 +U 12 +R 1 +L 2 +U 8 +L 16 +R 10 +D 15 +R 5 +L 1 +U 5 +R 18 +U 1 +R 4 +U 14 +R 3 +L 4 +U 13 +D 14 +L 10 +D 7 +U 3 +R 17 +L 19 +R 1 +D 12 +L 9 +U 19 +D 19 +L 5 +R 10 +L 14 +U 15 +D 19 +U 18 +D 14 +R 11 +U 8 +R 2 +U 16 +L 17 +D 6 +L 1 +D 10 +R 18 +U 5 +R 14 +U 10 +D 6 +L 17 +D 6 +U 7 +R 14 +L 1 +U 3 +D 3 +R 18 +D 1 +L 6 +D 18 +L 2 +R 10 +L 9 +R 10 +L 14 +R 15 +U 10 diff --git a/day9/src/main.rs b/day9/src/main.rs new file mode 100644 index 0000000..8f6a2a8 --- /dev/null +++ b/day9/src/main.rs @@ -0,0 +1,103 @@ +use std::collections::HashMap; + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let lines = contents.lines().collect::>(); + + let mut hash: HashMap<(i32, i32), bool> = HashMap::new(); + run(&lines, &mut hash, 1); + let part_1 = hash.keys().len(); + println!("Num visited, part 1: {:?}", part_1); + + let mut hash2: HashMap<(i32, i32), bool> = HashMap::new(); + run(&lines, &mut hash2, 9); + let part_2 = hash2.keys().len(); + println!("Num visited, part 2: {:?}", part_2); +} + +fn run(lines: &Vec<&str>, hash: &mut HashMap<(i32, i32), bool>, num_tails: usize) { + let mut head = (0i32, 0i32); + let mut tails: Vec<(i32, i32)> = vec![(0, 0); num_tails]; + + for line in lines { + let (dir, steps) = line.split_once(" ").unwrap(); + + for _ in 0..steps.parse().unwrap() { + match dir { + "L" => head = (head.0, head.1 - 1), + "R" => head = (head.0, head.1 + 1), + "U" => head = (head.0 - 1, head.1), + "D" => head = (head.0 + 1, head.1), + _ => (), + } + // println!("\nnew head location {:?}", head); + + let mut parent = head; + + for tail_i in 0..num_tails { + let mut tail = tails[tail_i]; + + // find the distance from parent to tail + let v_dist = parent.0 - tail.0; + let h_dist = parent.1 - tail.1; + let mut already_moved_diagonally = false; + + // move vertically + if v_dist.abs() > 1 { + let v_diff = v_dist / v_dist.abs(); + tail = (tail.0 + v_diff, tail.1); + + // do we need to move horizontally? + if parent.1 != tail.1 { + let h_diff = h_dist / h_dist.abs(); + tail = (tail.0, tail.1 + h_diff); + already_moved_diagonally = true; + } + } + + // move horizontally + if h_dist.abs() > 1 && !already_moved_diagonally { + let h_diff = h_dist / h_dist.abs(); + tail = (tail.0, tail.1 + h_diff); + + // do we need to move horizontally? + if parent.0 != tail.0 { + let v_diff = v_dist / v_dist.abs(); + tail = (tail.0 + v_diff, tail.1); + } + } + + // println!("new tail ({:?}) location {:?}", tail_i + 1, tail); + if tail_i == num_tails - 1 { + // for the last tail, record where it has been + hash.insert(tail, true); + } + + tails[tail_i] = tail; + parent = tail; + } + } + + let mut temp = tails.to_owned(); + temp.insert(0, head); + display(temp); + } +} + +fn display(rope: Vec<(i32, i32)>) -> () { + let v_min = rope.iter().map(|x| x.0).min().unwrap(); + let v_max = rope.iter().map(|x| x.0).max().unwrap(); + let h_min = rope.iter().map(|x| x.1).min().unwrap(); + let h_max = rope.iter().map(|x| x.1).max().unwrap(); + + for v in v_min..=v_max { + for h in h_min..=h_max { + match rope.iter().position(|&x| x == (v, h)) { + Some(i) => print!("{i}"), + None => print!("."), + } + } + println!(""); + } + println!(""); +} diff --git a/day9/test.txt b/day9/test.txt new file mode 100644 index 0000000..9874df2 --- /dev/null +++ b/day9/test.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 diff --git a/day9/test2.txt b/day9/test2.txt new file mode 100644 index 0000000..60bd43b --- /dev/null +++ b/day9/test2.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20 From 4b3a9aaa7e62d3309d693aff3b01c1340e0b8901 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Sun, 11 Dec 2022 19:49:32 -0500 Subject: [PATCH 04/10] Day 10. I'm a bit behind. CA trip got in the way --- day10/Cargo.lock | 7 +++ day10/Cargo.toml | 8 +++ day10/data.txt | 144 +++++++++++++++++++++++++++++++++++++++++++++ day10/src/main.rs | 67 +++++++++++++++++++++ day10/test.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 372 insertions(+) create mode 100644 day10/Cargo.lock create mode 100644 day10/Cargo.toml create mode 100644 day10/data.txt create mode 100644 day10/src/main.rs create mode 100644 day10/test.txt diff --git a/day10/Cargo.lock b/day10/Cargo.lock new file mode 100644 index 0000000..5f8ea61 --- /dev/null +++ b/day10/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day10" +version = "0.1.0" diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/day10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day10/data.txt b/day10/data.txt new file mode 100644 index 0000000..3d9acd7 --- /dev/null +++ b/day10/data.txt @@ -0,0 +1,144 @@ +noop +noop +noop +addx 6 +addx -1 +addx 5 +noop +noop +noop +addx 5 +addx -8 +addx 9 +addx 3 +addx 2 +addx 4 +addx 3 +noop +addx 2 +noop +addx 1 +addx 6 +noop +noop +noop +addx -39 +noop +addx 5 +addx 2 +addx -2 +addx 3 +addx 2 +addx 5 +addx 2 +addx 2 +addx 13 +addx -12 +noop +addx 7 +noop +addx 2 +addx 3 +noop +addx -25 +addx 30 +addx -10 +addx 13 +addx -40 +noop +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 3 +addx -1 +addx 7 +noop +noop +addx 5 +addx -1 +addx 6 +noop +noop +noop +noop +addx 9 +noop +addx -1 +noop +addx -39 +addx 2 +addx 33 +addx -29 +addx 1 +noop +addx 4 +noop +noop +noop +addx 3 +addx 2 +noop +addx 3 +noop +noop +addx 7 +addx 2 +addx 3 +addx -2 +noop +addx -30 +noop +addx 40 +addx -2 +addx -38 +noop +noop +noop +addx 5 +addx 5 +addx 2 +addx -9 +addx 5 +addx 7 +addx 2 +addx 5 +addx -18 +addx 28 +addx -7 +addx 2 +addx 5 +addx -28 +addx 34 +addx -3 +noop +addx 3 +addx -38 +addx 10 +addx -3 +addx 29 +addx -28 +addx 2 +noop +noop +noop +addx 5 +noop +addx 3 +addx 2 +addx 7 +noop +addx -2 +addx 5 +addx 2 +noop +addx 1 +addx 5 +noop +noop +addx -25 +noop +noop diff --git a/day10/src/main.rs b/day10/src/main.rs new file mode 100644 index 0000000..23e31e5 --- /dev/null +++ b/day10/src/main.rs @@ -0,0 +1,67 @@ +#[derive(Debug)] +enum Instruction { + Noop, + AddX(i32), +} + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let ops = contents.lines().map(parse).collect::>(); + + let strength = run(&ops); + println!("total signal strength {:?}", strength); +} + +fn parse(line: &str) -> Instruction { + let parts = line.split(" ").collect::>(); + match parts[0] { + "noop" => Instruction::Noop, + _ => Instruction::AddX(parts[1].parse().unwrap()), + } +} + +fn run(ops: &Vec) -> i32 { + let mut x: i32 = 1; + let mut cycle = 1; + let mut to_add = None; + let mut op_idx = 0; + let mut strength = 0; + + while op_idx < ops.len() { + let pixel = cycle % 40; + let diff = (x + 1 - pixel).abs(); + if diff.abs() <= 1 { + print!("#"); + } else { + print!(" "); + } + + match to_add { + None => { + match ops[op_idx] { + Instruction::Noop => (), + Instruction::AddX(v) => { + to_add = Some(v); + } + } + op_idx += 1; + } + Some(v) => { + x += v; + to_add = None; + } + } + + if (cycle + 20) % 40 == 0 { + strength += x * cycle; + } + + if cycle % 40 == 0 { + println!(); + } + + cycle += 1; + } + + strength +} diff --git a/day10/test.txt b/day10/test.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/day10/test.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop From 99681b61d5c9658941ccf300bb7d7893c2756f05 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Tue, 13 Dec 2022 19:54:04 -0500 Subject: [PATCH 05/10] day 11. took me wayyy to long --- day11/Cargo.lock | 7 ++++ day11/Cargo.toml | 8 ++++ day11/data.txt | 55 +++++++++++++++++++++++++++ day11/src/main.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++ day11/test.txt | 27 +++++++++++++ 5 files changed, 194 insertions(+) create mode 100644 day11/Cargo.lock create mode 100644 day11/Cargo.toml create mode 100644 day11/data.txt create mode 100644 day11/src/main.rs create mode 100644 day11/test.txt diff --git a/day11/Cargo.lock b/day11/Cargo.lock new file mode 100644 index 0000000..4ab9be8 --- /dev/null +++ b/day11/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day11" +version = "0.1.0" diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..8f5b9a5 --- /dev/null +++ b/day11/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day11/data.txt b/day11/data.txt new file mode 100644 index 0000000..616cce7 --- /dev/null +++ b/day11/data.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 83, 62, 93 + Operation: new = old * 17 + Test: divisible by 2 + If true: throw to monkey 1 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 90, 55 + Operation: new = old + 1 + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 91, 78, 80, 97, 79, 88 + Operation: new = old + 3 + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 5 + +Monkey 3: + Starting items: 64, 80, 83, 89, 59 + Operation: new = old + 5 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 98, 92, 99, 51 + Operation: new = old * old + Test: divisible by 5 + If true: throw to monkey 0 + If false: throw to monkey 1 + +Monkey 5: + Starting items: 68, 57, 95, 85, 98, 75, 98, 75 + Operation: new = old + 2 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 0 + +Monkey 6: + Starting items: 74 + Operation: new = old + 4 + Test: divisible by 7 + If true: throw to monkey 3 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 68, 64, 60, 68, 87, 80, 82 + Operation: new = old * 19 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 5 diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..de97874 --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,97 @@ +use std::collections::VecDeque; + +#[derive(Debug, Clone)] +struct Monkey { + id: usize, + items: VecDeque, + operation: String, + divisor: u64, + true_monkey: usize, + false_monkey: usize, + inspected: usize, +} + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let mut monkeys = parse(contents.lines().collect()); + let mut monkeys2 = monkeys.clone(); + + let mut items = monkeys + .clone() + .into_iter() + .map(|m| m.items) + .collect::>>(); + let mut items2 = items.clone(); + + // Part 1 + run(&mut monkeys, &mut items, 3, 20); + + // Part 2 + run(&mut monkeys2, &mut items2, 1, 10000); +} + +fn run(monkeys: &mut Vec, items: &mut Vec>, divisor: u64, rounds: usize) -> () { + let total_prod = monkeys.iter().fold(1, |acc, m| m.divisor * acc); + for _ in 1..=rounds { + for m in 0..monkeys.len() { + let monkey = &mut monkeys[m]; + for _ in 0..items[m].len() { + let item = items[m].pop_front().unwrap(); + let new_item = operate(monkey.operation.as_str(), &item) % total_prod; + let new_item = new_item / divisor; + monkey.inspected += 1; + if new_item % monkey.divisor == 0 { + items[monkey.true_monkey].push_back(new_item); + } else { + items[monkey.false_monkey].push_back(new_item); + } + } + } + } + + let mut active = monkeys.iter().map(|m| m.inspected).collect::>(); + active.sort(); + active.reverse(); + let score = active.drain(0..2).fold(1, |acc, x| acc * x); + println!("\nactive: {:?}", active); + println!("score: {score}"); +} + +fn parse(lines: Vec<&str>) -> Vec { + lines + .chunks(7) + .into_iter() + .map(|lines| { + let items = lines[1] + .split(" ") + .skip(4) + .map(|i| i.get(0..2).unwrap().parse().unwrap()) + .collect::>(); + let operation = lines[2].split("=").collect::>()[1] + .trim() + .to_string(); + let divisor = lines[3].split(" ").last().unwrap().parse().unwrap(); + let true_monkey = lines[4].chars().last().unwrap().to_digit(10).unwrap() as usize; + let false_monkey = lines[5].chars().last().unwrap().to_digit(10).unwrap() as usize; + + Monkey { + id: lines[0].chars().nth(7).unwrap().to_digit(10).unwrap() as usize, + items, + operation, + divisor, + true_monkey, + false_monkey, + inspected: 0, + } + }) + .collect() +} + +fn operate(eq: &str, item: &u64) -> u64 { + match eq.split(" ").collect::>().as_slice() { + ["old", "*", "old"] => item * item, + ["old", "*", x] => item * x.parse::().unwrap(), + ["old", "+", x] => item + x.parse::().unwrap(), + _ => panic!("Invalid operation"), + } +} diff --git a/day11/test.txt b/day11/test.txt new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/day11/test.txt @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 From ef825522fde8a10d2c66cfcb8e27cb155f7085ba Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 14 Dec 2022 00:32:12 -0500 Subject: [PATCH 06/10] day 12. BETTER --- day12/Cargo.lock | 7 +++ day12/Cargo.toml | 8 ++++ day12/data.txt | 41 +++++++++++++++++ day12/src/main.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++++ day12/test.txt | 5 +++ 5 files changed, 173 insertions(+) create mode 100644 day12/Cargo.lock create mode 100644 day12/Cargo.toml create mode 100644 day12/data.txt create mode 100644 day12/src/main.rs create mode 100644 day12/test.txt diff --git a/day12/Cargo.lock b/day12/Cargo.lock new file mode 100644 index 0000000..5e9bbe3 --- /dev/null +++ b/day12/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day12" +version = "0.1.0" diff --git a/day12/Cargo.toml b/day12/Cargo.toml new file mode 100644 index 0000000..ba13763 --- /dev/null +++ b/day12/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day12" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day12/data.txt b/day12/data.txt new file mode 100644 index 0000000..7fc8957 --- /dev/null +++ b/day12/data.txt @@ -0,0 +1,41 @@ +abcccccccccccccccccccccccccccccccccccccccccccccccccccaaaaacccccccccccccaaaaaaaccccccccccccccccaaaaaaccccccccccccccccccccccccccaaaaacccaaaccccccccccccccccccccccccccccccaaaaa +abcccccccccaaaccccccccccccaaaccccccccccccccccccccccccaaaaaacccccccccccccaaaaaaaaaaaccaaaaccccaaaaaaacccccccccccccccccccccccccccaaaaaacaaaaccccccccccccccccccccccccccccccaaaa +abcccccccccaaaaaacccccccccaaaacccccccccccccccccccccccaaaaaaccccccccccccaaaaaaaaaaaccaaaaacccaaaaaacccccccccaaacccccccccccccccaaaaaaaacaaaaccccccccccccccccacccccccccccccaaaa +abcccccccccaaaaaacccccccccaaaaccccaacccccccccccccccccaaaaaaccccccccccaaaaaaaaaaaaaacaaaaaaccaacaaaccaacccaaaaaaccccccccccccccaaaaaaaacaaaccccccccccaccccaaaccccccccccccaaaaa +abcccccccaaaaaaaccccccccccaaaacccaaaaccccccccccccaaccccaaacccccccaaacaaaaaaaaaccccccaaaaaacccccaaaccaacccaaaaaacccccccccccccccccaaccccccccccccccccaaacccaaaccccccccccccaaaca +abcccccccaaaaaaacccccccaaacccccccaaaacccccccaaccaaaccccccccccccaaaaacaaaaaaaaaccccccaaaaaccccccccaaaaaaaacaaaaaccaacaaccccccccccaaccccccccccccccccaaaaaaaaaccccccccccccccccc +abcccccccccaaaaaaccaaccaaacccccccaaaacccccccaaaaaaaccccccccccccaaaaaaccccaaaaaacccccccaaaccccccccaaaaaaaaaaaaacccaaaaacccccccaaaccccccccccccccccccaaaaaaaackcccccccccccccccc +abcccccccccaaaaaaccaaaaaaaccccccccccccccccccaaaaaacccccccccccccaaaaaaccccaaaaaaccccccccccccccccccccaaaaccaaaaaccccaaaaaccccccaaaaaccccaaaaaccccccccaaaajjkkkkkccccccaacccccc +abcccccccccaaccccccaaaaaaccccccccccccccccccccaaaaaaaaccccccccccaaaaacccaaaacaaccccccccccccccccccccaaaaaccccccccccaaaaaacccccaaaaaaccccaaaaaccciijjjjjjjjjkkkkkkccaaaaaaccccc +abcaaaccccccccccccccaaaaaaaaccccccccccccccccaaaaaaaaacccccccccccaaaacccaaaccaaccccccccccccccccccccaacaaacccccccccaaaacccccccaaaaaacccaaaaaaciiiijjjjjjjjjoopkkkkcaaaaacccccc +abaaaacccccccccccccaaaaaaaaaccccccccccccccccaaaaaaaacccccccccccccccccccaaaaaaaccccaccaccccccccccccacccaacccccccccccaaccccccccaaaaacccaaaaaaiiiiiijjjjjjoooppppkkcaaaaaaacccc +abaaaaaccccccccccccaaaaaaaaccccccccccccccccaaaaaaaccccccccccccccccccccccaaaaaaccccaaaacccccccccccccccccccccccccccccccccccccccaaaaccccaaaaaiiiinnnoooooooooppppkkkaaaaaaacccc +abaaaaacccccccccccaaaaaaaccccccccccccccccccccccaaacccccccccccccccccccaaaaaaaacccccaaaaccccccccccccaaccaacccccaccccacccccccccccccccccccaaaciiinnnnoooooooouupppkkkaaaaaaacccc +abaaaaccccccccccccccccaaaccccccccccccccccccccccaaacccccccaaccccccccccaaaaaaaaacccaaaaaacccccccccccaaaaaaccccaaaaaaaacccccccccccccaaccccccciiinnnntttooouuuuupppiiacaaacccccc +abaaaacccccccccccccccccaaccccccccccccccccccccccccccccccaaaacacccccccccaaaaaaaacccaaaaaacccccccccccaaaaaccccccaaaaaacccccccccccaaaaaacccccciinnnttttuuuuuuuuuuppiiccaaacccccc +abcccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaacccccccccccaaaaaaaccccaacccccaaccccccaaaaaacccccaaaaaacccccccccccaaaaaccccccciinnntttttuuuuxyuuuppiiicccccccccc +abccccccccccccccccccccccccccccccccccccaaacccccccaaccccccaaaaccccccccccccaaccccccccccccccccaacaaacaaaaaaaacccaaaaaaaaccccccccccaaaaaaaccccchinnnttxxxxuuxyyyuuppiiiiccccccccc +abccccccccccccccccccccccccccccccccccccaaaaaaccccaaacccccaaaaccccccccccccaaccccccccccccccccaaaaaccaaaaaaaaccaaaaaaaaaaccccccccaaaaaaaaccccchhhnnttxxxxxxxyyuvppppiiiccccccccc +abccccccccccccccccccccccccccccccccccaaaaaaaaaaccaaaaaaacacaacccccccccccccccccaaaccccccccaaaaaaccccccaacccccaaaaaaaaaaccccccccaaaaaaaaccccchhhnntttxxxxxxyyvvvppqqiiicccccccc +abccccccccccccccccccccccccccccaacaccaaaaaaaaaaaaaaaaaaacccccccccccccccccccccaaaaaaccccccaaaaaaacccccaacccccaccaaaaacacccccccccacaaaccccccchhhnnnttxxxxxyyyvvvvqqqqiiiccccccc +SbccccccccccccccccccccccccccccaaaaccaaaaaaacaaaaaaaaaaccccccccccccccccccccccaaaaaaccccccccaaaaaaccccccccccccccaaaaccccccccccccccaaacccccchhhmmmtttxxxEzzyyyyvvvqqqqiiccccccc +abcccccccccccccccccccccccccccaaaaaccccaaaaaccaaaaaaaaacccccccccccccccaaaaaccaaaaaaccccccccaaccaacccccccccccccccaacccccccaaaaccccccccccccchhhmmmtttxxxyyyyyyyyvvvqqqjjjcccccc +abcccaaacccccccccccccccccccccaaaaaacccaacaaaaaaaaaaaaacccccccccccccccaaaaacccaaaaaccccccccaacccccccccccccccccccccccccccaaaaacccccccccccchhhmmmttsxxyyyyyyyyvvvvvqqqjjjcccccc +abcccaaaaaacccaacaaccccccccccacaaaacccaacccaaaaaaaaaaaacccccccccccccaaaaaacccaacaaccccccccccccccccccccccccccccccccaacccaaaaaaccccccccccchhhmmmssxxwwwwyyywvvvvvqqqjjjjcccccc +abccaaaaaaacccaaaaaccccccccccccaaccccccccccaaaaaaaccaaccccccccccccccaaaaaacccccccccccccccccccccccccccccccccccaaccaaacccaaaaaacccccccaaachhhmmssswwwwwwyyywvvqqqqqqjjjccccccc +abcaaaaaaacccccaaaaacccccccccccccccccccccccccccaaaccccccccccccccccccaaaaaacccccccccccaaccccaaccccccccccccccccaaacaaacccaaaaaacccccccaaacgggmmsssswwwswwyywwrrqqqjjjjcccccccc +abcaaaaaaaccccaaaaaacccccccccccccccccccccccaaccaaaccccccccccccccccccccaaccccccccccccaaccccaaaacccccccccccccccaaaaaaccccccaacccccccaacaaagggmmmssssssswwwwwwrrqjjjjjddccccccc +abcccaaaaaacccaaaacccccccccccccccccccccccccaaacaaccccccccccccccccccccccccccccccccaaaaacaacaaaaccccccccccccccccaaaaaaaaccccccccccccaaaaaagggmmmmssssssswwwwrrrkjjjjddddcccccc +abcccaaaaaacccccaaccccccccccccccccccccccccccaaaaaccccccccccccccccccccccccccccccccaaaaaaaacaaaacaaccccccaaccaaaaaaaaaaacccccccccccccaaaaaggggmmmmllllsrrwwwrrkkkjdddddaaacccc +abcccaaaccccccccccccaacccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccaaaaacccccccaaaaaaccaaaaaaaaaaaaaacccccccccccccccaaaaaggggmmllllllrrrrrrrkkkdddddaaaccccc +abccccaaaaaaccccccccaacaaaccccccccacccaaccaaaaaaaaccccccccccaaaccccccaacccccccccccaaaaaccccccccaaaaacccaaaaaaaaaaaaccccccccccccccccaaacaacggggggflllllrrrrrrkkddddaaaaaccccc +abccccaaaaacccccccccaaaaacccccccccaaaaaaccaaaaaaaaccccccccccaaacacccaaaaccccccccccaacaaacccccaaaaaaccccaaaaaaaccaaacccccccccccccccccaacccccggggffffllllrrrrkkkdddaaaaaaacccc +abccaaaaaaacccccccaaaaaaccccccccccaaaaaccccccaacccccaaccccaacaaaaaccaaaacccccccaaccccaaccccccaaaaaaaccaaaaaaaaccaaaccccccccccccccccccaaaccccccgffffflllkkkkkkeedaaaaaaaacccc +abccaaaaaaacccccccaaaaaaacccccccccaaaaaacccccaacccccaaacccaaaaaaaaccaaaacccaaaaacccccccccccccccaaaaaacaaaaaaaacccccccccccccccccccccccaaaacaacccccffffllkkkkkeeedccaaaaaacccc +abccccaaaaaaccccccccaaaaaacccccccaaaaaaaacccccaaccccaaaaaaaaaaaaccccccccccaaaaaaaacccccccccccccaaccaaccccaaaccccccaacccccccccccccccccaaaaaaaccccccfffffkkkkeeeecccaacccccccc +abccccaaccaaccccccccaaccaacccccccaaaaaaaacccccaaccaaaaaaaaccaaaaacccccccccaaaaaaaaccccccccccaacaaccccccccaaccccaacaaaccccaacccccccccccaaaaaaccccccaafffeeeeeeecccccccccccccc +abccccaaccccccccccccaaccccccccccccccaacccccaaaaaaaaaaaaaaacaaacaaaaaaacaccaaaaaaacccccccaaacaacccccccccccccccccaaaaaccccaaaacccccccaaaaaaaaccccccaaaaffeeeeeeeccccccccccccca +abacccccccccccccaaacccccccccccccccccaacccccaaaaaaaaaaaaaacccaaccaaaaaaaaaaaaaccaaacccccccaaaaaccccccccccccccccccaaaaaaccaaaacccccccaaaaaaaaaccccccaaacceeeeeccccccccccccccaa +abaaccccccccccaaaaaacccccccccccccccccccccccccaaaacccaaaaaaccccccaaaaaaaaaaaaaaaaaaaccccccaaaaaaacccaaaccccccccaaaaaaaaccaaaaccccccccaaaaaaaaccccccccccccaaacccccccccccaaacaa +abaaccccccccccaaaaaacccccccccccccccccccccccccaaaaacaaaaaaaccccccaaaaaaaaaaaaaaaaaaccccccaaaaaaaaccccaaaaccccccaaaaacaaccccccccccccccccaaaaaaacccccccccccacacccccccccccaaaaaa +abacccccccccccaaaaaaccccccccccccccccccccccccaaaaaacaaaccaaccccccaaaaaaccaaaaaaaaccccccccaaaaaaaaccaaaaaacccccccccaaaccccccccccccccccccaacccccccccccccccccccccccccccccccaaaaa diff --git a/day12/src/main.rs b/day12/src/main.rs new file mode 100644 index 0000000..e1f6181 --- /dev/null +++ b/day12/src/main.rs @@ -0,0 +1,112 @@ +use std::collections::HashMap; +use std::collections::HashSet; +use std::collections::VecDeque; + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let map = contents + .lines() + .map(|x| x.chars().collect::>()) + .collect::>>(); + + let start = find(&map, 'S').unwrap(); + let mut visited: HashSet<(usize, usize)> = HashSet::new(); + + // Part 1 + let distance = explore(&map, start, &mut visited); + println!("Part 1 distance: {distance}"); + + // Part 2 + let distance = find_all(&map, 'a').into_iter().map(|s| { + let mut v: HashSet<(usize, usize)> = HashSet::new(); + explore(&map, s, &mut v) + }).filter(|&d| d != 0).reduce(usize::min).unwrap(); + println!("Part 2 distance {:?}", distance); +} + +fn explore( + map: &Vec>, + start: (usize, usize), + visited: &mut HashSet<(usize, usize)>, +) -> usize { + let mut queue = VecDeque::new(); + let mut steps: HashMap<(usize, usize), usize> = HashMap::new(); + + visited.insert(start); + queue.push_back(start); + steps.insert(start, 0); + + while queue.len() > 0 { + let coord = queue.pop_front().unwrap(); + let mut val = map[coord.0][coord.1]; + if val == 'S' { + val = 'a'; + } + + for n in neighbors(&coord, &map) { + let n_val = map[n.0][n.1]; + let n_val = if n_val == 'E' { 'z' } else { n_val }; + let can_travel = (val <= n_val && n_val as u8 - val as u8 <= 1) || val as u8 > n_val as u8; + if !visited.contains(&n) && can_travel { + visited.insert(n); + queue.push_back(n); + let distance = steps.get(&coord).unwrap() + 1; + steps.insert(n, distance); + + if map[n.0][n.1] == 'E' { + queue.clear(); + return distance; + } + } + } + } + + 0 +} + +fn neighbors(coord: &(usize, usize), map: &Vec>) -> Vec<(usize, usize)> { + let mut coords = vec![]; + + if coord.0 > 0 { + coords.push((coord.0 - 1, coord.1)); + } + if coord.1 > 0 { + coords.push((coord.0, coord.1 - 1)); + } + if coord.0 < map.len() - 1 { + coords.push((coord.0 + 1, coord.1)); + } + if coord.1 < map[0].len() - 1 { + coords.push((coord.0, coord.1 + 1)); + } + + coords +} + +fn find(map: &Vec>, val: char) -> Option<(usize, usize)> { + for i in 0..map.len() { + let line = &map[i]; + for j in 0..line.len() { + if map[i][j] == val { + return Some((i, j)); + } + } + } + + None +} + +fn find_all(map: &Vec>, val: char) -> Vec<(usize, usize)> { + let mut coords = vec![]; + + for i in 0..map.len() { + let line = &map[i]; + for j in 0..line.len() { + if map[i][j] == val { + coords.push((i, j)); + } + } + } + + coords +} diff --git a/day12/test.txt b/day12/test.txt new file mode 100644 index 0000000..86e9cac --- /dev/null +++ b/day12/test.txt @@ -0,0 +1,5 @@ +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi From 86c696e61baf9e9a4a10fb2125d71fb511344f40 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 14 Dec 2022 22:12:01 -0500 Subject: [PATCH 07/10] Day 13. Got to try serde! fun. --- day13/Cargo.lock | 89 +++++++++ day13/Cargo.toml | 10 ++ day13/data.txt | 449 ++++++++++++++++++++++++++++++++++++++++++++++ day13/src/main.rs | 73 ++++++++ day13/test.txt | 23 +++ 5 files changed, 644 insertions(+) create mode 100644 day13/Cargo.lock create mode 100644 day13/Cargo.toml create mode 100644 day13/data.txt create mode 100644 day13/src/main.rs create mode 100644 day13/test.txt diff --git a/day13/Cargo.lock b/day13/Cargo.lock new file mode 100644 index 0000000..932f6cc --- /dev/null +++ b/day13/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day13" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e326c9ec8042f1b5da33252c8a37e9ffbd2c9bef0155215b6e6c80c790e05f91" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42a3df25b0713732468deadad63ab9da1f1fd75a48a15024b50363f128db627e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" diff --git a/day13/Cargo.toml b/day13/Cargo.toml new file mode 100644 index 0000000..5573772 --- /dev/null +++ b/day13/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day13" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0.150", features = ["derive"] } +serde_json = "1.0.89" diff --git a/day13/data.txt b/day13/data.txt new file mode 100644 index 0000000..5779d4f --- /dev/null +++ b/day13/data.txt @@ -0,0 +1,449 @@ +[[6,[[0],[6,7],[4,10,8,4,1]],1,[[2,6],0,[4,6,2,3],9]]] +[[10,[7],6]] + +[[[2,5,[0,2,6,4],[6],5],[1,[1,4,10]]],[7,[[],6,[9],[0,5,8,8,1],8]]] +[[2,3],[8,10],[3],[[10,[1,3,8,0,7],2,6,4],[],[1,[9,1,4],[1,4,5,10],[]],[5,[],[7],2],[3,7,[10,3],7,[]]]] + +[[[4,[0,8,2]],3,[[],[0,1,9,3,5],6],[]],[],[0,2,[[5,5,8],[8,6,5,10]],10]] +[[[4,[9],[9,8,10,1,0]]],[7],[8,[0,[8,9,5,3],3,[8,4],4],0,[[]]]] + +[[[],[8,5],9,10],[7,6,10,1],[[7,[3,5,0,1],2,0,[2,0,3,7]],3,10,2,[8]],[[5,[8,2,10],[5,8,2,8,7]],7,10,[],[[3,10,4,8,4],4,9,10,2]]] +[[[[0,2,8,5,3],4],[[5,4,10],[],4,[3,1,4]],[],10,10],[[],[],2,10],[6,1,[6,[2,6,4,6,4],[6]]],[[],0],[]] + +[[[],5],[2]] +[[7,4,6,[4],[3,[7],[3,1,8,9,8]]],[7],[[]],[0,[[1,3,1,10],8,8,7]],[3,[8,[6,3,1]]]] + +[[[],6],[[],10,4,[5,8],[5,5,[6,4,2,6,3],10]]] +[[],[[6,5],8,5,[2,[2,2],[4,8,1],[]]]] + +[[6,8,[[6,4,9,2,3]]],[4,[[6,1,4,7,4],[1,10,3]],4,1],[],[5,[[9,5,2],[0,5,4],[4],[8,5,7,8],3]],[]] +[[10,[]],[[[0,2]]],[5,1,1,[],1]] + +[[9,7,1]] +[[8,3,[[2],2,[6]],4],[[9,[9,3],[10,9]],[[10,9,0,6,8],[10,5,7,7,5],[8,10],5],3],[8],[[[9],[1,3,7,4,3],6,[],6],2,8],[[6,[],8,5],0,[[2,1],7],3]] + +[[3],[],[],[5,[9,[8,5,2],[3,3,6,9],[]],[[9,2,1],[],2]]] +[[[10,[7,0,5],4,4,6],[4,9],[],[[4,1,7,7],9,8,2],[2,[],9,[8,0,1,0,5]]],[],[[],5,[1,3,[5],[2]],10]] + +[[[[],8],3],[8,[[4,5,6,0],2,[3,3,4,6],[7,2,4]],[2,[8],[8,5],[4,6,4,5,8],[6,9,6,6,6]]],[[1,6,3,6,9]]] +[[],[[10,[],[9,3,7,8,3],[3,5,1,9,7]],[1,[2,4],[10,8,2],4],9],[],[[2,6,8,[9,3,8,5,1],0],[1],2]] + +[[[[8,6,1],[8],[2,8,7,4,0],0,8],[[],[],8,[4,4,3]]],[[2,2],[8]],[3,[[3,7,10,0,10]]]] +[[9,[[2],[]],0],[],[5,[[5]],[[6,9,3],[]],1,5],[6,[8,5,9,[7,7]],[],3],[]] + +[[7,10,2,8,[[4,9,0],[0,2],0,[6,1,3,5]]],[[[5]],[[4,9],9,[8],[2],[7,10,2]],9,2],[7,6,[[3],1,0,[7,8,0,2]],8],[0,6],[4,9]] +[[10,5,[],[[4,5,6,2,3],3,2],[3,[2],[2],3,[9,1]]]] + +[[1,8,3,[[5,9],4,9,[8],[2,3,6,4]]],[8,6,8,4],[[3,0,[8,3]],[4,3,0],8,[2,6,10]],[10,7,1,10,3],[2,7,[],[]]] +[[[[8]],6],[[[8,10,8],7,8],[]],[[9,5,[10,3,2,4],8,[3,0,1,3,4]],[[10,6,0],2,7]],[[[],[5,4,6,6,5]],[],[[0,1,0,5,10],7,[],5,[4,10,4,7]],[]]] + +[[[[2,4,3,1,6],5,[3,10,5,4,9],[3,4,8,7,6],[]],8,[8,1,4]],[[[1,9,6],0,7,6],3,10,[[3,8,9,5,6],[9,9,5,9],[2],8],8],[0],[10]] +[[[10,0,10],10,7,10,[7,4,6,[]]],[4],[5,0,4,[]],[]] + +[[[4,[7,2,8,4,1],[10,7],4],5,[]],[5,3,4],[[],[0,4,3,[4,0,8]]],[]] +[[4,[]],[[[10,4],[1,6,0,3]],[[1,6,1],5,[],1,4],[[4,3,8,0,3],0],10],[[9,[7,0,0,10,6],[3,8,7],2,[1,2,6,0]],[0,4,3,[1,3,2,8],[7,0]],9],[],[]] + +[[6,8,[[3],8,7,10]],[[[0],10,[5,8,4],9,[1,3]],[3,6,[3,4,0,9]],1,[0,4,[0,7,8],3,6]],[5,[],[[9],[2,0,10,8],5],7,0],[[9,6,[6,6],10],[],2,[[2,1,9],[]]]] +[[2,2,5],[[0,[1]],3,4,[[5,6,2],8,[1,5,6],[10,3,0,3,5],9],[[7,10,6,3,7],[9,10,0,9,4]]],[[[9,9,8,7,3]],1],[[],10,6],[[7,[2],[7],0,6],[8,[8],4,9],4,8]] + +[[5,[1,[0,10,9,8],6,3,7],[[5,9,1],9],[10]],[3,[9]],[10,5,[10,2,6,[],[4,3,2,7]],4]] +[[2,[[3,8,3,0,5],[9,2]],[[6,3,9]],[1,8,10,[],4],5],[[6,[4,4,2,10,9],[3]]],[0],[10,[2,1,[],[3]],8],[10,3]] + +[[[10],[],1,7,[[],9,[5],[9,4,8,0,4]]]] +[[],[[[2,2,5,1,4]],[7,10,[2,5],1],[],0,3],[[],[6,8,3,2],5,9]] + +[[4,[4,[],[9,2,10,10],10],[3,[],[]],[[5],[9,9,8],4,[2,5,6]],[0,[0,6,0]]],[],[8,[5],1],[[10,2,9],[],9]] +[[2,[4],[[]],[]],[[8,8,[],4],10,9],[[[4,2,10],5],5,[3,[],[8],[2,9,6],[3,6]]]] + +[[],[],[8,[8,0],10,[10,[10,6,4]],0],[10,1,[1]]] +[[[],[[],[]],[],[]],[[],0,10]] + +[[],[],[]] +[[5],[0,[6,0,[4,3,1],4],7],[10,[[]],4,[[0,1],4,[5,4,9,2],0]],[10,4,5,8,10]] + +[[[[5,3,7,8],[],[1,10,6]],1,[],[6,[3,7,3,7],10,5,8],0],[[7,[],6,[1,4,8,2,2]],0,6]] +[[[[]],[],9,7]] + +[[[9,5]],[[5,9]],[]] +[[[9,1],[],[]],[2,0,[6,9],9],[7,[[],[6,8,6],[7],2],[[]],[10,[3,2],[3],[9,2],10]]] + +[[],[0,[[]],7,4,[[],8,[10],[]]],[[1,[6,6,0],[2,2,8],[3,3,8,8],[1,5,9,2,6]],9,1],[]] +[[],[10,[[],9],[[0,8,9,10],9,[8,2,5]]],[[6],[[],10,2,[6,10,1],5],[4],[3,[2,4,6]],[4,[],[3,3,0],[7]]],[3,10,8]] + +[[[7,[10,7,7,0],0,5,[6]],[]],[[0],[2,7,9,5],4,[2,[4,2,4],[4,4,0,1]],[7,[3,0,9,9,2]]],[[3,7],[5,[6,3],9,[]]]] +[[],[3,1,5,[[],[4,3,5,10],5,[1,8,4,5],[]],[6,[7]]],[7,[[4,9,5,3,10],7]]] + +[[2],[],[[[5,6,4,7,1],[],4,5,[8,10]],5],[2,7],[6,[],[[9]],2,[[5],[5,6,9],1,[5,0,0],[6,8,7,5]]]] +[[],[[],5,[2,[0],[6,6,2,5,9],[0],[4]],9,9],[2],[8]] + +[[9,10,[9,[0,5,9,8],4],[[5],1,[3,6,10],[3,8,4]]]] +[[[0],[10],1,[[0],8,[]],4]] + +[[[4,5,[6,8,6,9,2],[1,0,6,2,9]]],[[[10]]],[7,0,[[4,8,7,9],[7,2,4],8]],[8,3,[],10,7]] +[[[8,2],0],[1,[]],[[0,1,[2],6],[[8,4,8,0,3],[],1],10]] + +[[[7],[[3,0,10,9,8],[7,0,7,3],8,0,0],[[1,0,6,8,7],[8,3]],[[5]]],[[],[[9],[8],10,[10,4,7,5],[6,1]],1],[[[],[9,1,6,3],[5,8,2,7]]],[[[8],8],[[0,2,6],[4,0]],[[9,8],[3]],2,[[0,8],[10,6,4,10],4,5]],[[[8],5,3,[2]]]] +[[[],[9,[],[3,2,4]],6,[3,[5],[2,2,8,6,10],5,[]],4],[[[8,7],2,9,[1,0,2,7,4],9]],[]] + +[[],[[],[]],[]] +[[],[[]],[[],4],[3,[],[[4,8,8,6,6],[3,2],8]]] + +[[[0,4,0,[2,7,7],[0]]]] +[[[6,[7,1],[5,10],[],[0]],[10,9,[5],8,[4,2,2,7]],[3]],[9],[6,7,9,[3],3],[0,10],[[],1]] + +[[6],[[[],[1],9,[0,2,2,7],6],8]] +[[7,3,[[],4,[],8,[10,4,3,5]],[1,[9,3,4,6,9],6,[]]]] + +[[],[[[],[],7,[0],0],4,8],[4,10,2,2],[10,5,3]] +[[8,5],[2,10,0],[[[7,1,0,3,4],[9,2,4,2]],[[9,1,6,7,7],[5,2,8,7,3],[8,1,5,1,7],[],[]],[[10,1,10],[5],1]],[3,0,0,[],6]] + +[[2,9,8,[],[]],[[[3,1,9,4],[6,10,4],1,9,10],[7,2,4,[10,8],4],0],[[],[[3]],[[],2,[5,9,2,3,3],[6,8,2]]],[[10,[5,1,3,5],10,9],0,5,10,[[],[0],5,3,3]],[0,[8,[4,9,2]],[[5,1,1,2],[9,4,3,10],6,[9,5],6]]] +[[9,[2,[1],4]],[],[9,[8],0],[10,[[6,6,10,1],9,10,4],3,[[1]]]] + +[[2,2,7],[[],10,8,[4,10,5,[3,5,1],5],8]] +[[],[],[10,5,0,[[8],9,[10,2,9,8],[8,2,4],[8,1,6]]],[9]] + +[[[[10,7,2],[5,9,5],[3,5,3]],[[9,8,7,2],4],0],[[8,1],[[10,10,7],6,5,9,6],0],[[9,[9,3,0,3],5],7,[[]]],[[1,[7,10,8]],2,9,[[4,1,4,1]],8]] +[[[]]] + +[[2,[],[10]],[]] +[[[[1,1,4,2,9],1]],[10],[]] + +[[0,5,[4,9]],[[],[[8,0,7],10,5],[],[[4,1]],[9,[9,6,3,7],[4,2,8,4,2],0,9]],[],[],[2]] +[[7],[2,[9,2,5,0],[3,[4,1],[4,8,3,8],[0,10],5]],[3,[10,[4,2,2,2],[0,3],[7,2,9,2,9],1],[[1,3],[10,6],6,8]],[],[1,9]] + +[[5],[[[10,1,1,1],[9,9,0],[6,9]],3,8,6],[7,3,5],[[[0,0],[5,6,10,8,8],[10]],[1,10,2,0]],[]] +[[[[2,10,7],[1]],7,[],[[0,4,8,5]],8],[[[9,8,2,1],[7,10,8,9],3,[]],[],[1],[0,[5,8,7]],[[],[1,9]]]] + +[[5,10,[[9,7,4],4,[4,10,5],[5,1,0,4,5],[8]]],[[7,[0,9,4,8,7],[1,8,4]],10,[],[9,[7,7,2,5],[8,9],[10,6,4,4],[9,2,6,3]],[[3],4,8]],[],[]] +[[5],[[4,8,4,[5,3,0,0,8]]],[8,[[7,8,5,8,8]],7],[9],[[3,[4,6],[],[6,6,1,8,8]],9,[],0,3]] + +[[[2,0,8,[2,9,5,9]],[[],[]]],[6,4,10],[[8,4,5,[10],6],7]] +[[1,10,[[3,4,1],[],10,[6,4,7]]]] + +[[[[2,10,2],[5,8],[],[],[3]],[[3,8,3,5,2]]],[[7,[2,2,5,5]],[6]],[[[10,10,8,2],[9,7,2,1]],[2,9],4],[0,[]],[]] +[[10,0],[],[8,10,7,[]],[[[10],[6,7],2],5,7]] + +[[],[[6,[3],[8,6,7,3]],[3],[[5,10,8,3],0,[2,6,9,4,6],[1,5,6],0],10,[]],[[6,6,[],10],8]] +[[9],[8],[0],[6,4,[[],[4,4,2,4],[3,2,9]],[5,[8,2,5,9,1]]]] + +[[[[6,5],5,1,3],5,6,5],[1,[]],[[],10,4],[[0,[4,9,10,10],[1,8],2],2,0,5,[[4,3,8],7,[7,4,1,9,2],6,[6,7,10,0]]],[0,1,6]] +[[],[4,[7,1,2,0,7],0,2,8],[],[[],7,[6,10,[0,7,2],8,3]],[]] + +[[0,[[9,8,2,9],[6,5],3,1]],[[[6,4,3],[3,10,10,10,3],[10,1,6]],[5],8,1],[6,6,[[8,4,10]],7]] +[[[],2,[],[[0,1,5,0],4]]] + +[[],[[],4,1]] +[[[5,[5],5,9],6,1,[8,5],6]] + +[[1],[[10]],[1,[0,9]],[],[[[],7,[3,3,6]],[4,[9,6,2,4],[9,3],5,[4]],[[10,9,7,0],8,[6,0]],[8,[8,0,5,9],1,9,6],5]] +[[],[8,[[8],2,2],[]],[[1,7],6,[[0,3,9],7],0,6],[5,[[8,2,7],3,6,0]],[[5,4,[2],1,2],2,[[2,1],[9],[2,4,3],7],[]]] + +[[[[3,10,1,4,9],[9,4,2,10]],[1,5,[3,8],4],5,[2,[]],[2]],[7,[[3,7,2],[0,8,3,8],9],6,[],[]],[[],[[0],[6,10,9,4,1],7],8,[[5,9,4,2,0],3,5]],[8],[]] +[[[[]],[0,7,2,2]],[],[],[[2,[7,4,5,3],[],3,10]]] + +[[0,[[]],2,6]] +[[[7],[],[8,[10],[10,9,1,7]],5,[]],[[7,[1,2,6],4],[[6],[0,7,5]],[[10],[9,9],[0,2,9,3],[6,10,9],[]],[9],9],[],[1]] + +[[3,[[5,6,2,1],7,[8,6],1,0]],[]] +[[[[7,9,3,8],4,[2,9],[4,1,10],7],9,1,[10,[9],[0,6,9],[1,2],2]],[],[1,5,2],[2,[],1]] + +[[10,8,[[5]],3],[]] +[[[0,[8],[1,5,7,7,10],3],[[5],9],[[0,7],[7,10],2,[10,8,2,1,9]]],[[[],[2,1,10]],2,[9],1],[6,[[4,6,6],[3,4,6,10,10]],3],[5,9,8,5,[[4,4,1],8,6,[6]]],[10,[[],[9,5,1,4,9],8,[2,7,3,8,2],8],4]] + +[[[3,4,5,[10,4,7,8,1],4],6],[[[4]],1,[10],5],[3],[[6,7,1,2,9],[7,[1,5,10,4],[6],[5,3,10,9,6],7],[2],[[7,1]],[5,9,9,5]],[0]] +[[[[8,5,7,1,4],6,[7,1,10],[],[3,3,3,7]],[[4,1],8]],[[[0,7,10,4],5,3,10,6],3,9,[]],[6,[5,8],[]],[0,5]] + +[[8,2,3,[[1,7,10],5,4,[6,2,6],[8]]]] +[[3,[]]] + +[[[10,[10],3],5],[[8,6,4],1,[[0,1,1,4,2]],[[8,1],0],[[6,7,5,5]]],[[[2,8,1,0,7],[8,7,7,1],[3,2,8,8,8],[8,3,8],[8,5,7,9]],1],[9,[4,1,10]]] +[[10,6,7,[[6],[2,4,9,8],7,[5]]],[1,1,[3,[6],[]],[8,[4],[4]],4],[[[8,5,9],[6,1],6],[5],3],[2,9]] + +[[[[2,10,4,8,7]]],[7,0],[],[[],[10,[],[10,4,3],4,7],5],[9,[7,10],[]]] +[[[2],[[3,9,2],[],7,[8,8,2],[5,9]]],[[]],[[10,[],3,6,7]]] + +[[[9,[8,6,6],[10,8,10,0]],2],[1,5,6,5,6]] +[[1,4,7,[4,10,[9,10,2],8,8],[[7,7,9,0,10],[0,3],2]],[],[2]] + +[[2,5],[[6,[],[8,8,5,0],1],3,6,9],[[2,5,2,1,[6,8,0,6]]],[],[7,0,[1,[6,6],[1,1,0],7],2]] +[[[[]],5,[[3],3],[[10,3,1,5],8,8,0]],[2,[[7,0,6],[9,10,2],2,7,[4,4]],[3,9],8,0],[7,0],[[[1],[3],[5],[]]]] + +[[1,5,[[1,6,9,8,0],[],10,3]],[],[],[[2,[7,6,8],[1,3]],[9,[10,1,4],[5,6,5]],[7,[3,5,2,5,2],[4,9],[6,9,8,7]]]] +[[1,[5,9,3,5,7],[[1,0,1,0],1,[3,8,2]]],[7,[[],2,1,2,1],[[3],6,9,[0,6,9,4]],[[3,3,3,3]]],[[[9,5,5],5,[3,5,4,0],[8,6,2,6,9]],[[5,10,8],[0,6,6,0,3]],[9,5,10,5,5],6,4],[[[0,5,0,3],[3,1,3,7],[4]]],[[6,4]]] + +[[[[10,1]]],[[9,[3,3,4],2],[[5,3,8,9,6],4,[7,0,0,9],2],10,[],10]] +[[],[[[1,0,8,5]],10,[[5,6],[0,1],[0,7,2]],10,[6,0]],[3],[],[[6,[10,2,4,7,1],0,[6,6],5],[[6,9],[1,3]],[10,[9,10,6,1],0],[4,[10,7,3]],[[5],5,2,[7]]]] + +[2,10,6,3] +[2,10,6,3,2] + +[[4,2,1],[[[1],[6,5,3,7],[4,7,6],8],[[0,3,4],[3,9],[3,7,6,4,0],[3,8,0]],[5,[7,9,6,7,10],[1],[8,9,6,4,9]],[6,5,7,3]]] +[[[],[3],[7,[4,7,3],[10,6,7]],10],[8,[0,3],[0,3],[4,[8,6,3],8,0,4],[3,[1]]],[8,0,[[5,2,2,7]],1]] + +[[[[7],8,4,2],1,[4,[10,9],[3,2,6],3,[5,0,2,9]],[9,[0,7,2,2],[],[],3],7],[]] +[[4,0],[0],[]] + +[[[[],8,9,[0]]],[],[[8,[6,4,9,7],[2,7,6,1],[1,7,2,8],[4,8,9,4,0]],[5,1],6,9,4]] +[[8],[[[2,9,1],10,[1,2],[]],5,[]]] + +[[[6,0,[10,3],6],1,[5],6],[5,1]] +[[[5,[4,5,2],3],[4,1],[[1],9],[9,10,[],[10,0,7]]],[[[],2],[[]],2,[0,6,10]],[5,6]] + +[[[1,[],4,7],9],[4,5,9,[]],[],[4,10,4,[10]],[]] +[[2,9],[]] + +[[9,[[5,5,1,4,4],[8,4,0,6,0],3],4,8,10],[[9,4,8,3,[0,6]]]] +[[2],[5,[3,7,[7,4,6,1,3],10,[2,6]],1,6],[10,3,[6,7,[2,4,8],[9,8],8],0,[]]] + +[[[[0,5,4,1,4],[2,5,3,3,10],3]]] +[[8,1,5,5]] + +[[[0,[5,8,9,5,4],[9,1,3],[9,10,7,0]],0,[4,9,6]],[[[1]]],[[10,8,3],4,[],[[5,4],2,2,4,8]],[[[],[2,2],8],[3,3,[6,9],4]],[5,1,2]] +[[[[],10,[8,3],2],[[5,3,8,8,4],1,7],[[4],[1,1],7,4,7]],[[[6,7,10,7,8]],[[2],0,10,[0,7,7,6,10]],[7,10,0,4,[5]],4]] + +[[3,4],[[[1,6],1],[[],10,8,[6,8,8]],9,7]] +[[],[[[7,10]],3,0,[[],[],[8,7]],[[8,3,5],[6,4,2,8],2,[2]]],[7,10,7,[5,5]],[[8,[2,7,6,2],9,6,9],8,[[6,3]],5],[[[],7],10,1,7]] + +[[],[],[[[3],3],2,6,10,10],[10,[],6]] +[[0,[2],4],[3,[7,2,2,8],5,[[8]],8],[[],[[],[10,8],3,6,[]]],[6,[1],[2,[],4],[[7,2,4,6],[7,1,10,4,4],8,7]],[7,10,[3],0]] + +[[0,2],[],[]] +[[[6,10,[]]],[[1,[4,5,5]],[[]],[5,[],6,[0,1,8],[2,1]]]] + +[[[3],[5,[5,4,1]],2],[],[],[[[4]],[[8,2,10]],10]] +[[[[6,2,6,1],[],5,[4,8,5,1,1]]],[10],[[3,0,4,5],4,4]] + +[[]] +[[1,4,6,[[4],5],[[7,5],[],9,2]],[]] + +[[[0,[]],5,[[7,0],[10,5,9,2],[1,0,6,5],3,[8,9,4,4]]],[[[0,8,10,3],10,0],10,[[7,8,2,8]],[5,5,1,[9]],[[6,5],6,5,10]],[1,[[0]],2,8],[[7,[7,5,1,10,1],7,[6,10,1]],[]]] +[[[9,6,[10],[0,10,10],[8,1,7,9,7]],10],[[0],3,[1,[8,5]],[2,5,3,[]]]] + +[[0,6,[[7,5,2,8,8],[4,6,0,10]],[6,[6,6,5,3,5]]],[5,6,[1,[6,0]],[7,[9,0],[10,4,5],[2,4,10],[4,6]]],[[],5]] +[[[10,[6],[1,6],[5,7],9],9],[2,9],[],[[[9,7]],0,8,0]] + +[[[],4,[]],[2,2,3,3,3],[[[8,1,2,9,2]],4],[1,[[],[],7,5],[0,[2,6,8,4]]]] +[[9,[[4,5],9,[]],0]] + +[[[],9]] +[[7],[[2],7],[2,[8,[5,5],8,10,3],1,[[3,0],[0,1],2],10],[],[[8,[10,2,0,10,0],0,1]]] + +[[3,8]] +[[],[[]],[0]] + +[1,0,0,5,1] +[1,0,0,5] + +[[],[[5]]] +[[7,8],[[5,[10,6,0,4],[10,9,9,4,6],[3,7]],[[],7,[7,4],6,3]]] + +[[[3,[0,5,4,1,3],4,[10,9,10],[9,2,6]]]] +[[5,6,0,3],[],[0,[6,2,9,[0,8]],[0,[5,7,4,3],[6,9],[3,8,6,6],[9]]],[]] + +[[9],[1],[6,[[1,1],8,5]],[2],[10,[[4],[4,6],[4,3],[2,5],2],[[5,3,8,5,3],3,2]]] +[[3,[],1,[[1,0,10],9,3,[8,9,8,1,9]]]] + +[[5,[9,[1]],[],[0,5]]] +[[[9]],[10,10]] + +[[[[0,7],[6,6,1,2]]],[8,[[4,4,10],[3,7,2,9,2],[4,9],8,7],10,0],[10,4]] +[[8]] + +[[[[1,6,0,2],[2],6,4]],[[1],[[1,1],7,8],5,[0,[],[6,3,4],[0,2,9,2]]]] +[[3,[[4,4,0,5],[5,1,4,8,3]],[]],[]] + +[[[[3]],[[9,2]],[3,5,[7,2,9,9],[6,3,3],[5,6,8]],1]] +[[5,[[3,8,2,2,0],[0,9,2]],10,[[],[1,10,7,0],[5,10,7,6,6],[]]],[]] + +[[8,[[9,5,0],[7]],[[9,0,3,7]]],[]] +[[6,7,4,5,0],[10,[[5,3,8,3,8]],0,10],[9,3,[[10,3,2,4,10],7,[0,10,10,1],6]]] + +[[10,[[],0,[5,9,9,1,5],[2,0,10,9,9]],4,[0,[],[0,6,7],[5],8]],[],[7,6],[7,[[8,10,2,2,4],[],[8,3,3],[2,1,6,9],[1,7,7]],7]] +[[[],[[0,6],[5],[9,1,9,2,10],[9,2,4,7]],[2],5,1]] + +[[9,[]],[5,[[1,2]],4,[],8],[[[5,2]]]] +[[2,4,[4,[9,3,6,6],[0,0],1],1,0],[8,10,[[1,9,6,0]],4,[[0,10,7],5]],[],[]] + +[[[[8,1,1,8],10,3],[[8],10,8,9],5],[5,[],1,9,[[0,4,7,3,9]]],[],[2],[[1,8,[2,4,5,6],[9,9,4,7],[5,0,10,8,2]],[9,[],[4,5,0]],[9,[10]]]] +[[],[[]],[[5,4,[7,0],[4,6,5],9],[[10,9,0]],[[7],[1,4],2],2,10]] + +[[3,5,7,[0,0]]] +[[],[[[9],5,2,0],9,0],[[5,[0,6,0,10,1]],[9,[5,8,10,6,9]],5],[1,[[8,10,1,0]],8]] + +[] +[[[4,[3,6],8,5],[[2,1,0,3,4],[3,8,10],7],[[0,1,5,0,8],[4,1,8],[]],[[10],3,2,[6,2,1],[10,4,10]],[10,[2,9],3,6]],[],[[[3],[10],[8,5,1],[10,6],4],[5,[1,2],3,4,[]],[2]],[5,8],[[[3,4,3],[1],[],5],3,7,8]] + +[[[],[[6],6,[7,2],[8,7,0,10]],[0,[8,10,7],4],[7,6,9,2,4]],[3,6,[[2,5,7]]],[[8,[9],8,[],9],5],[3,[[],1,7,7]],[4]] +[[[0,[3,5,9,0],[5,2],8,6]],[3],[8,[8,6,[8,1]],[[8,4],[3,0,7,4,2],[6]],7,[4]],[[6,[4,1,10,3],[5,8,6],[3,10,1,3]],8],[5,[],7,[1],[1,[2,2,7,10]]]] + +[[5,1,[[],[]],5,3],[5,[[],8,0,[6,7],2],6,1,[3,8,8,0,6]]] +[[],[],[0],[7,4,[[5,7,3,5,1]],9],[]] + +[[5],[[[7,8,0],4]],[],[10,6,[[4,6],6,1],8],[[[1,8,7],6]]] +[[],[[[],[6,4,1,9,10],[6,9,2],[10,2,2,8],[]],[0,10]],[[[3,2],2,5],[],[],10]] + +[[0],[3],[0,3,[8],[],[[7,0],0,10,4,5]]] +[[6,[7,[],[6,2,5],[]],[5,[8,7,9],5,[4,1,6,9,7],5],[9,[],1,[10],7],1],[],[3,4,[6,[2],7],6,8]] + +[[[2,[4,2,7,7,9],0,[6],[5,1]],0],[[],[2,[1,7,3,7,5]],2,[],[[10,6,8,5],5,0,[0,10,8,2,0]]],[[6,[],6,[]],[[10,4,4,4,3],4,[8,0,5,1,3]],[2,[6],5,8]],[6],[[[10,0],9],[[],8,1],10,[[]],9]] +[[5,[7],[1,10,6,9,9],[8,0,5]],[]] + +[[[[9,4,9],[],3,[],[8,4,2]],9,1,[[10,10,4],0,6]],[[[]],[6,[4,4,10],4,[3,2,4,0,3],3],[],[[10,1,10,9],5,[3,5,5]],6],[],[6,6,3,5]] +[[9,3,[],8],[0,[1,0,5],7]] + +[[8,1,1,[]],[1,[],[[9,10]],4]] +[[2,3,9,6],[[[7,9],[3,7,4]],7,10,[]],[]] + +[[[[4,6]],7,9,7,3],[[[7,2,8,3,1],10]],[7,5,8],[[[2,6,7,10,5],1,9,3,[]]]] +[[4,[[10]],4,[6,[9],8,[]],[]],[1],[9],[[4,5,[1,10]]],[2,9]] + +[[0,[[0,3],[7],[1,10,6,2,4]]]] +[[[4,0,3],4,8,8],[],[]] + +[[[],[[0,8,8]]],[5],[8,8,0],[4]] +[[[[],[8,5,3,6],[1,10,5]],[[],[1,7,10,9,7]],2,[6],8],[]] + +[[[[4],8,[],[0]],8],[[],[[6,9],[2]],[5,0,6,[10,6,4]],[]]] +[[10,[[0,2],[4,9,9]]],[],[2],[[[8,9,0,1],9,8],0,10]] + +[[[5,7]]] +[[[2,6],[[6,2,8],10,1,6,[5,10,0,5]],[3,1,[4],[7,7]]],[7,[[7,0,4],2,7,0]],[[4,5,[4,4],[1]],[],[[3,9,2,2],4]]] + +[[[[7,3,10,2,1],[10,5,4,6,1],9,9,[]]],[[[7],10,[5,3,2,7]],[[1,0],6,[0,10]],[[0,0,9],[1,6,2,0],8,9],[[6,9,3,8,0],[8,8],[8,3],[5,4,1,1]]],[6,[5,[],10,2,4],[0,6,[6,0,0,10,8]],[[],[1,3,1],7],5]] +[[4,[7,[5,3,2,7,7]],6,[],[5,[6,6,0,2,6],8,[10,2]]]] + +[[[1,[],5],[],10,5,1],[[[],[7],2,[4,7],[1,1]],[9,[8,1,10,8,1],[2,3],[10,9],10],6,4,[10,2,[6,2],3]],[[4],[[],7,[8],[1,5,6]],[[],[7,3,6,5],2],[10,[2,9],5,[7,4],[4,6,4,8,4]],[8,0,[],6,[0]]],[[[9,0],[6,10,7,4,10],9],[]]] +[[[1]]] + +[[8,[9],[7,[7]]]] +[[[3,9,[9],[10,8],[5,9]],4,[8,[5,4,10,10,8],[],[10,3,5,7,7]]],[],[[[9,3],1],0,[5,[0,4,3,1],[]],[[],2,[10,0,8],8,[]]],[[0,10,5,[]]]] + +[[2],[7]] +[[[],9,3,[[4,0,2]],[]],[[3,[4,7,10,5,8],3,10,[1]],[3,[],[1,2],[0,8,7,1]],0,1,2],[0,6,7,[[0,1,5,7],[7,4,2,8,5],5]],[]] + +[[1,1,[],[[8],8,2,[10,8]],5],[],[10],[9,[4],[[],7,[7,5,9,9,1],5]],[6,0,[]]] +[[[[9,1,4]],[[6,8],[3,5]],[4],[6,[2,9,7,4,10],4,5,[2,2]]],[7,2],[2,[[6,7],[],[4,0,5],[6,7]],[],[[],[10,1],2,[],0]],[],[[10,1],3,6,[[],[8,10,9,3],[4,1,8,6,5]],0]] + +[[0,[0,[1,6,4],2,7],4,[],[3,[],6,5,[7,5,0,3,8]]],[],[[[10,6,4,2],9,[],3],6,6,0,8]] +[[5],[[[]],[[7,2,1],10],8]] + +[[[1,[2]],[1,9],[2,4,3],2,[[0],2,[1,1,7,10]]],[]] +[[[10],[[7,10],4,10,9],7],[[0,1,8,1,9],3],[[],9,[[0,8],[0,9],[5,7],6,0],[[],2,[],3],6],[6,[[],1]],[6,[[4],[3,7]],[],[3,7,[8,4,8]],10]] + +[[[8,[6,10,6],3]],[7,[]],[7,[],3]] +[[],[]] + +[[8,6,[[2],[6]],3,6],[],[3,9],[]] +[[9,9,[[9],[6,5,7]],0],[[],8,10,10]] + +[[3,1],[4]] +[[[[3,2,0,3,1],[1,9,6],7]],[7,[9,9],2],[4,7,7],[[[3,2],10,[]],0,[3,[3,5,4],[5,8,3],[10,2,9,8],[10,6,4,1,2]],[[7,7,8]],6]] + +[[[1,[8],4,[3,2,3,3],4]],[[[10,3]],4,[10,[5,3,2,8],[6,4,8,5],6,0],8],[[[7,4,7],[7,8],4],[10,[5,3,8,8],9,[]],[]]] +[[[2,[8,2,3,3,2]]],[[[],[0,5],9,[0,5]],[3,10,[7],[2,7,2,7,10]]]] + +[[5,5],[[[5],[6,3,4,4,7],6,8,[0]],[[9],[7,3,3,3],[5,8,0,0,3],[9,1,10],8],3,0,6],[[[5,10],6,7,[2,6,0,5]]]] +[[4,3],[[4],0,[[10],[7,2,2,6],6,[2],[6,3,2]],9],[[[9],4,2,8,[4]]]] + +[[6,[[0,6,10,4,9],[0],10,[]]],[[[]],9,[1,4,[6],[9,10]],8,10]] +[[8,4,3,10],[],[3,8,0],[8,8,0],[]] + +[[2,0],[[]]] +[[2,9,[[3,1,6,9,0],[8,1,1],1,4,2]]] + +[[],[2,10],[3,[[6,0,7,9],1,3],[[3],[3,4,8,7,5],3,1,8],4]] +[[6,2]] + +[[[1],[],[]]] +[[[[9],4]],[3,10],[[4,[1],[9]],[[]],6]] + +[[10,9,[]],[[9],9,[[0,3,8],10,7,[3],[5,2,7]],8,4],[[],7,[2],[8],4]] +[[],[[[5,0,3,2],[9,10,5],[0,6,9,1,3]],[9,5],[[2,3,5,10],4]],[0,0],[]] + +[[[[3,10,8,8,6],[0,5,4,7],4],9,3],[4,[[3,8,10,0],[10,0,1],7,10,5],7],[[2,[0,0],9],9],[4,10,[[6]],[[10,3,9,3],[5,10,10],[1,3,0,2,6],[0,1,4],6]],[]] +[[[1,1]],[[[5,4,8]],6,[[0]],7]] + +[[[5,[6,10,10,2]]],[1,[10,[],7,[4,3]],3],[5]] +[[[[7,9],1],[0,[9,8,7,8],0],8,9]] + +[[[9],[[2,5],[],4,[]],[],[0,5,1,4]],[6,[[9,1,5,1,3],0]],[[[3,5],1,[9,1],2],[],4],[9],[2,[[10,3,1,10],4,6,7],1,0,[[],10,[10]]]] +[[[],2],[[],[3,[2,6,8,9,0],[0,9,7,1,8],[8,8]]]] + +[[7]] +[[1,[[8,3],2,2,4],1,[]],[3,[[6]]]] + +[[[[]]],[3,[],[9,[10,0,5,5],4,[]],[1],[[],[10,9,1],7,[]]]] +[[[[2]],[[],[],[]],[[],2,2],5,5],[[9,[],[10,5,7,4,7],[4]],4],[[[],[9,5],0,1,[9,10,2]],9,8,[],5],[[[],[4,7]],[0,1,7],[7,[9],3]],[4,[[1,9,5,8,9],[1,8],[5,4,0,8,5],[],[8,2,0,4]]]] + +[[[],0,[[6,1,1],[3,10,7,1,3],[6,2,0,8]]]] +[[[6,[],[9,10,8,8,6]],2,[1,2],[5,[9,2,9,5],5,10]],[8,[[1,6,4],[2,7,3,10],[0,5,10]],[[2,4,1,1],[0],4]],[[]],[9,6,6,[],4],[[[10,5,2,0,6],[]],0]] + +[[[8,[5,8],[],[],[10,4]],[[4,4,6],[],1,[8,10,3,3,5],0],[[],[],[2,9,1]],5],[],[[[],2,[10,9,2,10],10],2,[[2,3,2]],[[4,6,7,3,0],[7,8,3,6,7]],[5,[5]]]] +[[7,[10,1,[7,3,4,10,4],[],0]],[[0,0],3,[[1,5,2,1],[],[7,2,7,3],[8],5]],[[[8,5,1,2,2],[2,8]],3,[9,[9,6,3,2,0],[10]],7],[[7,[8,9,4,10],1]]] + +[[[[4,5,9,2,8],[],[4,4,6,7],4]],[9,[6,2],[[3,1],9,[]],[5,[1,7,6,0,2],7],0],[[[2]],4,[[9,2,5],[1,1,0,8],[5,7],[5,3,4,7]]],[],[[4,[0,4,0,1],[1]],[],4,0,[]]] +[[[[],1,2],0],[4],[7,[9,[0,3,0]],[10,[6,3]],[[0]]],[7],[3,9]] + +[[5,[[0,5,1,4]],9],[1,4,10],[],[[9,[10,10,2,1,0],2,5],[[0,4,2,6],10,10,8,[]],[9,10,[6,9],[],[2,5,4,3]],[[7,6,4,1,3]],[[8],[3,3,0]]]] +[[[1,[8,7,1,1]],[[1,7],[],[3,5,4,2],6],10,4,10],[[],[[],[0,10],3],[],0]] + +[[],[[3,7],[0],0,3,[]],[8,[9,[],[1,10,1,7,7],[8,0]],1,[[6,2,3,7,10]],9],[6,7,5,[[7,7,0],4,[5,8,1],[6,5]]]] +[[[5,[3,8,9]],[[],[3,7,7],[3],10],[],[[4,9],[4,10]]],[9,4,3],[[3],10,8,[],4],[7,1,7]] + +[[[1],[],2,5]] +[[[0,9],[7,9]],[0,9,[[9,10,3],[],[]],0],[[1],8,4],[9,[8,[5,8],10,8],[2,1,8,9]]] + +[[7,[[3,6],3,[10,8,10]],[[6,6,2],[3,8,0],[7,9,7],7],[],4],[10,[1,[5,8,3]],[6,[2,7,10],[],[10,8]],6],[8,3,7,1,[[4,8],4,[6,0,7],[]]]] +[[3,[0,2]],[],[[1,[2,2,1,6],[]],[[9,3,3,3]]],[[6,[0,7,1],[8,7,4],[],10]],[9,[10,[10,3,2],[2],8,1],1,6]] + +[[[5,5,10,4,3],[2,[],[7,9,2,3,9],2,[0,10,7]]],[],[[[0,4,8],9,10,0],[2,[2,6,4,8,10],5],10,[],6]] +[[[],[[5],10,10,5],0],[[[1],3,[3],[1]]],[]] + +[[5,10,[1,10,6,10],0],[[[7,9,5,3,8],[7,0],3],3,10,7],[5],[[[9,0],0],2],[[7,[2,3],[3],[10,10,6,6]],9,[[2,0]]]] +[[2,[7],[10,[4,0],[4,3,6],6,10],[[9,8,9],9,[6,2,0]]]] + +[[4,[[9,2,8,9]],1,10],[[[5,8,4],[8,10],8,9],[5,2,[9,0]],[[10,5,0,10]],4,6],[[2],[2,[9],1],5],[[3,[8,0,2,4],8,6],6,4,[[],7,[4,10,8,9,9],2,[3,9,6]]],[[2,[2,1]],[[5],[4,6,4,10],6],[7,[8,4],[1,7,7,7],[3]],3]] +[[[[],[],10,[3,2,3,4],4]],[[],[[],[],5,[10,4,5,10,1],3],2,10,[[0,7,8,10]]],[3,[7,[]],[5,[4,8,6],2,[1,4,7],6],[]],[[6,8],[],[],1],[10,[0,3],[[0],[7,8,3,9,9]]]] + +[[10,[[1,2,5],[2]],[]]] +[[[9,[],4,5,[5,0,10]],9,3],[7,[0,2,1],[[4,7,6,0],[5,1,6]],[[],4,4,[10,1,10,3,1]],9],[0,[2,5,[6],0]],[[1,[]]],[[7]]] + +[[],[[[],8,10,7]],[4,[8],1],[1,[[7,2],6,[6,0,4,1]],[[2,1],[10,2,10,8,3],2,[9,4],2],5],[]] +[[[[7,6,7],[10,0]],[[3,5,5,6,1],3],[1],1,3],[2,[10,6],1,8],[],[[[3],[0,1,8],[0,8,4,10,4],9,3],[[3],8],3,[5,[5,2,1],[]]]] + +[[3,2,2,10]] +[[10,[10,3,[7,2,0],[]]],[[7,7,9,0,8],[[8,6],7],[1,6,4]]] + +[[[10,[10,9,1,1,3]],1],[1,5,7,8,[9]],[2,2,9,4]] +[[],[],[5],[[1,9],[[0,0,8,5],1,[2,3,10,0],[6,9]]],[]] + +[[[],1,[7,9,[],4]]] +[[[1,0,[9],[2,8,6,7],6]],[9,[10],3],[[8,3,7],[],[2,8],2],[]] + +[[1,[6,[6,1],[3,1],8,[2,6,9,8,4]],7,0,6],[0,[1,[3,10,2],6,7],[[8,10],1,[0,9,3,6],6,7],9,9],[[[9,1,9,2,8],4,[],[3,8,4]],8,[9,[6,2,9,5,9],8],3],[2,6,[1,8,[]]],[10]] +[[0,[10],6,10],[],[4,[[10,5,0],[]]],[8,[[4,1,4],[],4]],[4,10,[]]] + +[[[[4,9],[7],[4,8],3,[6,10,8,1]],3]] +[[[],[[6,5,8,9],1,7,[9,2,9,5,8],10],[[6,8,10,8],7,4,[2,1,0,6,6]]],[10],[[],[],0,[10,8,[5,3,4,5,0],[9,9]],[6,0,[4,8],[]]],[[[8],[4],[1,8,2],[5,6,4,6],[]],4]] + +[[],[],[10,[10]],[[[4,7,9,2],2,[10,4,7,3,6],[10,4,1],[9,0,10]],8,[[2,3,1,9],10,[9,2,9],[]]],[5,7,4,[[0],6,[9,0],[1,7,5,7,2]],1]] +[[[[0],[0,6,4],6],[2,4]],[[0,[0]],[[],[10,3,9,2,2]],[[8,6,8,3,3],4],7,8]] + +[[[5],[],[9,2,0,[9,3]]],[3,7,[4,3,[5,5]],4,8],[[1,[]]]] +[[[2,4]],[0,[5,8,1,[7,10]],[[1,10,2,0,0],7],[9,4,[8],[0,10,1,8,9],7]]] + +[[4,[[5,3,2,0],7,4,10],8,3,[[7,10],[5],[1,10,9,7],[3,4,1,6,5]]],[[2,6],[8,3,0,2,9]]] +[[[10,[4,10,8,6],[3,4],[3,1,3,3,2],[3,10,0,4]],7],[5,[],[[],[7,3],6],10],[],[[2,[6,3],6],[9,9,[0,8,9,5,6],[]]],[[7,[4,1,0,0,6]],7,[]]] + +[[[],[[8,6,1,6],[10]],[8,3,[2,1,2,6],[8,1,10,8],0]],[[0,[7,4]],5,7,[[10]]],[[[],8],[],8],[[6,[0,1],9,1,0],[],3,5,9]] +[[],[1],[7],[4,0,4,[]]] + +[[[7,3,6]],[8,[[],4,8,[6]],[1,4,[]],0,9],[]] +[[3,0,2,[]],[3,[],5,[]],[[[4,1,9,4],[5,10,3],9,[9,1]],[[10],7,[3]],[[],[2,5,0,10],8,5],[],4]] + +[[9,[9,[0,2]],10,[[0,7,9,4,2],2,[6,7,4,3],[7]],4],[],[[],10,4,5]] +[[[[9],[7],2,1,8],[[7,5,9],10],[],[]]] + +[[1,3,[[7],3,[6,10],[7,2,10,6],6],5],[],[[],[[],2],1,[10,[],8,3,[9,7,9,8]]],[]] +[[[10,[10,7,9],[],[8,9]],7],[2,10,[0,9,[4,2,10,7]],6,9],[[1,2,3],0],[5,3,[2],2]] diff --git a/day13/src/main.rs b/day13/src/main.rs new file mode 100644 index 0000000..53331c6 --- /dev/null +++ b/day13/src/main.rs @@ -0,0 +1,73 @@ +use serde_json::{json, Value}; +use std::cmp::Ordering; + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + + // Part 1 + let sum = contents + .trim() + .split("\n\n") + .map(|pair| { + pair.split("\n") + .map(|str| serde_json::from_str(str).unwrap()) + .collect::>() + }) + .enumerate() + .map(|(i, p)| (i + 1, in_order(&p[0], &p[1]))) + .fold(0, |acc, (i, good)| { + acc + if good.unwrap_or(false) { i } else { 0 } + }); + + println!("Part 1. Sum: {:?}", sum); + + // Part 2 + let two = json!([[2]]); + let six = json!([[6]]); + let mut packets = contents + .split("\n") + .filter(|&l| !l.is_empty()) + .map(|line| serde_json::from_str(line).unwrap()) + .collect::>(); + packets.append(&mut vec![two.clone(), six.clone()]); + packets.sort_by(cmp); + let idx1 = packets.iter().position(|x| x == &two).unwrap(); + let idx2 = packets.iter().position(|x| x == &six).unwrap(); + println!("Part 2. Decoder Key: {:?}", (idx1 + 1) * (idx2 + 1)); +} + +fn in_order(left: &Value, right: &Value) -> Option { + match left { + Value::Number(l) => match right { + Value::Number(r) => match l.as_u64().cmp(&r.as_u64()) { + Ordering::Less => Some(true), + Ordering::Greater => Some(false), + Ordering::Equal => None, + }, + Value::Array(_) => in_order(&json!([left]), &right), + _ => panic!("Bad input"), + }, + Value::Array(l) => match right { + Value::Number(_) => in_order(&left, &json!([right])), + Value::Array(r) => match (l.len(), r.len()) { + (0, 0) => None, + (0, _) => Some(true), + (_, 0) => Some(false), + (_, _) => match in_order(&l[0], &r[0]) { + None => in_order(&json!(l[1..]), &json!(r[1..])), + a => a, + }, + }, + _ => panic!("Bad input"), + }, + _ => panic!("Bad input"), + } +} + +fn cmp(l: &Value, r: &Value) -> Ordering { + match in_order(l, r) { + Some(true) => Ordering::Less, + Some(false) => Ordering::Greater, + None => Ordering::Equal, + } +} diff --git a/day13/test.txt b/day13/test.txt new file mode 100644 index 0000000..af73fbb --- /dev/null +++ b/day13/test.txt @@ -0,0 +1,23 @@ +[1,1,3,1,1] +[1,1,5,1,1] + +[[1],[2,3,4]] +[[1],4] + +[9] +[[8,7,6]] + +[[4,4],4,4] +[[4,4],4,4,4] + +[7,7,7,7] +[7,7,7] + +[] +[3] + +[[[]]] +[[]] + +[1,[2,[3,[4,[5,6,7]]]],8,9] +[1,[2,[3,[4,[5,6,0]]]],8,9] From 2cd9f24083a4dfe880cfccee566da9adf8d6693d Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 15 Dec 2022 00:35:02 -0500 Subject: [PATCH 08/10] Day 14. Best one yet! --- day14/Cargo.lock | 7 ++ day14/Cargo.toml | 8 +++ day14/data.txt | 169 ++++++++++++++++++++++++++++++++++++++++++++++ day14/src/main.rs | 123 +++++++++++++++++++++++++++++++++ day14/test.txt | 2 + 5 files changed, 309 insertions(+) create mode 100644 day14/Cargo.lock create mode 100644 day14/Cargo.toml create mode 100644 day14/data.txt create mode 100644 day14/src/main.rs create mode 100644 day14/test.txt diff --git a/day14/Cargo.lock b/day14/Cargo.lock new file mode 100644 index 0000000..c3eb0f2 --- /dev/null +++ b/day14/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day14" +version = "0.1.0" diff --git a/day14/Cargo.toml b/day14/Cargo.toml new file mode 100644 index 0000000..85aa832 --- /dev/null +++ b/day14/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day14" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day14/data.txt b/day14/data.txt new file mode 100644 index 0000000..06c2940 --- /dev/null +++ b/day14/data.txt @@ -0,0 +1,169 @@ +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +545,125 -> 549,125 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +558,119 -> 563,119 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +539,131 -> 543,131 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +526,82 -> 530,82 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +548,122 -> 553,122 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +523,84 -> 527,84 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +551,119 -> 556,119 +529,84 -> 533,84 +505,62 -> 509,62 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +526,86 -> 530,86 +548,128 -> 552,128 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +523,88 -> 527,88 +551,131 -> 555,131 +541,122 -> 546,122 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +517,88 -> 521,88 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +562,122 -> 567,122 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +532,82 -> 536,82 +505,58 -> 509,58 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +502,60 -> 506,60 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +535,88 -> 539,88 +554,134 -> 558,134 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +508,56 -> 512,56 +540,91 -> 540,92 -> 547,92 -> 547,91 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +540,91 -> 540,92 -> 547,92 -> 547,91 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +499,62 -> 503,62 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +548,134 -> 552,134 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +542,134 -> 546,134 +518,76 -> 518,77 -> 530,77 -> 530,76 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +544,119 -> 549,119 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +493,15 -> 493,16 -> 504,16 -> 504,15 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +542,128 -> 546,128 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +540,91 -> 540,92 -> 547,92 -> 547,91 +511,58 -> 515,58 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +538,86 -> 542,86 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +508,60 -> 512,60 +517,62 -> 521,62 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +518,76 -> 518,77 -> 530,77 -> 530,76 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +493,15 -> 493,16 -> 504,16 -> 504,15 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +547,116 -> 552,116 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +518,76 -> 518,77 -> 530,77 -> 530,76 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +541,88 -> 545,88 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +555,122 -> 560,122 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +532,86 -> 536,86 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +550,113 -> 555,113 +554,116 -> 559,116 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +514,60 -> 518,60 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +535,84 -> 539,84 +521,65 -> 521,67 -> 513,67 -> 513,72 -> 526,72 -> 526,67 -> 523,67 -> 523,65 +511,62 -> 515,62 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +529,80 -> 533,80 +545,131 -> 549,131 +503,53 -> 503,50 -> 503,53 -> 505,53 -> 505,51 -> 505,53 -> 507,53 -> 507,52 -> 507,53 -> 509,53 -> 509,50 -> 509,53 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +504,19 -> 504,23 -> 503,23 -> 503,31 -> 512,31 -> 512,23 -> 507,23 -> 507,19 +539,150 -> 539,152 -> 534,152 -> 534,159 -> 544,159 -> 544,152 -> 543,152 -> 543,150 +536,172 -> 536,168 -> 536,172 -> 538,172 -> 538,164 -> 538,172 -> 540,172 -> 540,168 -> 540,172 -> 542,172 -> 542,163 -> 542,172 -> 544,172 -> 544,164 -> 544,172 -> 546,172 -> 546,171 -> 546,172 -> 548,172 -> 548,165 -> 548,172 -> 550,172 -> 550,170 -> 550,172 -> 552,172 -> 552,170 -> 552,172 +554,110 -> 563,110 +539,105 -> 539,98 -> 539,105 -> 541,105 -> 541,100 -> 541,105 -> 543,105 -> 543,103 -> 543,105 -> 545,105 -> 545,101 -> 545,105 -> 547,105 -> 547,95 -> 547,105 -> 549,105 -> 549,104 -> 549,105 -> 551,105 -> 551,99 -> 551,105 -> 553,105 -> 553,97 -> 553,105 -> 555,105 -> 555,96 -> 555,105 +493,15 -> 493,16 -> 504,16 -> 504,15 +529,88 -> 533,88 +527,147 -> 527,139 -> 527,147 -> 529,147 -> 529,142 -> 529,147 -> 531,147 -> 531,138 -> 531,147 -> 533,147 -> 533,137 -> 533,147 -> 535,147 -> 535,140 -> 535,147 -> 537,147 -> 537,146 -> 537,147 -> 539,147 -> 539,142 -> 539,147 -> 541,147 -> 541,140 -> 541,147 +536,134 -> 540,134 +500,34 -> 500,37 -> 496,37 -> 496,40 -> 506,40 -> 506,37 -> 504,37 -> 504,34 +520,86 -> 524,86 diff --git a/day14/src/main.rs b/day14/src/main.rs new file mode 100644 index 0000000..eecb445 --- /dev/null +++ b/day14/src/main.rs @@ -0,0 +1,123 @@ +use std::collections::HashMap; + +#[derive(Debug, Clone)] +enum Pixel { + Wall, + Sand, + Source, +} + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let paths = contents + .lines() + .map(parse) + .collect::>>(); + + let mut cave: HashMap<(usize, usize), Pixel> = HashMap::new(); + cave.insert((500, 0), Pixel::Source); + create_cave(&mut cave, paths.clone()); + + // Part 1 + simulate(&mut cave.clone()); + + // Part 2 + add_floor(&mut cave); + simulate(&mut cave); +} + +fn parse(line: &str) -> Vec<(usize, usize)> { + line.split(" -> ") + .map(|c| c.split_once(",").unwrap()) + .map(|(l, r)| (l.parse().unwrap(), r.parse().unwrap())) + .collect::>() +} + +fn create_cave(cave: &mut HashMap<(usize, usize), Pixel>, paths: Vec>) -> () { + for path in paths { + for i in 0..path.len() - 1 { + let (x1, y1) = path[i]; + let (x2, y2) = path[i + 1]; + + for x in if x1 < x2 { x1..=x2 } else { x2..=x1 } { + cave.insert((x, y1), Pixel::Wall); + } + + for y in if y1 < y2 { y1..=y2 } else { y2..=y1 } { + cave.insert((x1, y), Pixel::Wall); + } + } + } +} + +fn draw_cave(cave: &HashMap<(usize, usize), Pixel>) -> () { + let x_min = cave.keys().map(|(x, y)| x).min().unwrap(); + let y_min = cave.keys().map(|(x, y)| y).min().unwrap(); + let x_max = cave.keys().map(|(x, y)| x).max().unwrap(); + let y_max = cave.keys().map(|(x, y)| y).max().unwrap(); + + for y in *y_min..=*y_max { + print!("{y} "); + for x in *x_min..=*x_max { + match cave.get(&(x, y)) { + Some(Pixel::Wall) => print!("#"), + Some(Pixel::Sand) => print!("o"), + Some(Pixel::Source) => print!("+"), + _ => print!("."), + } + } + println!(); + } +} + +fn simulate(cave: &mut HashMap<(usize, usize), Pixel>) -> () { + let mut grains = 1; + while drop_sand(cave) { + draw_cave(cave); + println!("Grains: {grains}"); + grains = grains+1; + } +} + +fn drop_sand(cave: &mut HashMap<(usize, usize), Pixel>) -> bool { + let mut sand = (500, 0); + let mut falling = true; + while falling { + if cave.get(&(sand.0, sand.1 + 1)).is_none() { + // sand beats down + sand = (sand.0, sand.1 + 1); + + // Part 1 ends when it falls forever (or to 500) + if sand.1 > 500 { + return false; + } + } else { + if cave.get(&(sand.0 - 1, sand.1 + 1)).is_none() { + // check down and left + sand = (sand.0 - 1, sand.1 + 1); + } else if cave.get(&(sand.0 + 1, sand.1 + 1)).is_none() { + // check down and right + sand = (sand.0 + 1, sand.1 + 1); + } else { + // neither are open + cave.insert((sand.0, sand.1), Pixel::Sand); + falling = false; + + // Part 2 ends when it hits sand immediately + if sand.1 == 0 { + return false; + } + } + } + } + + return true; +} + +fn add_floor(cave: &mut HashMap<(usize, usize), Pixel>) -> () { + let y = cave.keys().map(|(_, y)| y).max().unwrap().to_owned(); + + for x in 0..1000 { + cave.insert((x, y + 2), Pixel::Wall); + } +} diff --git a/day14/test.txt b/day14/test.txt new file mode 100644 index 0000000..4e87bb5 --- /dev/null +++ b/day14/test.txt @@ -0,0 +1,2 @@ +498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9 From 207752ab3c8d009498384c8e303b85e430c7d142 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 15 Dec 2022 16:52:40 -0500 Subject: [PATCH 09/10] Day 15. meh. it was alright --- day15/Cargo.lock | 42 ++++++++++++++++ day15/Cargo.toml | 9 ++++ day15/data.txt | 27 ++++++++++ day15/src/main.rs | 126 ++++++++++++++++++++++++++++++++++++++++++++++ day15/test.txt | 14 ++++++ 5 files changed, 218 insertions(+) create mode 100644 day15/Cargo.lock create mode 100644 day15/Cargo.toml create mode 100644 day15/data.txt create mode 100644 day15/src/main.rs create mode 100644 day15/test.txt diff --git a/day15/Cargo.lock b/day15/Cargo.lock new file mode 100644 index 0000000..c327390 --- /dev/null +++ b/day15/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "day15" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" diff --git a/day15/Cargo.toml b/day15/Cargo.toml new file mode 100644 index 0000000..4ccb6f6 --- /dev/null +++ b/day15/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day15" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.7.0" diff --git a/day15/data.txt b/day15/data.txt new file mode 100644 index 0000000..f9faa3b --- /dev/null +++ b/day15/data.txt @@ -0,0 +1,27 @@ +Sensor at x=2288642, y=2282562: closest beacon is at x=1581951, y=2271709 +Sensor at x=2215505, y=2975419: closest beacon is at x=2229474, y=3709584 +Sensor at x=275497, y=3166843: closest beacon is at x=-626874, y=3143870 +Sensor at x=1189444, y=2115305: closest beacon is at x=1581951, y=2271709 +Sensor at x=172215, y=2327851: closest beacon is at x=-101830, y=2000000 +Sensor at x=3953907, y=1957660: closest beacon is at x=2882446, y=1934422 +Sensor at x=685737, y=2465261: closest beacon is at x=1581951, y=2271709 +Sensor at x=1458348, y=2739442: closest beacon is at x=1581951, y=2271709 +Sensor at x=3742876, y=2811554: closest beacon is at x=3133845, y=3162635 +Sensor at x=437819, y=638526: closest beacon is at x=-101830, y=2000000 +Sensor at x=2537979, y=1762726: closest beacon is at x=2882446, y=1934422 +Sensor at x=1368739, y=2222863: closest beacon is at x=1581951, y=2271709 +Sensor at x=2743572, y=3976937: closest beacon is at x=2229474, y=3709584 +Sensor at x=2180640, y=105414: closest beacon is at x=3011118, y=-101788 +Sensor at x=3845753, y=474814: closest beacon is at x=3011118, y=-101788 +Sensor at x=2493694, y=3828087: closest beacon is at x=2229474, y=3709584 +Sensor at x=2786014, y=3388077: closest beacon is at x=3133845, y=3162635 +Sensor at x=3593418, y=3761871: closest beacon is at x=3133845, y=3162635 +Sensor at x=856288, y=3880566: closest beacon is at x=2229474, y=3709584 +Sensor at x=1757086, y=2518373: closest beacon is at x=1581951, y=2271709 +Sensor at x=2853518, y=2939097: closest beacon is at x=3133845, y=3162635 +Sensor at x=1682023, y=1449902: closest beacon is at x=1581951, y=2271709 +Sensor at x=3360575, y=1739100: closest beacon is at x=2882446, y=1934422 +Sensor at x=2904259, y=1465606: closest beacon is at x=2882446, y=1934422 +Sensor at x=3078500, y=3564862: closest beacon is at x=3133845, y=3162635 +Sensor at x=2835288, y=1011055: closest beacon is at x=2882446, y=1934422 +Sensor at x=2998762, y=2414323: closest beacon is at x=2882446, y=1934422 diff --git a/day15/src/main.rs b/day15/src/main.rs new file mode 100644 index 0000000..bcf5ebf --- /dev/null +++ b/day15/src/main.rs @@ -0,0 +1,126 @@ +use regex::Regex; +use std::ops::Range; + +#[derive(Debug)] +struct Sensor { + location: (isize, isize), + beacon: (isize, isize), + reach: isize, +} + +fn main() { + let contents = std::fs::read_to_string("data.txt").expect("Failed to read file"); + let sensors = contents.lines().map(parse).collect::>(); + + // Part 1 + let row = 2000000; + let mut reached = 0; + let range = find_range(&sensors); + for col in range { + let p = (col, row); + // if this cell has a beacon or sensor, it can' be here + if sensors.iter().any(|s| s.beacon == p) { + continue; + } + // if any of the beacons can reach this cell, count it + if sensors.iter().any(|s| can_reach(s, p)) { + reached += 1; + } + } + println!("Reached {reached}"); + + // Part 2 + let max = 4_000_000; + // Look in a diamond shape just outside of each sensor's reach + 'outer: for s in &sensors { + let per = perimeter(&s) + .into_iter() + .filter(|c| c.0 <= max && c.0 >= 0 && c.1 <= max && c.1 >= 0) + .collect::>(); + for coord in per { + if sensors.iter().any(|s| can_reach(s, coord)) { + continue; + } else { + println!("Found it: {:?}", coord); + let freq = coord.0 * 4000000 + coord.1; + println!("Tuning Frequency: {:?}", freq); + break 'outer; + } + } + } +} + +fn parse(str: &str) -> Sensor { + let re = Regex::new(r"Sensor at x=(.*), y=(.*): closest beacon is at x=(.*), y=(.*)").unwrap(); + let c = re.captures(str).unwrap(); + let p = (1..=4) + .map(|i| c.get(i).unwrap().as_str().parse().unwrap()) + .collect::>(); + + let location = (p[0], p[1]); + let beacon = (p[2], p[3]); + let reach = distance(location, beacon); + Sensor { + location, + beacon, + reach, + } +} + +fn distance(p1: (isize, isize), p2: (isize, isize)) -> isize { + let (x1, y1) = p1; + let (x2, y2) = p2; + (x1 - x2).abs() + (y1 - y2).abs() +} + +fn can_reach(s: &Sensor, p: (isize, isize)) -> bool { + distance(s.location, p) <= s.reach +} + +fn find_range(sensors: &Vec) -> Range { + let mut min = 0; + let mut max = 0; + + for s in sensors { + let leftmost = s.location.0 - s.reach; + if leftmost < min { + min = leftmost; + } + + let rightmost = s.location.1 + s.reach; + if rightmost > max { + max = rightmost; + } + } + min..max +} + +fn perimeter(s: &Sensor) -> Vec<(isize, isize)> { + let mut coords = vec![]; + let length = s.reach + 2; + + // start on the left, then go clockwise around the sensor's range + let (mut col, mut row) = (s.location.0 - s.reach - 1, s.location.1); + for i in 0..=length { + coords.push((col + i, row - i)); + } + // from the top, down and right + (col, row) = (s.location.0, s.location.1 - s.reach - 1); + for i in 0..=length { + coords.push((col + i, row + i)); + } + + // from the right, going down and left + (col, row) = (s.location.0 + s.reach + 1, s.location.1); + for i in 0..=length { + coords.push((col - i, row + i)); + } + + // from the bottom, going up and left + (col, row) = (s.location.0, s.location.1 + s.reach + 1); + for i in 0..=length { + coords.push((col - i, row - i)); + } + + coords +} diff --git a/day15/test.txt b/day15/test.txt new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/day15/test.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 From 11a2064d5eb2dba0c95e55a9dd75e5950f84a7a6 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 28 Dec 2022 12:56:05 -0700 Subject: [PATCH 10/10] Day 16. Didn't finish. Actually barely started. See you next year! --- day16/Cargo.lock | 42 +++++++++++++++++++++++++++++++++ day16/Cargo.toml | 9 +++++++ day16/data.txt | 60 +++++++++++++++++++++++++++++++++++++++++++++++ day16/src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++ day16/test.txt | 10 ++++++++ 5 files changed, 179 insertions(+) create mode 100644 day16/Cargo.lock create mode 100644 day16/Cargo.toml create mode 100644 day16/data.txt create mode 100644 day16/src/main.rs create mode 100644 day16/test.txt diff --git a/day16/Cargo.lock b/day16/Cargo.lock new file mode 100644 index 0000000..caf7216 --- /dev/null +++ b/day16/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "day16" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" diff --git a/day16/Cargo.toml b/day16/Cargo.toml new file mode 100644 index 0000000..0f02dee --- /dev/null +++ b/day16/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day16" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.7.0" diff --git a/day16/data.txt b/day16/data.txt new file mode 100644 index 0000000..334e693 --- /dev/null +++ b/day16/data.txt @@ -0,0 +1,60 @@ +Valve NQ has flow rate=0; tunnels lead to valves SU, XD +Valve AB has flow rate=0; tunnels lead to valves XD, TE +Valve IA has flow rate=0; tunnels lead to valves CS, WF +Valve WD has flow rate=0; tunnels lead to valves DW, II +Valve XD has flow rate=10; tunnels lead to valves AB, NQ, VT, SC, MU +Valve SL has flow rate=0; tunnels lead to valves RP, DS +Valve FQ has flow rate=15; tunnels lead to valves EI, YC +Valve KF has flow rate=0; tunnels lead to valves FL, QP +Valve QP has flow rate=0; tunnels lead to valves KF, RP +Valve DS has flow rate=0; tunnels lead to valves SL, AA +Valve IK has flow rate=0; tunnels lead to valves XC, AA +Valve HQ has flow rate=0; tunnels lead to valves VM, WV +Valve WR has flow rate=0; tunnels lead to valves WV, HF +Valve HH has flow rate=20; tunnels lead to valves PI, CF, CN, NF, AR +Valve DW has flow rate=19; tunnels lead to valves KD, WD, HS +Valve RP has flow rate=14; tunnels lead to valves SL, QP, BH, LI, WP +Valve EC has flow rate=0; tunnels lead to valves NF, XC +Valve AA has flow rate=0; tunnels lead to valves NH, ES, UC, IK, DS +Valve VM has flow rate=18; tunnel leads to valve HQ +Valve NF has flow rate=0; tunnels lead to valves HH, EC +Valve PS has flow rate=0; tunnels lead to valves AR, SU +Valve IL has flow rate=0; tunnels lead to valves XC, KZ +Valve WP has flow rate=0; tunnels lead to valves CS, RP +Valve WF has flow rate=0; tunnels lead to valves FL, IA +Valve XW has flow rate=0; tunnels lead to valves OL, NL +Valve EH has flow rate=0; tunnels lead to valves UK, YR +Valve UC has flow rate=0; tunnels lead to valves AA, FL +Valve CS has flow rate=3; tunnels lead to valves IA, CN, LD, RJ, WP +Valve AR has flow rate=0; tunnels lead to valves PS, HH +Valve CF has flow rate=0; tunnels lead to valves HH, FL +Valve NH has flow rate=0; tunnels lead to valves AA, LD +Valve RJ has flow rate=0; tunnels lead to valves DJ, CS +Valve XC has flow rate=17; tunnels lead to valves IL, EC, YR, IK, DJ +Valve TE has flow rate=24; tunnels lead to valves AB, YA +Valve CN has flow rate=0; tunnels lead to valves HH, CS +Valve KD has flow rate=0; tunnels lead to valves DW, UK +Valve SC has flow rate=0; tunnels lead to valves EI, XD +Valve MU has flow rate=0; tunnels lead to valves XD, YP +Valve SU has flow rate=22; tunnels lead to valves PS, LI, II, NQ +Valve FL has flow rate=8; tunnels lead to valves KF, WF, CF, UC, HS +Valve OL has flow rate=4; tunnels lead to valves KZ, HF, XW +Valve EI has flow rate=0; tunnels lead to valves FQ, SC +Valve NL has flow rate=0; tunnels lead to valves XW, UK +Valve YP has flow rate=21; tunnels lead to valves YA, MU, YC +Valve BH has flow rate=0; tunnels lead to valves VT, RP +Valve II has flow rate=0; tunnels lead to valves SU, WD +Valve YA has flow rate=0; tunnels lead to valves TE, YP +Valve HS has flow rate=0; tunnels lead to valves FL, DW +Valve DJ has flow rate=0; tunnels lead to valves RJ, XC +Valve KZ has flow rate=0; tunnels lead to valves OL, IL +Valve YR has flow rate=0; tunnels lead to valves EH, XC +Valve UK has flow rate=7; tunnels lead to valves KD, NL, EH +Valve YC has flow rate=0; tunnels lead to valves FQ, YP +Valve ES has flow rate=0; tunnels lead to valves PI, AA +Valve LI has flow rate=0; tunnels lead to valves SU, RP +Valve LD has flow rate=0; tunnels lead to valves NH, CS +Valve VT has flow rate=0; tunnels lead to valves BH, XD +Valve PI has flow rate=0; tunnels lead to valves ES, HH +Valve WV has flow rate=11; tunnels lead to valves WR, HQ +Valve HF has flow rate=0; tunnels lead to valves OL, WR diff --git a/day16/src/main.rs b/day16/src/main.rs new file mode 100644 index 0000000..7d1e5f6 --- /dev/null +++ b/day16/src/main.rs @@ -0,0 +1,58 @@ +use regex::Regex; +use std::collections::{HashMap, HashSet}; + +#[derive(Debug)] +struct Room<'a> { + valve: &'a str, + rate: u8, + tunnels: Vec<&'a str>, +} + +fn main() { + let contents = std::fs::read_to_string("test.txt").expect("Failed to read file"); + + let mut hash: HashMap<&str, Room> = HashMap::new(); + + contents.lines().for_each(|l| parse(l, &mut hash)); + println!("rooms {:?}", hash); + + traverse(&mut hash, "AA"); +} + +fn parse<'a>(str: &'a str, hash: &mut HashMap<&'a str, Room<'a>>) -> () { + let re = Regex::new(r"Valve (.*) has flow rate=(.*); tunnels? leads? to valves? (.*)").unwrap(); + let c = re.captures(str).unwrap(); + let valve = c.get(1).unwrap().as_str(); + let rate = c.get(2).unwrap().as_str().parse().unwrap(); + let tunnels = c + .get(3) + .unwrap() + .as_str() + .split(", ") + .collect::>(); + hash.insert( + valve, + Room { + valve, + rate, + tunnels, + }, + ); +} + +fn traverse(hash: &mut HashMap<&str, Room>, start: &str) -> () { + let mut visited: HashSet<&str> = HashSet::new(); + let cur = hash.get(start).unwrap(); + + // let mut max = 0; + let mut biggest = cur.tunnels.iter().fold((start, 0), |acc, r| { + let room = hash.get(r).unwrap(); + if hash.get(r).unwrap().rate > max { + max = room.rate; + r + } else { + acc + } + }); + println!("biggest {:?}", biggest); +} diff --git a/day16/test.txt b/day16/test.txt new file mode 100644 index 0000000..9f30acc --- /dev/null +++ b/day16/test.txt @@ -0,0 +1,10 @@ +Valve AA has flow rate=0; tunnels lead to valves DD, II, BB +Valve BB has flow rate=13; tunnels lead to valves CC, AA +Valve CC has flow rate=2; tunnels lead to valves DD, BB +Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE +Valve EE has flow rate=3; tunnels lead to valves FF, DD +Valve FF has flow rate=0; tunnels lead to valves EE, GG +Valve GG has flow rate=0; tunnels lead to valves FF, HH +Valve HH has flow rate=22; tunnel leads to valve GG +Valve II has flow rate=0; tunnels lead to valves AA, JJ +Valve JJ has flow rate=21; tunnel leads to valve II