From 17733c951f88a7e9908c1da8466a8c9d7f228832 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 07:07:35 +0000 Subject: [PATCH 1/7] Day 08 --- input/08.txt | 1103 +++++----------------------------------------- src/day08/mod.rs | 78 +++- 2 files changed, 165 insertions(+), 1016 deletions(-) diff --git a/input/08.txt b/input/08.txt index 6d7a76f..4a5b0fe 100644 --- a/input/08.txt +++ b/input/08.txt @@ -1,1004 +1,99 @@ -$ cd / -$ ls -282959 btm -dir fmfnpm -dir gwlwp -dir hchp -275929 hmbbjbf -dir nsphznf -dir phschqg -193293 rhpwvff -dir spfwthmd -dir wchdqb -dir zlpmfh -191479 zlpmfh.gpt -$ cd fmfnpm -$ ls -dir fgtqvq -194704 fwdvgnqp.fsm -48823 fwdwq.tsq -224991 mtjngt -79386 rdsgpfjb.sfn -dir rvnwwfq -dir wrzcjwc -dir zlpmfh -$ cd fgtqvq -$ ls -293783 rjc.ncl -324635 wdjrhw -$ cd .. -$ cd rvnwwfq -$ ls -76914 btm -$ cd .. -$ cd wrzcjwc -$ ls -dir fwdwq -2159 fzb.tjs -dir lddhdslh -dir mjp -284475 vclnlds -196284 zjtftd -$ cd fwdwq -$ ls -120795 jqnl.hzj -$ cd .. -$ cd lddhdslh -$ ls -293030 fzb -dir gzj -$ cd gzj -$ ls -dir qzgsswr -$ cd qzgsswr -$ ls -33681 qzgsswr.wmv -121649 sbjbw.shv -$ cd .. -$ cd .. -$ cd .. -$ cd mjp -$ ls -289491 btm -169221 jqnl.hzj -$ cd .. -$ cd .. -$ cd zlpmfh -$ ls -189296 ldgpvnh -$ cd .. -$ cd .. -$ cd gwlwp -$ ls -dir dwcrnbj -dir fmfnpm -dir fwdwq -dir hzpsts -dir hzrq -dir jzwpjtf -dir lmmpmghg -dir mnw -dir qzgsswr -dir zlpmfh -$ cd dwcrnbj -$ ls -182989 btm -145822 fwdvgnqp.fsm -dir jbtfslcn -dir lgbglc -293584 mfstl.hhp -dir sbffqq -dir zhvn -$ cd jbtfslcn -$ ls -195255 sbjbw.shv -$ cd .. -$ cd lgbglc -$ ls -261423 fmfnpm.rqh -323530 fzb.lmm -314800 hbl.blm -173052 lwpglgt -dir qdpjss -184808 zlpmfh.chl -$ cd qdpjss -$ ls -83714 djsrhr.vch -68191 lqljcq.sdv -$ cd .. -$ cd .. -$ cd sbffqq -$ ls -29842 fwdvgnqp.fsm -$ cd .. -$ cd zhvn -$ ls -dir clpcg -dir gswvch -dir lgmfhnq -184407 rlvprbs -235779 rnh.dlv -99852 ttjtnj.gjs -$ cd clpcg -$ ls -97780 fwdvgnqp.fsm -201515 rth.rhm -$ cd .. -$ cd gswvch -$ ls -dir fzb -dir mdrlrtl -$ cd fzb -$ ls -dir qzgsswr -$ cd qzgsswr -$ ls -198805 jqnl.hzj -$ cd .. -$ cd .. -$ cd mdrlrtl -$ ls -154648 spm.wvf -$ cd .. -$ cd .. -$ cd lgmfhnq -$ ls -303154 mjzw.tbq -$ cd .. -$ cd .. -$ cd .. -$ cd fmfnpm -$ ls -134756 bdmdfvs.llm -228983 brfrv -dir fzb -52921 mnfsltsj -185748 sbjbw.shv -dir svtswb -$ cd fzb -$ ls -dir fzb -$ cd fzb -$ ls -dir gwtslbr -$ cd gwtslbr -$ ls -224479 fmfnpm.jqh -$ cd .. -$ cd .. -$ cd .. -$ cd svtswb -$ ls -dir fmfnpm -55122 fmfnpm.fdl -dir msfd -30710 rtfwvd.fch -71950 vnwvdmcm.pbz -$ cd fmfnpm -$ ls -318326 rtv.jgn -$ cd .. -$ cd msfd -$ ls -86195 jqnl.hzj -$ cd .. -$ cd .. -$ cd .. -$ cd fwdwq -$ ls -203108 fzb -72579 gcjd.nvp -dir hjfpn -dir qbhn -dir qbnfrhdn -310102 wbdhc -248602 wgdmvslc.ffs -140513 znrlqvmb.thj -dir ztf -$ cd hjfpn -$ ls -dir tfdt -$ cd tfdt -$ ls -202865 chvfjrgz.rdm -$ cd .. -$ cd .. -$ cd qbhn -$ ls -34122 fmfnpm -19676 gfbt.nnc -dir hgdgbrw -dir jlztdcl -93203 jqnl.hzj -dir ppcqqtlj -167401 qzgsswr -$ cd hgdgbrw -$ ls -25297 fzb.vlp -dir nbl -$ cd nbl -$ ls -dir czpfqbp -dir fwdwq -$ cd czpfqbp -$ ls -135628 fzb.tqn -$ cd .. -$ cd fwdwq -$ ls -165198 lrgpwnvv -280597 mcmcn -$ cd .. -$ cd .. -$ cd .. -$ cd jlztdcl -$ ls -188181 bzgzvfjv -116603 fwdvgnqp.fsm -dir fwdwq -277728 fzb -63999 zfrvp.ppd -281623 zgpwjbc.wcj -$ cd fwdwq -$ ls -dir bpt -dir fmrmm -dir fzb -dir qzgsswr -$ cd bpt -$ ls -179418 dhwvhq -313538 njnhmgg -28613 rjc.ncl -267053 sbjbw.shv -$ cd .. -$ cd fmrmm -$ ls -258044 fmfnpm -120035 qcgrnqf.flz -141340 rjc.ncl -dir sps -$ cd sps -$ ls -dir sphjrrvc -$ cd sphjrrvc -$ ls -152609 cwjwbp -$ cd .. -$ cd .. -$ cd .. -$ cd fzb -$ ls -99140 btm -$ cd .. -$ cd qzgsswr -$ ls -25030 cjdltcj.wsw -$ cd .. -$ cd .. -$ cd .. -$ cd ppcqqtlj -$ ls -39010 zcmf.jll -$ cd .. -$ cd .. -$ cd qbnfrhdn -$ ls -282601 cdw.mfn -dir fwdwq -121557 pnqzb.njr -145720 wznfpcn.qhp -$ cd fwdwq -$ ls -152478 fwdvgnqp.fsm -dir fwdwq -dir hhnf -301797 hngcrd.hgt -dir hpj -dir qzgsswr -$ cd fwdwq -$ ls -dir cdb -69663 fmfnpm -97882 fwdvgnqp.fsm -61057 hlg.zhq -115474 rzv.vjv -$ cd cdb -$ ls -193519 djwqhfg.mhl -255813 zptj.cgm -$ cd .. -$ cd .. -$ cd hhnf -$ ls -dir cpzrh -$ cd cpzrh -$ ls -167401 fwdvgnqp.fsm -224559 jqnl.hzj -$ cd .. -$ cd .. -$ cd hpj -$ ls -dir spqljpnt -$ cd spqljpnt -$ ls -268253 fjvqzqt.lmz -63824 lfdrvlc.pww -$ cd .. -$ cd .. -$ cd qzgsswr -$ ls -dir fwdwq -dir hwnmf -dir rbjpbm -dir vhstcbnf -dir zflfcnw -$ cd fwdwq -$ ls -29113 ctr.zjl -130931 jqnl.hzj -111025 zlpmfh -$ cd .. -$ cd hwnmf -$ ls -dir dspbm -260735 fwdwq.qwg -297937 fzb -45087 fzb.drv -19946 jqnl.hzj -51617 nqt.wpr -$ cd dspbm -$ ls -29361 btm -dir fng -127536 lspft.twz -$ cd fng -$ ls -47261 rjc.ncl -$ cd .. -$ cd .. -$ cd .. -$ cd rbjpbm -$ ls -135665 cgfsvqf -dir dbppcf -259879 fwdvgnqp.fsm -143467 qrssbjm.wbh -dir rgvvmf -dir trtgj -$ cd dbppcf -$ ls -72276 jqnl.hzj -$ cd .. -$ cd rgvvmf -$ ls -dir zhldtfj -$ cd zhldtfj -$ ls -248448 hflnwgj -$ cd .. -$ cd .. -$ cd trtgj -$ ls -94714 flfhzc -137893 qqdrglf.rbp -306051 rggqwl -$ cd .. -$ cd .. -$ cd vhstcbnf -$ ls -142465 btm -dir fcpwqj -dir fzb -310858 jmfsz -dir mjjcsbcn -223040 qfnwth.lwq -dir qjfwzdn -dir qzgsswr -156837 rjc.ncl -244544 tvsfmpc.nhz -$ cd fcpwqj -$ ls -75022 thrdj.vnw -$ cd .. -$ cd fzb -$ ls -206638 rjc.ncl -20614 vnq.sfp -277785 zpfjd.vgr -$ cd .. -$ cd mjjcsbcn -$ ls -22117 cscpb.tpw -$ cd .. -$ cd qjfwzdn -$ ls -124990 qzgsswr.pdw -$ cd .. -$ cd qzgsswr -$ ls -dir chptwpbh -dir fwdwq -305061 htbcc.dbl -22511 jqnl.hzj -80135 svvdgvsw -dir vqtnpbn -$ cd chptwpbh -$ ls -55085 fdhzfd.vbl -324100 mcbr.jtn -148699 rtlmz.lvp -$ cd .. -$ cd fwdwq -$ ls -321251 qbqjp.cvs -$ cd .. -$ cd vqtnpbn -$ ls -211984 btm -181182 fwdwq.nsg -212415 lnqbtt.fvs -$ cd .. -$ cd .. -$ cd .. -$ cd zflfcnw -$ ls -183836 jqnl.hzj -$ cd .. -$ cd .. -$ cd .. -$ cd .. -$ cd ztf -$ ls -dir vrj -$ cd vrj -$ ls -dir fmfnpm -dir jqmcczl -$ cd fmfnpm -$ ls -52873 sbjbw.shv -$ cd .. -$ cd jqmcczl -$ ls -153695 sbjbw.shv -$ cd .. -$ cd .. -$ cd .. -$ cd .. -$ cd hzpsts -$ ls -dir dsqsfqv -51518 fmfnpm.bhn -dir fszpc -dir fwdwq -dir vrhcvmn -$ cd dsqsfqv -$ ls -221801 hqjwts -$ cd .. -$ cd fszpc -$ ls -dir lrtsfgg -dir nndsvglm -$ cd lrtsfgg -$ ls -316171 lhl.jwm -dir qhph -193371 zbttt.rmd -$ cd qhph -$ ls -71851 zqswjnp.tnc -$ cd .. -$ cd .. -$ cd nndsvglm -$ ls -5209 ddjgsmsm.rhv -dir qzgsswr -$ cd qzgsswr -$ ls -313591 flmfc.pbn -$ cd .. -$ cd .. -$ cd .. -$ cd fwdwq -$ ls -134971 fmfnpm.rcs -$ cd .. -$ cd vrhcvmn -$ ls -95237 fzb.zqw -147520 qzgsswr -dir zrnsdljf -$ cd zrnsdljf -$ ls -120643 sbjbw.shv -$ cd .. -$ cd .. -$ cd .. -$ cd hzrq -$ ls -177154 fwdvgnqp.fsm -127561 fwdwq.hcd -dir vqt -$ cd vqt -$ ls -148546 fwdvgnqp.fsm -108391 rjc.ncl -$ cd .. -$ cd .. -$ cd jzwpjtf -$ ls -46687 fwdwq -$ cd .. -$ cd lmmpmghg -$ ls -dir fwdwq -dir snqlc -dir trtpbbs -dir zlpmfh -$ cd fwdwq -$ ls -dir dzfmw -dir fmfnpm -dir qgdjz -dir qzgsswr -102754 qzgsswr.wns -$ cd dzfmw -$ ls -77275 fmfnpm.qgg -dir fwdwq -$ cd fwdwq -$ ls -132030 fwdvgnqp.fsm -311894 jgvg.lgb -dir mbqw -309968 pwv -185449 qzgsswr.nrz -283317 sbjbw.shv -dir zlpmfh -$ cd mbqw -$ ls -292344 fwdwq.sfb -43979 sbjbw.shv -dir stmb -$ cd stmb -$ ls -164954 fwdwq.gtn -$ cd .. -$ cd .. -$ cd zlpmfh -$ ls -248119 jqnl.hzj -$ cd .. -$ cd .. -$ cd .. -$ cd fmfnpm -$ ls -306459 wrscv -$ cd .. -$ cd qgdjz -$ ls -dir llb -dir pstmbbv -dir qth -dir qzgsswr -dir rqbmzrh -dir ssqj -$ cd llb -$ ls -dir fmfnpm -8728 rjc.ncl -95783 sbjbw.shv -$ cd fmfnpm -$ ls -266355 ddfzzh -$ cd .. -$ cd .. -$ cd pstmbbv -$ ls -303042 fnj.vsr -$ cd .. -$ cd qth -$ ls -155353 chqshnhb.sdm -$ cd .. -$ cd qzgsswr -$ ls -155632 zlpmfh.dhp -$ cd .. -$ cd rqbmzrh -$ ls -202692 rjc.ncl -$ cd .. -$ cd ssqj -$ ls -dir gffp -$ cd gffp -$ ls -34212 rjc.ncl -$ cd .. -$ cd .. -$ cd .. -$ cd qzgsswr -$ ls -324875 btm -275450 fmfnpm.gdr -190317 hwzgndb -251199 mgdsh.tfd -$ cd .. -$ cd .. -$ cd snqlc -$ ls -dir ztsc -$ cd ztsc -$ ls -169368 jqnl.hzj -$ cd .. -$ cd .. -$ cd trtpbbs -$ ls -216211 hhf.hww -101252 htzmmgd -$ cd .. -$ cd zlpmfh -$ ls -312349 qtgpq.jjf -$ cd .. -$ cd .. -$ cd mnw -$ ls -dir fwdwq -dir vbplht -dir zgrwvc -dir zqlqrjgr -$ cd fwdwq -$ ls -dir cgtmbm -dir dpnhgsf -dir lddgc -dir qzgsswr -29505 vffzmhj.jqc -$ cd cgtmbm -$ ls -dir dcw -132840 fwdvgnqp.fsm -dir fwdwq -143226 hwsqv -160598 sbjbw.shv -$ cd dcw -$ ls -dir mdqq -dir nrg -$ cd mdqq -$ ls -230975 fwdvgnqp.fsm -dir fzb -207419 mdwmrb.nqd -dir pfzsg -dir wvtjplcj -$ cd fzb -$ ls -116263 fmfnpm -$ cd .. -$ cd pfzsg -$ ls -211092 chp.lwg -149800 fbffjpz -124128 nqvdq.hhl -$ cd .. -$ cd wvtjplcj -$ ls -82449 lcchd.gsl -$ cd .. -$ cd .. -$ cd nrg -$ ls -32393 fwdvgnqp.fsm -$ cd .. -$ cd .. -$ cd fwdwq -$ ls -63350 zlpmfh.jql -$ cd .. -$ cd .. -$ cd dpnhgsf -$ ls -89773 mchpsf.cwl -$ cd .. -$ cd lddgc -$ ls -56273 cwdjlw -261919 fzb.qbr -49151 vtjc.qqq -$ cd .. -$ cd qzgsswr -$ ls -288467 jqnl.hzj -283162 tfsnpt.cpr -$ cd .. -$ cd .. -$ cd vbplht -$ ls -115696 mfwsbzzm.stf -149951 mnwj.jdn -222362 vtpzr.nnj -dir zlpmfh -$ cd zlpmfh -$ ls -dir dqszzbs -293533 fmfnpm.msw -184810 jsfqrptb.dcl -224988 whmgv -$ cd dqszzbs -$ ls -140899 btm -$ cd .. -$ cd .. -$ cd .. -$ cd zgrwvc -$ ls -dir prmrv -182098 qsljdzsj -16089 rjc.ncl -24028 tpq.dhd -$ cd prmrv -$ ls -261147 rjc.ncl -285206 zlpmfh.hcz -$ cd .. -$ cd .. -$ cd zqlqrjgr -$ ls -dir cjmhdr -16259 sbjbw.shv -$ cd cjmhdr -$ ls -43212 fmfnpm -36273 jqnl.hzj -66482 jtm.hmj -$ cd .. -$ cd .. -$ cd .. -$ cd qzgsswr -$ ls -154596 frglht.mvr -dir fwdwq -dir fzb -150820 rjc.ncl -$ cd fwdwq -$ ls -dir gmthth -121228 mfsmjhr -dir njm -dir tslpnb -$ cd gmthth -$ ls -315665 fzb.gwc -291878 qzgsswr.gss -105707 rjc.ncl -$ cd .. -$ cd njm -$ ls -49199 fwdvgnqp.fsm -17669 jqnl.hzj -$ cd .. -$ cd tslpnb -$ ls -268158 fwdwq -$ cd .. -$ cd .. -$ cd fzb -$ ls -270392 jqnl.hzj -38464 rwfjhg.rqt -19087 wcqmpcrf.tzp -5009 zlpmfh.mmq -$ cd .. -$ cd .. -$ cd zlpmfh -$ ls -dir dfgz -dir fmfnpm -58493 sbjbw.shv -dir tvzqcg -12981 zlpmfh.ggc -$ cd dfgz -$ ls -dir ncrggngr -202192 qdnfl.pvt -$ cd ncrggngr -$ ls -dir fwdwq -$ cd fwdwq -$ ls -246049 btm -123758 vfw -$ cd .. -$ cd .. -$ cd .. -$ cd fmfnpm -$ ls -90482 fwdwq -$ cd .. -$ cd tvzqcg -$ ls -37360 btm -$ cd .. -$ cd .. -$ cd .. -$ cd hchp -$ ls -dir jlcsc -191408 jqnl.hzj -dir lvws -dir zlpmfh -dir zvb -$ cd jlcsc -$ ls -dir pqdwdtm -$ cd pqdwdtm -$ ls -203078 cwtdqrn -169712 fwdvgnqp.fsm -106227 jqnl.hzj -66550 zvmf -$ cd .. -$ cd .. -$ cd lvws -$ ls -210229 tcc.ngf -$ cd .. -$ cd zlpmfh -$ ls -dir vdtv -$ cd vdtv -$ ls -dir fdrv -$ cd fdrv -$ ls -40808 fmfnpm.ssj -$ cd .. -$ cd .. -$ cd .. -$ cd zvb -$ ls -dir cnsddwlg -dir csdz -$ cd cnsddwlg -$ ls -232 jqnl.hzj -dir tcjhhh -$ cd tcjhhh -$ ls -225195 flm.hmb -227170 vvdqw.hwn -$ cd .. -$ cd .. -$ cd csdz -$ ls -dir qzgsswr -$ cd qzgsswr -$ ls -250314 fzb.jhc -$ cd .. -$ cd .. -$ cd .. -$ cd .. -$ cd nsphznf -$ ls -277038 vns.cpc -162685 wvzlgf.dss -dir zldbdl -$ cd zldbdl -$ ls -263687 jqnl.hzj -137415 qzgsswr -$ cd .. -$ cd .. -$ cd phschqg -$ ls -291670 fwdvgnqp.fsm -163036 rjc.ncl -dir zlpmfh -$ cd zlpmfh -$ ls -dir qzgsswr -37363 szdv.chv -$ cd qzgsswr -$ ls -22625 tzhpl -$ cd .. -$ cd .. -$ cd .. -$ cd spfwthmd -$ ls -294499 ltfjljcv -$ cd .. -$ cd wchdqb -$ ls -162305 jtf -215603 zlpmfh -$ cd .. -$ cd zlpmfh -$ ls -dir czb -dir hdm -dir jhqqbqb -dir zmj -$ cd czb -$ ls -17922 fmcljr.qvz -263068 fmlr.sgr -$ cd .. -$ cd hdm -$ ls -dir frbhzrg -dir fwdwq -270049 fwdwq.ldt -297608 glbb -252269 gsdccc.hqw -169953 nlhj.hnc -224702 rjc.ncl -$ cd frbhzrg -$ ls -195491 sbjbw.shv -219888 sgfhs -$ cd .. -$ cd fwdwq -$ ls -dir ggc -dir mvqtp -dir rzvtdvvz -dir vjppb -$ cd ggc -$ ls -150629 fwdwq -144726 fwdwq.csr -18186 fwdwq.jwt -$ cd .. -$ cd mvqtp -$ ls -dir dhp -$ cd dhp -$ ls -132445 btm -$ cd .. -$ cd .. -$ cd rzvtdvvz -$ ls -122759 fwdwq.ptd -$ cd .. -$ cd vjppb -$ ls -286152 jrmjsz.rrq -dir qzgsswr -292661 tnh -$ cd qzgsswr -$ ls -205580 pswmmn.psl -$ cd .. -$ cd .. -$ cd .. -$ cd .. -$ cd jhqqbqb -$ ls -dir mbqc -149539 npgls -156208 nzfphn -dir nzzlwdd -223923 sbjbw.shv -$ cd mbqc -$ ls -11260 lcgnzwcn.pvp -$ cd .. -$ cd nzzlwdd -$ ls -29180 qzgsswr.tcq -232156 zlpmfh.jls -$ cd .. -$ cd .. -$ cd zmj -$ ls -160097 fgt.tfr +000110211310120301121312200301013204213433321324454125534120411314221421432204213123312010120220212 +020012220322312203320423330100141123432232555552131513413333243201041414120414404222300212031212210 +012220223030033030244430211234220045555352514254335343332514243345202201013120303123003031322301122 +122212123203002004034001203313232515112543525251445353434224414533524014020242242322320223010131111 +221102333103333221411324203034235135343455415425134251221521352512445343444242410343133031022213111 +012230110220232130043321130142424543415542352534155145434324432553532253510101043422110311302133322 +121211332110331222432012225134422432341112144421535153351545122134242232152130110334114400332212301 +021003020222222304114022552315552411255554221162455263643351112333322235214353424140101110301013303 +211101331332303134320025235254523341134225246335554356533426415514344521433235223100024100322012213 +333121033001133202211331144545334545442426462335662255342342626524313432432255113203322322002312200 +010322322141333044304115552115413253563353625666244464226452656243424545525555553333333441121311231 +032330304041422420432124324244236264255242536445626642464646454324353143112245143242140134012131113 +113222323414023431235314252222246225364232452564335525523344525664226653522112244131121334130320322 +121002342323322351314412423235433523643565644552335525522462425253643354453342113152132444223043001 +303334322134140123154241513636446642663346536533667552624355222224332425555525121255313000110224301 +220002230431013514545542433426226364623335746773567356566543235364324426533313525143244100121033121 +000021230222223532342154546526322562455734556753354636566447345465225625536633243334425112234211423 +100240131421551532143146364665353436655665676767645757767765365733656436442643144352144332403323231 +231322213332442222125662224633422364634335777574764763343776757435645252662432644231412254011230044 +120320134105242421434423353553635756666465645446464476746533434474737333355643266531333324433212400 +040301032133333254244335264252755376435754666436667554445363634375377335452223443455551432333010144 +122213043542434415443363356663454534757746355453573654756377543673443533255466446625122132310014334 +404243133333412213345434336533364653576735676667576876848337555643553537554265552455111211141430132 +443341301142235415336322424546477554637688787745866488887577434344636437734332223546335121555322312 +010101032453544354452355447644336446437444776676584487868688446743777537543333245666541212524431033 +130342114125121245655353346445355733446776587588784546588474685553536367574445523434254125522412333 +201242125553223436463223777745535566487874456667845657466784588644655657455355352552555444112241410 +234243354255336642454256767534444878687668756444875858587854778474484437455656353656253411335452400 +314411514514125254223353563444477554768588585448654748855456656667558676453446723446433551542333323 +320425133145356325256655577477467888556845884576957768675647786647464447677575756555366251552252023 +402024545131232242524557435777674845786545558766585956759687647846545584454443736545244555225513404 +221122211244626363325464534767668748756767978988996968588875755867566656577763547532353323245443324 +241532435425235552535674564444576465866975867987658575886697775645646467874734565535246546512244511 +003324252512442566553463345787864877859757886699855979689556865965885747456343365533654353244413223 +212315155352232566544737454548674574998798699555866589956966599795788486474536636336336342425134412 +415441242523246256336374766665674457785867567956675565789686897696764587674446666642422423644424325 +003554554162525244354667375865755578679858897996697678587867758755877644755534673566366364215111142 +125233324364265264545556378688676958976975966767698999896779575577774547466453456456443363332524521 +133515233335323373765654467667486986756665797876767877886899897979969475847765767637544446523515551 +423321431443554355375376748446757899656688998798978687676986796699688855776746674753723255464344423 +135533214666522656775554775567665797957679899988997898976779768787598768878445373375735633533152423 +424233114545442654745646856857767575855668667669968786879669768579688844877458655735443652256255431 +122311356255465443775736754747756597959866677969777897778796669799996864878444747533472262262432453 +444412515423634374457557568574698568569796689667999968899788697787968667768664344766546653236143151 +152311513646633365333568844555559779697686687699777998989779788768756594585758837374575424224211225 +432411444656462746443756865787956975798986999787977978996987778795768586656655473453364365634145515 +512553242524235344467755668868885977997876879978998878787888776866887566586456466377353633526612112 +115351124324666765477575484768566785899876668997898988879879687896986577677786744336653645225533134 +521325356522544377445774755666867759866698988779997998778687879779588576866784766376455535443334445 +433451166554445354353554886675567598968878879979777887878988976979689787756548837435743345242455141 +251112342434644676347767747746557587886869778877788877789787767787779598548856756754774536542212331 +255533535452264557543768667886686886969966689777797988888776799775755889645756557644656535253352415 +324425154344254736457678866646677886667896679787789878898879666995998757877665757356457366362624453 +133124434355236446553476678549686757888986787979777777777688968798699766877778645555763233323653111 +222452162524456534544666745659767986569977869888798889797967869896588579678645466757742223536551125 +424113153224246464437444666655586769888767899997999998888899787785789988565667547344466324245444121 +342222535222265477673374467445978889589796696777887788896778866766575958886864746654456432465143345 +343222443646553344666565758455597897568767997897989877768698886656896987444854633767335332566315312 +031554353342263455343465844688566996587977697767668887776787776985778754456646747553435562222553224 +433344426223464443364358688754655656655796979887796696799797776855755885854676557736723432655514323 +013424132332422364434378758577758958968878668787687777668989797955778846555856344635566224523432451 +425553255655633236466566556555858699675969687978968799698978595977567446657483654746364253324544543 +432514124535636275635556647685849565689989686678799779666769657669586678877883365364425535653142315 +234213331454244233774547658465845955685598696976697898886596997865578648447647767565654326321345225 +033142335256632253667547655784646566787758855877679696866998778855965474867574633636266434253231343 +433312355254243222736457466486484557878576698897857857878667659897748755645557367664665624322214245 +414352553244643424464634565857556876595979575859559697897855757678648446474443347476454343621315123 +142445323134524455344767565745845466776955777775867689676669795765547875554364474423523255433452550 +420122443552465225677645453567777645666955697588578797786675779777688864467444476525625534353143351 +013443533555256553426677376778475746848576769888786588999657647784787744733675443533555552342334414 +041143134325455654456573473653567744748677959966765568889765584875786654736746477425243323345412331 +432143141143446546334633656476476747668864878778688989957788768655568457357565474424624643255141123 +433102225554544652566677347757675544755546776854548575485655786465456345347635444252452425412221021 +323443424115135364655624436464533765445864646766688454775574785855547635756475456566323553413444404 +040133323452356462366536677466667655766665575477656846545868855587636377637445452653223313533353431 +112321424255322325434562636744647576545484488884676746485886457756657434733352222326531315442212112 +414000041225332224365336457576353475444476854684468466666787764643346356664452563552325454125532334 +333211225554233356326636546733667664766764488678465547455847554453346674333644366565122142144303010 +201332104431213354534342624674574463634343535468776645455555777363557644444632234452452514524001241 +221121011452151314652546563557737463367364573534653347663557476557753774362253653622553112413343020 +301020124355152323443536262535766773343757377576554465333456475773466764562232455333212154220142002 +322244112312155113454636553224336764436375336773533577673334677767346562553664626122341142230310021 +023103242244551224134442622342463557736536665447475654776476664656746264566536561531213151202033312 +000241244142514451335326646524645362354365656733573556434643567466354436652444155224421243124420312 +211134103233321112311333366243566524426453645455335474755773645624422266326533321441535122300041202 +010100412012005141235135226623364644564632374554665337675444336522352336344211133431343203034321031 +120100431404011341421515543345452262445624345326474644544443332326425454424431513314250240023131303 +302230432233042151414231553363365325522333634334265345542443426352642445554145124343411121104121203 +303233311431323211311335332253563563355242335445366433543356662343656654554132224132234403131422203 +113031120441440344151555331254253525425556245652343434526624622453624343111341315343030233140303323 +113030023221043232305142142324135442534246224522233522334666345254334253143442354430224400423233033 +031102021123340100023323433221314345163424223524353422623255522232122453215412122243144210131021220 +123100111112040244104314311535251111141234335442354234236235354514311422435123332031213231120131013 +221222103112033302010413133424335241315253243263446652241133155434215112225523013040443233102021312 +000012122130221202130324443534215451343511144224113442315342555214421524454224402343214132100123030 +112012230223002044203111222225321532514541153222214115443212344354545231533442344002011210202230120 +221200232013222231032340233141433354514311213113224441111332422254143133440204220101431230200002121 +211212213121303211020441232042142451254444244521121152121224435112515234204202321124222332312210022 +202120032200122003044340422223002041534554215334252122413441432141223402244034330212233311001312221 diff --git a/src/day08/mod.rs b/src/day08/mod.rs index bda60d8..3191706 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -1,26 +1,80 @@ use crate::read; -use indextree::Arena; -use itertools::Itertools; - -#[derive(Debug)] -struct Directory { - pub size: usize, - pub name: String -} pub fn run() { - for line in read("08").lines() { + let input = read("08"); + let lines = input.lines().collect::>(); + let mut count = 0; + let mut max_score = 0; + for r in 1..lines.len() - 1 { + let row = lines[r]; + for c in 1..row.len() - 1 { + let height = row[c..=c].parse().unwrap(); + + let mut is_visible = 4; + let mut score = 1; + + score *= if let Some(sc_score) = (0..c) + .rev() + .map(|cc| (c - cc, row[cc..=cc].parse::().unwrap())) + .find(|(_sc_score, other_height)| other_height >= &height) + { + is_visible -= 1; + sc_score.0 + } else { + c + }; + + score *= if let Some(sc_score) = (c + 1..row.len()) + .map(|cc| (cc - c, row[cc..=cc].parse::().unwrap())) + .find(|(_sc_score, other_height)| other_height >= &height) + { + is_visible -= 1; + sc_score.0 + } else { + row.len() - c - 1 + }; + + score *= if let Some(sc_score) = (0..r) + .rev() + .map(|rr| (r - rr, lines[rr][c..=c].parse::().unwrap())) + .find(|(_sc_score, other_height)| other_height >= &height) + { + is_visible -= 1; + sc_score.0 + } else { + r + }; + + score *= if let Some(sc_score) = lines + .iter() + .enumerate() + .skip(r + 1) + .map(|(rr, l)| (rr - r, l[c..=c].parse::().unwrap())) + .find(|(_sc_score, other_height)| other_height >= &height) + { + is_visible -= 1; + sc_score.0 + } else { + lines.len() - r - 1 + }; + + max_score = max_score.max(score); + + if is_visible > 0 { + count += 1; + } + } } + count += 2 * lines.len() + 2 * (lines[0].len() - 2); #[cfg(feature = "part1")] { - println!("Day 8, Part 01: {}", "TODO"); + println!("Day 8, Part 01: {}", count); } #[cfg(feature = "part2")] { - println!("Day 8, Part 02: {}", "TODO"); + println!("Day 8, Part 02: {}", max_score); } } - From b4f9e56227819fa4950df99ba6b1ef1bf7ed4d25 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 07:38:37 +0000 Subject: [PATCH 2/7] Parse numbers once and save to grid --- src/day08/mod.rs | 66 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index 3191706..85c8c55 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -1,72 +1,76 @@ use crate::read; pub fn run() { - let input = read("08"); - let lines = input.lines().collect::>(); + let grid: Vec> = read("08") + .lines() + .map(|l| { + l.chars() + .map(|c| c.to_digit(10).unwrap()) + .collect::>() + }) + .collect::>(); + + let width = grid.first().unwrap().len(); let mut count = 0; let mut max_score = 0; - for r in 1..lines.len() - 1 { - let row = lines[r]; - for c in 1..row.len() - 1 { - let height = row[c..=c].parse().unwrap(); - - let mut is_visible = 4; + for (row, row_vec) in grid.iter().enumerate().skip(1) { + for (col, height) in row_vec.iter().enumerate().skip(1) { + let mut sides_visible = 4; let mut score = 1; - score *= if let Some(sc_score) = (0..c) + score *= if let Some(sc_score) = (0..col) .rev() - .map(|cc| (c - cc, row[cc..=cc].parse::().unwrap())) - .find(|(_sc_score, other_height)| other_height >= &height) + .map(|cc| (col - cc, row_vec[cc])) + .find(|(_sc_score, other_height)| other_height >= height) { - is_visible -= 1; + sides_visible -= 1; sc_score.0 } else { - c + col }; - score *= if let Some(sc_score) = (c + 1..row.len()) - .map(|cc| (cc - c, row[cc..=cc].parse::().unwrap())) - .find(|(_sc_score, other_height)| other_height >= &height) + score *= if let Some(sc_score) = (col + 1..width) + .map(|cc| (cc - col, row_vec[cc])) + .find(|(_sc_score, other_height)| other_height >= height) { - is_visible -= 1; + sides_visible -= 1; sc_score.0 } else { - row.len() - c - 1 + width - col - 1 }; - score *= if let Some(sc_score) = (0..r) + score *= if let Some(sc_score) = (0..row) .rev() - .map(|rr| (r - rr, lines[rr][c..=c].parse::().unwrap())) - .find(|(_sc_score, other_height)| other_height >= &height) + .map(|rr| (row - rr, grid[rr][col])) + .find(|(_sc_score, other_height)| other_height >= height) { - is_visible -= 1; + sides_visible -= 1; sc_score.0 } else { - r + row }; - score *= if let Some(sc_score) = lines + score *= if let Some(sc_score) = grid .iter() .enumerate() - .skip(r + 1) - .map(|(rr, l)| (rr - r, l[c..=c].parse::().unwrap())) - .find(|(_sc_score, other_height)| other_height >= &height) + .skip(row + 1) + .map(|(rr, l)| (rr - row, l[col])) + .find(|(_sc_score, other_height)| other_height >= height) { - is_visible -= 1; + sides_visible -= 1; sc_score.0 } else { - lines.len() - r - 1 + grid.len() - row - 1 }; max_score = max_score.max(score); - if is_visible > 0 { + if sides_visible > 0 { count += 1; } } } - count += 2 * lines.len() + 2 * (lines[0].len() - 2); #[cfg(feature = "part1")] { From 8509a884ff1ceaac80dc2cd356fc8a30a8f060c1 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 07:53:26 +0000 Subject: [PATCH 3/7] Repalce for-loops by flat_map and map --- src/day08/mod.rs | 117 +++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 55 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index 85c8c55..e9ff9bf 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -11,66 +11,73 @@ pub fn run() { .collect::>(); let width = grid.first().unwrap().len(); - let mut count = 0; - let mut max_score = 0; - for (row, row_vec) in grid.iter().enumerate().skip(1) { - for (col, height) in row_vec.iter().enumerate().skip(1) { - let mut sides_visible = 4; - let mut score = 1; - - score *= if let Some(sc_score) = (0..col) - .rev() - .map(|cc| (col - cc, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - col - }; - - score *= if let Some(sc_score) = (col + 1..width) - .map(|cc| (cc - col, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - width - col - 1 - }; - - score *= if let Some(sc_score) = (0..row) - .rev() - .map(|rr| (row - rr, grid[rr][col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - row - }; - - score *= if let Some(sc_score) = grid + let res = grid + .iter() + .enumerate() + .skip(1) + .flat_map(|(row, row_vec)| { + row_vec .iter() .enumerate() - .skip(row + 1) - .map(|(rr, l)| (rr - row, l[col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - grid.len() - row - 1 - }; + .skip(1) + .map(|(col, height)| { + let mut sides_visible = 4; + let mut score = 1; - max_score = max_score.max(score); + score *= if let Some(sc_score) = (0..col) + .rev() + .map(|cc| (col - cc, row_vec[cc])) + .find(|(_sc_score, other_height)| other_height >= height) + { + sides_visible -= 1; + sc_score.0 + } else { + col + }; - if sides_visible > 0 { - count += 1; - } - } - } + score *= if let Some(sc_score) = (col + 1..width) + .map(|cc| (cc - col, row_vec[cc])) + .find(|(_sc_score, other_height)| other_height >= height) + { + sides_visible -= 1; + sc_score.0 + } else { + width - col - 1 + }; + + score *= if let Some(sc_score) = (0..row) + .rev() + .map(|rr| (row - rr, grid[rr][col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + sides_visible -= 1; + sc_score.0 + } else { + row + }; + + score *= if let Some(sc_score) = grid + .iter() + .enumerate() + .skip(row + 1) + .map(|(rr, l)| (rr - row, l[col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + sides_visible -= 1; + sc_score.0 + } else { + grid.len() - row - 1 + }; + + (sides_visible > 0, score) + }) + .collect::>() + }) + .collect::>(); + + let count = res.iter().filter(|e| e.0).count(); + let max_score = res.iter().map(|e| e.1).max().unwrap(); #[cfg(feature = "part1")] { From ab0889b661790737ceeb6a03c10d13aa467cad81 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 07:58:50 +0000 Subject: [PATCH 4/7] moved final calculations to their respective parts --- src/day08/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index e9ff9bf..3c4ae76 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -76,16 +76,15 @@ pub fn run() { }) .collect::>(); - let count = res.iter().filter(|e| e.0).count(); - let max_score = res.iter().map(|e| e.1).max().unwrap(); - #[cfg(feature = "part1")] { + let count = res.iter().filter(|e| e.0).count(); println!("Day 8, Part 01: {}", count); } #[cfg(feature = "part2")] { + let max_score = res.iter().map(|e| e.1).max().unwrap(); println!("Day 8, Part 02: {}", max_score); } } From d23fee8c4be7ab6014b4eb31b3ebc28a3c4d468d Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 08:27:43 +0000 Subject: [PATCH 5/7] Improve score/is_visible calculation --- src/day08/mod.rs | 91 ++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/src/day08/mod.rs b/src/day08/mod.rs index 3c4ae76..86305bb 100644 --- a/src/day08/mod.rs +++ b/src/day08/mod.rs @@ -22,55 +22,48 @@ pub fn run() { .enumerate() .skip(1) .map(|(col, height)| { - let mut sides_visible = 4; - let mut score = 1; - - score *= if let Some(sc_score) = (0..col) - .rev() - .map(|cc| (col - cc, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - col - }; - - score *= if let Some(sc_score) = (col + 1..width) - .map(|cc| (cc - col, row_vec[cc])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - width - col - 1 - }; - - score *= if let Some(sc_score) = (0..row) - .rev() - .map(|rr| (row - rr, grid[rr][col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - row - }; - - score *= if let Some(sc_score) = grid - .iter() - .enumerate() - .skip(row + 1) - .map(|(rr, l)| (rr - row, l[col])) - .find(|(_sc_score, other_height)| other_height >= height) - { - sides_visible -= 1; - sc_score.0 - } else { - grid.len() - row - 1 - }; - - (sides_visible > 0, score) + let values = [ + if let Some(sc_score) = (0..col) + .rev() + .map(|col_left| (col - col_left, row_vec[col_left])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, col) + }, + if let Some(sc_score) = (col + 1..width) + .map(|col_right| (col_right - col, row_vec[col_right])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, width - col - 1) + }, + if let Some(sc_score) = (0..row) + .rev() + .map(|row_above| (row - row_above, grid[row_above][col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, row) + }, + if let Some(sc_score) = grid + .iter() + .enumerate() + .skip(row + 1) + .map(|(row_below, l)| (row_below - row, l[col])) + .find(|(_sc_score, other_height)| other_height >= height) + { + (1, sc_score.0) + } else { + (0, grid.len() - row - 1) + }, + ]; + let is_visible = values.map(|e| e.0).iter().sum::() < 4; + let scenic = values.map(|e| e.1).iter().product::(); + (is_visible, scenic) }) .collect::>() }) From a3d1031b05f3cee44f00bd4f094964b93953f5d1 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 8 Dec 2022 18:23:45 +0000 Subject: [PATCH 6/7] Day 9 boilerplate --- input/09.txt | 0 src/day09/mod.rs | 18 ++++++++++++++++++ src/main.rs | 4 ++++ 3 files changed, 22 insertions(+) create mode 100644 input/09.txt create mode 100644 src/day09/mod.rs diff --git a/input/09.txt b/input/09.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/day09/mod.rs b/src/day09/mod.rs new file mode 100644 index 0000000..446eb6b --- /dev/null +++ b/src/day09/mod.rs @@ -0,0 +1,18 @@ +use crate::read; +use itertools::Itertools; + +pub fn run() { + let input = read("08"); + let lines = input.lines(); + for line in lines {} + + #[cfg(feature = "part1")] + { + println!("Day 9, Part 01: {}", "TODO"); + } + + #[cfg(feature = "part2")] + { + println!("Day 9, Part 02: {}", "TODO"); + } +} diff --git a/src/main.rs b/src/main.rs index 9738ba8..715bdf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod day05; mod day06; mod day07; mod day08; +mod day09; fn main() { let today = Local::now().day(); @@ -37,6 +38,9 @@ fn main() { if cfg!(feature = "day08") || (cfg!(feature = "today") && today == 8) { day08::run(); } + if cfg!(feature = "day09") || (cfg!(feature = "today") && today == 9) { + day09::run(); + } println!("Finished, time taken: {:?}", now.elapsed()) } From ad889622d50064c89e11700462265094df8f6c73 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Fri, 9 Dec 2022 05:48:13 +0000 Subject: [PATCH 7/7] Day 09 --- input/09.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++++ src/day09/mod.rs | 95 ++- 2 files changed, 2089 insertions(+), 6 deletions(-) diff --git a/input/09.txt b/input/09.txt index e69de29..704e4cd 100644 --- a/input/09.txt +++ b/input/09.txt @@ -0,0 +1,2000 @@ +L 1 +D 2 +L 2 +U 2 +D 2 +U 2 +D 1 +R 1 +U 1 +R 1 +L 2 +R 1 +U 1 +R 2 +L 1 +D 2 +R 2 +L 2 +D 1 +U 2 +R 2 +L 1 +U 2 +D 2 +U 2 +D 2 +L 1 +R 2 +L 1 +R 1 +L 1 +D 2 +L 1 +U 1 +L 2 +U 1 +L 2 +R 2 +D 1 +L 2 +R 1 +D 1 +U 1 +L 1 +U 2 +D 2 +L 1 +U 1 +R 2 +D 1 +L 1 +U 1 +R 2 +D 1 +R 2 +D 2 +U 2 +R 2 +U 2 +R 1 +L 1 +R 1 +L 1 +D 1 +U 1 +R 2 +D 2 +L 2 +U 1 +D 1 +R 1 +U 1 +L 1 +D 2 +U 2 +D 1 +L 2 +U 1 +D 1 +L 2 +U 1 +R 2 +D 1 +L 1 +R 1 +L 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 +D 15 +R 4 +U 19 +D 8 +L 11 +R 17 +U 7 +D 1 +U 2 +R 18 +L 10 +R 4 +D 7 +U 8 +L 1 +U 11 +L 15 +R 2 +U 13 +R 12 +L 8 +R 1 +U 4 +L 12 +R 13 +D 7 +U 5 +R 14 +U 13 +D 13 +L 18 +U 18 +R 14 +D 18 +U 12 +D 11 +U 6 +R 6 +U 5 +R 4 +D 7 +R 1 +U 5 +R 1 +D 1 +U 13 +D 10 +R 19 +D 7 +U 14 +D 7 +U 19 +R 12 +L 2 +D 12 +U 10 +D 17 +L 12 +U 1 +R 8 +L 8 +D 15 +R 9 +U 5 +R 11 +L 10 +R 2 +L 13 +U 10 +L 7 +U 1 diff --git a/src/day09/mod.rs b/src/day09/mod.rs index 446eb6b..2b7b20b 100644 --- a/src/day09/mod.rs +++ b/src/day09/mod.rs @@ -1,18 +1,101 @@ use crate::read; use itertools::Itertools; +use std::collections::HashSet; + +#[derive(Debug, PartialEq, Eq, Default, Clone, Copy, Hash)] +struct Position { + pub x: i32, + pub y: i32, +} + +impl Position { + pub fn to_right(&self, count: i32) -> Self { + Self { + x: self.x + count, + y: self.y, + } + } + pub fn to_left(&self, count: i32) -> Self { + Self { + x: self.x - count, + y: self.y, + } + } + pub fn to_up(&self, count: i32) -> Self { + Self { + x: self.x, + y: self.y - count, + } + } + pub fn to_down(&self, count: i32) -> Self { + Self { + x: self.x, + y: self.y + count, + } + } + + pub fn follow_head(&self, head: &Position) -> Self { + if head.x == self.x && head.y > self.y + 1 { + self.to_down(1) + } else if head.x == self.x && head.y < self.y - 1 { + self.to_up(1) + } else if head.x > self.x + 1 && head.y == self.y { + self.to_right(1) + } else if head.x < self.x - 1 && head.y == self.y { + self.to_left(1) + } else if (head.x - self.x).abs() == 1 && (head.y - self.y).abs() == 1 { + *self + } else if head.x < self.x && head.y < self.y { + self.to_left(1).to_up(1) + } else if head.x > self.x && head.y < self.y { + self.to_right(1).to_up(1) + } else if head.x > self.x && head.y > self.y { + self.to_right(1).to_down(1) + } else if head.x < self.x && head.y > self.y { + self.to_left(1).to_down(1) + } else { + *self + } + } +} pub fn run() { - let input = read("08"); - let lines = input.lines(); - for line in lines {} - #[cfg(feature = "part1")] { - println!("Day 9, Part 01: {}", "TODO"); + println!("Day 9, Part 01: {}", simulate(2)); } #[cfg(feature = "part2")] { - println!("Day 9, Part 02: {}", "TODO"); + println!("Day 9, Part 02: {}", simulate(10)); } } +pub fn simulate(num_knots: usize) -> usize { + let mut visited: HashSet = HashSet::new(); + let mut knots = vec![Position::default(); num_knots]; + + let input = read("09"); + let lines = input.lines(); + for line in lines { + if line.is_empty() { + continue; + } + let (dir, count) = line.split_once(' ').unwrap(); + let count = count.parse().unwrap(); + for _c in 0..count { + knots[0] = match dir { + "R" => knots[0].to_right(1), + "U" => knots[0].to_up(1), + "L" => knots[0].to_left(1), + "D" => knots[0].to_down(1), + _ => unreachable!(), + }; + for i in 1..knots.len() { + knots[i] = knots[i].follow_head(&knots[i - 1]); + } + + visited.insert(*knots.last().unwrap()); + } + } + visited.len() +}