diff --git a/2023/.gitignore b/2023/.gitignore new file mode 100644 index 0000000..a7518e3 --- /dev/null +++ b/2023/.gitignore @@ -0,0 +1,2 @@ +**/target +input diff --git a/2023/.rustfmt.toml b/2023/.rustfmt.toml new file mode 100644 index 0000000..7530651 --- /dev/null +++ b/2023/.rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 diff --git a/2023/1/Cargo.lock b/2023/1/Cargo.lock new file mode 100644 index 0000000..ae5bfe8 --- /dev/null +++ b/2023/1/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day1" +version = "0.1.0" diff --git a/2023/1/Cargo.toml b/2023/1/Cargo.toml new file mode 100644 index 0000000..a3c4e52 --- /dev/null +++ b/2023/1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day1" +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/2023/1/input b/2023/1/input new file mode 100644 index 0000000..c115585 --- /dev/null +++ b/2023/1/input @@ -0,0 +1,1000 @@ +qzjggk1one +two2seven7 +vszthreetwo6threethree4two3 +zcsvvlslqvfive11chhzmdjdgz8vbgldl +jjsmsksvbr77cfdrdseven1zd +947lkkgznlhxseventwo +8twohprsxmz47 +five11eight1 +seventhree1eightztszfourfivesix +cbcvd9 +hg91fourhhpvkrxbn +1mqbdkqxnine817four +btr4seventhree7hbkkg +4fivegtbqthreeeight421six +ninefivekd7 +9onetwofprmrhmonedb +threemtmjpfzqkdnlpl81one +six7eight5 +twonkhnine22fivep9two +qfqtkz9fivetst +rkzxrhvsgonerqqkpf7nined +fivezg8jmf6hrxnhgxxttwoneg +21nine9two2tfmfxjrvkznkjzs +rhcdx54 +jhnpkkp5seven +one8xrjsk9rrgkt +nine58 +1twohphpl +eight3pfzvrt9msgxtrbdclsix5 +874eighttcpzdbrlj +qlxkgtwo9 +ninedbvone1sixlnzdccpsixgzrfdnddmtgf +7eightppzmhmpn4bvvxp39sspf +four48rvhnzsnzmjxrl258 +342 +three2mmqxh391 +eight7vcmhfqznbccrvl4onehfqmjone1 +6kgchb4xggs75two +five6ntbssixvvtvs8spgdxl1six +one7one +27three2seven +rbmgfive4snq +four239threenineeightzrbvbgthree +two4one3 +sixcpcthzvczjp725 +fjninetwo6 +seventwonine7 +1twoonepzrjzsdfourdbdrxg1 +vzskvvbtjfeight2sltfdnine +oneeightfive3lthns49 +cxkqdgrftbhzsnine6 +onethreesixsix5jsj +43347two7 +threexfcvbv9qkthree3 +7qbpndqrzdzfjnineznpfrtkrjqthnkrpvcflfr +prcjmkhg9242eightjtcxmljchpchkkftqfk7 +four2bdhjzsfctwothree5 +1qkhgpms6two +foursixqgeight5b +8eightxtzp +21bcfkqgjndone +five56fivefiveg9six3 +seven1fivegrgcmp +prxgtm9threezfqsvpshjnrhbtthree +2eightsevenonenine2 +sevennine8vr +htssttpntwolpghms7dhqzpcssnqtwozxxj +4hggpthreefive +httwone6hhrvkknd +six2r828 +four42xhlgfour +8one8 +6twoseven8onextcj7zcl +lqx9848 +xdsvgtm8672sixsmlndlc2four +8vrvqdrcmx9 +one42nine +dstdftnine8three7 +hhpbcz3 +6tdzgbkdjmfourtworfmjnmlqs2three63 +5fivejbcjfqjnd34qcjkjvnnkrtwo86 +ztkbh8threezjfkcm2eight1six +gpkzlpfq4kghqlzqfkfivenine +gxnlcr6four92 +qoneight55five3 +brhzqvk6 +sevenlzvktjftcvfkvjzn6thlzf +2tnvkdzrtshfzxvzpc +mbndnhq3one9one2one3 +six11eightoneightklv +1lrtvfzszkqlcstmztkninenine14llmfive +1four69two8shhmd +five7xtvtlzpsfsz1htsjxzph5 +9seven4fcp +fqhbptgthdfnng6zzzksevenbchrtvtwo +two7712two +fdkbn4lhcfqcdjbfiveeight6 +foursjlrleight2five +nzfmprckbcdvnzzj4xcpngq6 +kczeightwo4fzshkzoneonesixfivegpnnrpsp +nine5fivethree +ninerjkcvrjr8koneseven23 +sncnb4gb9 +four3five21 +8seventwo +pknhg9jql51one4 +1threetwo6rfvsgjbhjlcfour +four7fiveseven3fourdqvsnkkdrbrhnfsx +7rcqcbqsfz766 +4hdzzzggfzs18eight2twoqmnlcxzxs +x8 +eightsixdqcpvsgzndqqone716three +6sixeightnsmkckmssfive12 +7threesixninejrbthreebqpqtl38 +five11nine9cljfjk83 +qhrgg313jjhzptpvnrjb +mm23 +onenineseventhreeseven5thxmgzlxm2 +threevhrfjnvv9sevenone +7onehnxssvbgeightsixone5 +sixone9sfsix +seven3five6nineleighttwo +6fiveeight3bxtt8 +tvtzseven3ljkqmnkprmsevenmdnpfour1 +twovjjnznn36fourpvjfourk +4zk2slkstxjxhr9 +6qzhhzgvpfoureightljcfxcjntwoseven +73p43cqqpjrsrmzgqzhtrl +32fivemxfdldrkrvstffive2 +fivethree3eight86 +six33rxgscqrpqtkjrqtwonet +3ssmlszjgtd8sixznrvstmbjqjthzkszx27pbsgkvsj +4eightnjvnvd57sixjtbrthmtlh +3eightsix28 +18mnrnlppvxdjg88 +qbnmt4fivepbqrlzkd7eight3twonehzh +5xkmvfive8two +72fourpbrsix48tg +8eighttwo28eight +68dvfxsix5trrftjmgsevenslnjvzggnine +zblphhsrhr2nine +7fivethree35ninet +sevensix1cmqgxjdpj3jqct2 +5fourtwogrpxkgxvtcfive1sevenjhxzb +oneseven9sixone5 +four4klsixnbkxmeightfourtwonekmc +2vzknhhhznx4one +kndone4three942lqxdxddfour +vrcmxdgssevenseventwothree9 +stxvhlsqb9jccxph8three38kbrxvkr +fjxhcbbczfmt7twoxfmdgxsps +5715threefgtsgrjb +254hnjrpsbbffxmvtjsjstkxkponeljcqhdll +qmvlxjjklmkhvmmjd7dfkztjvtnqfn +seven34five5mflmvnine6five +jtqdgkv3three +6four1fcmhbpmbknkpxthpcthreesevenonethree +three18frtwo +boneightvlgzbzrqzhrsfvsevenninenine8nqlzrcqslqxkn +7seven6sevenvrlnvqjdsix +hprgnr2hmgnjnvzleighttssseven42seven +fivethreevhscfxqrz42sixseventhreefour +9onettcqgvvgcpone +threeseven2sevenpxsjphvdzj51 +26onecmkcseven583six +nine3csevenpzhjxssxsix52 +2seveneightsixthree2 +7vxxfxjkfkeight69 +f3714fivedrzst +qvlrmrgdjsdsevenxjf1fournprtlbpngkqt +9four7rd549four +3xrnlfivehsnkzdgmfoursixninenine +two1onelkqgvceight +four7vbrkrcnine +twotwo8fshnvrzrhgksbf4two +1eighthdnrtpglhk6bfqtjqkgvcpztrf2 +93five6hrxc8six +onechldlktgq5xckxbtlhhszjmsjlsttxrncssffh4sccgxjz +fiveninenbkxzqtbqglx69 +xpkfldsggg3eightbqtbrfthd +1six1kk54sixnine5 +onethreefourfour99threethree +cbpgjltprj5 +seveneightfivezrsrseight9 +sevenbqpkone7six9 +2sixfour9fivehrksb2six +386mfggvhgksmnine1 +tlrkxpbnmmcjfzgbh697 +8bkksmcbnxsgzsixpv24 +four5qtjjcvnjnxfhcblfdgsssqmcqfive +3jppthreezkvjnxptwoonefourjtbxfllqf +sixtffm85ninefour +19seven +twocxxzkcv37kleightwod +dncbnpbch1sjjbdnfjqj +twosixjmljjllk67 +hggzqspzbllqeightzbckfllhtwoseven9 +tvsrsix9ninefivefour5twoneg +5eightwox +zvpkkrs734sj +88threekmzzbfsfxnrztbgkqseven5ltfcmzpxxvb +fivesix62twonine +xrcssevenp2 +seven6eightjkfqnrkntmgfgccrvjcninemsix8 +jcnnjbttlf7522fqxpcvd4sjq7 +dvqlsgd1szspcmfhmptnphk32five +93nqrmmbfbhvhkbjxcj6pzjb +4fourp +35522 +1462 +four5eightsevenxcqsevenqxxp +2pmthree +five73fivelkcfbxttlkxgqrq +7hcjtq +3c2 +stmtgdlxz9mm13mdkpn3fqmfnghmn +fivellbhrntmseven7vltvqrclblsxg +ninebrfbxcrsgxhmn61kndonegfbfrdseven +xbq5two9 +hrmsgltrlllrrnd3seven +fivexfs9 +6eightqvqdpxltsrldc6xrxjpdz14qxfnsvbzgj +sixfourfivershtwo9dfvnqkpzczpxgsfsgb2 +8sevenfive9twoonevmjcpklsvmsixbghd +sixgcdjdhgglhdv5 +dmxvdp62g6six +fsseven8fcqkl +6ninexkgqsglbninedcgtmsp4qhngtvs1 +62nine2threesix +m8rpxcjgcvtseven7five4 +kxx13 +plptcvbzh3fourfiveczxdrckjbg8twoninecrhfkll +8six8 +xlvngzxlrnhh3fiverf3 +vk3eight78 +eightone97six3jdztsg +4mmdsmdllrkld +twotdnlkbdqmqtwo1foursevenxbbtrfjztbthree +fivetzszgzbvtwoone6 +snine52bmvkdkeight9 +fourponelvrncjhkmv5 +onexvnine2xtjkjvmhpvkzrzczthree3two +5xbdg5klzcxxfsix +seveneight17 +gnxptlnhfr32 +vcnfhslqb1nine +sixfour7oneights +sninefour9three +ddhmpjnnchkkc1hlb +mzvsgpbp21four468 +four8s3kfqhlxfeightkjbfv +83threexgsvhrnvfive +dhqtdfcl4sbvthreeckqr +1nineptcsbtckgrgeightjjcn82xtkzmjhnsb +bprldcvpqx94onezmcgntk6 +816 +sixone9nineeightclrvctwojfjskplgtx +twofourpqkvbs8kgtdrsqd +hgqxn9psdeightpmpqtrzpncxx +lvk1nine9 +twothree7eightqqhzbhbmntpknmnvckfourhbsdqlzs +nine61one +759fkfnzgjkhvztssv39 +gfhnrfone16eight +threepqxzpeighttwoseventgvjvgl1 +dgthhx1 +shdffcqfxpvpfsv4hmknbfnvmt5seven +sixsevenkgdvdqbp7tfqjpjfive4 +ngvqc6three88nlnhzjndss7two +sroneight8 +77seven114sixgzrnlh6 +8fourfive7two1 +nccsjq6twothree14vzjnnrjbxjhkdcvmvzzc +six4hxrjdztttqssvhslpqjlxtqhgtqf +five4dlmdtsdxrg73one8eight +1sevensevenhb +vbnmfr3knfncgtseven +8sceightnine +x3two +hjhknjmbhnpsl1six2six5 +ninenine4b74eightthreefive +twofive5fhnnsq32mdflndlft7 +threesix7 +sixfivefivesixthreeseven84 +ghzgpxjqj4threeone2153 +3threefive5 +four5one59fivesevenzlpgnmjtqh +47jznslzfdgklndmpcjdksrqfg4threefourgctxp +kjk9pxsdgsheight6 +four6mbskcnpgkb +48nine +3ninekncjthtk9rbnmdmdrhrksevenpcpnnk +jgqqvd71twoqmzr +fr8six93nine3 +29sevenppgrb3pqbbrthreeseven +onetwo3fivethree +2nineninefourpstb72 +one6two +971 +nine4foureight1dsftjdrq +msgbxbbtb3pnlnjsixthreefoureightwobm +69mqfclsqhss +one166eightbtchdlppone +zpg29lnksjlsxqmtwo1k +76ninetwo56 +fourbpmgdl38qmnfrlntxzthree7 +jjxvqchjzscxq1vfmjvklg +psixseven57 +qxgqsfhf54 +zcjshnthree45hhpdfninevhrlhzxthree +sgkztgmsbs7ndtvpm8twofive7 +ntwonemktccptwotwoseven4one +2sixnine4nine +onelzjlkttzqthree34threethreeeighthrfzgzzbr +2eightxktjp6tmlrlmkpbcz +four974 +9ngnj +7fjrh9 +2fxpcdjcnm1two +ninerpbnjfbfourtwoone61lndjmzqfrj +8vklltm1pthreedtp +onemfmhgfjcbptwosix31 +lrzvkvvffvlfjgzcxgqpf769fvppsmfivegktg +five7sevennine +6vjcfhkf2bgjknzlfour +nine7eightwoczx +gccdbvjlzeightfour4fcvxdqg +hsvjdlhqxmq1575two +8rtlptb5xjptxk +4seven3hcpnqhzz3xxkjzltgjmpnmthree +threejlsninefour12 +bg915kmgdfzmqp4 +four2lsix8zjhmkq +tmjtwonesevenktwophvrsqpntwobg9 +ht8fhqqsvzpg12 +fgbd11eight7486 +vzrtrfiveoneeightsixseven8xdklgqj +ninejg34 +2zlrpcf +318fbtmxdlnbr7sngkjbzt +hgneightwogrfkcxpthree98threefourxshxvpbone +ninesix6onekzffggjtvh3ninethree +5vn +7dctbvpm +cvvpszdheight6 +sjtlseven42threemsdfnfive +km6zcxpzlhmd +3sixbfs +clkpxpmtrthree4one +bthreethree42eightqzjfnc +v955onezltllqmk +vrqkjpr1rkjmljn1fourninezpkfvvqsldxzlnl +sixnfivetchsccgr5s7fourgnsxqg +hqkgndxmdthree5npf76seven +six7grnnxhgg4 +jthree432mvppjpbk2vcrnthzzc +9seventwo48jpvvkgf +86jhtmsftsmkxpjgjvdqjchpmt +kmpqfnvstncrxq3hpbk +hfbknxzrmqlzsdk79 +jxdlfivemplvrsl9eight +931 +hcfssghtwonsjcdhbnfx42 +49sixeight +vpc2eight7two +3sixlqdzbf2nine54five +twoone92threenphjgdlztslfourvlfpbdqpvh +eight1347eightdhfdqnp +3fiveone761 +9hbgljt +sixlfivet9qtsvhlfour +six35two +2one3vhzfxjp68 +seventnqpllcxcxbzkznqs24 +1twoeightsix +five2sixnine975ffjtdslzvhmgzxmjp +four53eight +6fivesevensixbnpbqvfvkc91kkcth +4hdfvjblpmhhgvssmzhgtspjc +xtftqpvbfbzlgmrrvmctwo4cqhrdsqjnjone +hbqllzxhz5t +qrxoneight3sixlcsldhone +hjdsevenqffone5threeljqpdxxxs +one79hdbrttptvpfoursix9seven +ninetwoczt18threenine4vlcrcnmxr +h9fourseven8kbbfkxpmfll +ztrqpd88fjfive +1onetwoxprmpthreevbjmjzqgcms1one +9qgpzftfq +lpsninevdxdxpfbbm2 +41hdnzrrv7 +vhdbnkjhxbsmsix5zppbcrhhv +fourlnfive19 +threedjjkssbhsevensevenfive8vzsrprh3fcnlz +zkgp95znpqjdgtsclscf6 +9z3 +fnrkmtkz4 +sixfour1qpzzzrtcneight +67kseven +4lrqrhone +vxqtbgltwosptqhznnine7 +1tqcxmxhtdljmsklfzfb9 +eight2mqcmcxqzdjnpddqjptwocznvhzone +g7 +1gzmnlvrmlfscdbnine +18qjtq6nine +three9six85four +vrhfclpzteightdvjdsjhseventhree2 +bblhvdcjfqgfive6ninenine7 +8nrjbqnfkfiveqhbbc7lfjhsfour7 +cjkphrvninefour2nine6 +kscvmlxssevenonelqdrtcqcqthree3two4 +1333 +sfrzr9 +onez2eight7 +tbsxcmgmnb636nfcdsbj +2vvqhrczfnmngbpgtsnlfourfiveqzk +7gsskljk6bmgstmptsixcdjzm61jzgxbjrtl +fourvfph31 +tpzfive8 +fiveseven62 +six55sevenvrxdeightnine4 +pffbhkpvhtmsr3 +4eightninemzbkzmkd +jntwo4twofdgrl3fgtqmnfnz +6threetvzjn8 +5kvc82tksgsxvm +mrtwone3seventhree +8six7gxgfmpkheightxxqbshrn +fivevtbcgzmvrdkxnssneight2three +fiveseven2ndpkqtxxkninesix +one7jslcxd +7vfjpjfsxgcfbgtsbcnfjfvdql +35ninexmssjbzvflcsgjlgf4 +jfnknfxsrp6hpmnclmone6hzpnfnbhsfive9 +jhmmvkspvdltqcmqlqmbsevengsvfvxhct7vnjntseven +lzml9ninerbnpl3ltsclmjvfour +fourpzsbmv2oneeightseven +fourcptgpkm5onepkbpbmzhsnbftflkthreeznnjkrmqkp +vflqthspgjrxnvvone9fourfour +two4jtcqdrbmfs2six +1five2seven +seveneight8threerspnbsxhjkeightnblhlj +cxnhsl6three +eight96nineone1oneone +twotwo5ffngqkkjc +sixnine9ptxjjbhhpxmxllvd +69tkdpg +tgrtrd4 +7xj99 +6kjgvlfivedfourseven +sixjvbqzs9 +1336threeseven +3mzfgkcgrkm668 +eight639five +zzg25s2mccdfrfsthreeklsqnbgpzbjmgqlntc +np9twofive57four +nrkxxt9mbxfnbrjvfzf +55gfl +five4ninepnpdrfqonepblsnm1 +2svkthgrhgrkjjvcxxgjjlnm +3758rn6 +rghbnfjdz6kj +brnfvpdxrkhfsql216seven +xchkrlrn5rqfcgznxjnfour4hpb +three658 +72sevensix +6four569rmcnhcqb6three +fourfoursix8eightqkpvbns2 +sixh6fourfive +465 +8oneightmx +dkdnrtqxhx6 +m13oneonethreeldjsbrrvbg +5tqvlxnfournddkxcfchr3 +ninefive418lfgb32 +cfjbm18 +eight6jdgnxxmgg +boneight2seven1 +vltwone3eight2one +five1xf1foursix +ninecgznbgcrfseven8hkqtwo +vttwone6tjnftjsmkb +sixdfthkdcq84chsrtwofdjm +hgtvtqbgchlntttzht34twojpjzqh17 +6qqpsnkdscgzvplqpdqkbmtv +eightjbone3cpfvrrpvjeight +9twondldz +974 +2jtnd7oneccdhldpghxjp +54fkqhzjgssixtkhvtmkggtpgpn +four7twosixninelmmhzttthtprgd +qkjtzllg7mhvhrdmhlvcjxkqlvplfeightk +xcndxmmqnj6bzhkx +m9one +nzcdtnk8one +onesevenvspvtnlx829one +32fbfbpblxjlsn +7tkgxjdk598xbkxzbt1xqgzppfcrh +zbffthreexqgtzpeight11csbcvrdsdnjxrf +twonine6sixnine +sqlzbx8llrvtzmgdjfrtvjxctp +5nine95nineonethree8 +three814fourb5eight +tcgrxfxjqthreemdzj1 +kpfbjlftxzcvqjmbtbjhzsnhknlcrdntseven8 +8572xrjm9bzrnzz +klkdgszqphbdt4 +seven69 +nineseventwosixseven6fourone +hkmhpdlxrxtbljnlq6twovszpngfdx5dpktd +nfcxsr5eight63one +8eighttwonine5five7eightwokv +4nkv +6klbcjh9rnhvrznrtvkktnhone +597twoseven +xlrfzngpffhjc18 +nine57seven3xfhs +377 +fivefourcxdfgbtnhmscdlsrnnljvgjlthree1f +4hsgqrzthtk +3ninez24 +k9qgqsbbqdsj44tr +djcsxdmd6jrgtmxpk5onegrljnflbhmnnx +sxxrhpkpclbjms3jq +8ngmpfiveoneninerpbscdgjxztkzldcjbrk +637zbrfpsvhj2hzmvx +4eight6chc6 +5one9nine +7sevengqrqztfvmhkqnjveightl2twovtgdlhv +twosixeighteightmm62 +zlbtcjgninevbctrmsqkqkkjmvcfthnfvnl4 +fivesjmgr7two +twotwofivexmsqvjv6four +7fiveeightsixnine +ptkdvqjbniner3five6 +68threepkzgtdhmmznine +5jhmzcznnppzpzvmtmn +onethree2nine +9six9 +fivek4qhqpdd +ninexqrfqddhsix96eight +one1nxzzlj +four6gjkhkmqvq3jn +6scthreedtsqfzdjdpb614 +77tqrbpsrkvvf +7ncjgtb854 +threeggknmtwofour9kjlnfkhfive +t5trbxmkss17qvbdbl +jjftkk5 +xqkxrsevennrlqqmcdtwo6srsxdmqffx +3six4 +bssbzmgdvs9qnsfznvone8seventfx4 +fiveckftrzxhmtwobrlgzeightkscfxzqqvm3five +pngggbvtpsfkjcnine18one +zkgg9sevensixhlxb3 +gxxgrmqfbrsvm3xdpzqzhjthreefivemz +9sixshcpfivetblntkvqrfcrqbgxcvgzxdq4 +1prnmqrteight9three6twozvbmhttzqzhjmxxg +191mjpnddq +4gtddmzprpvgrgbczcxbz +2onesevensixbmsmstbqeight9dxtjkndsz +mtwoneeightmdseventwo24qn +two69bmvplcfhgxqxzbnb32 +282seventwo1582 +19cmqgjn +8ninesxkxtjbmsssevenmtwoptlsx3 +5ninefourpc68eightonefour +nineoneseven3four7 +83fivevlndzxsix4njnine +4ninengdkgtt4eight +threenine1 +461q583 +37mtppzmdtonedfrgxxr +dbkqbcpgjgonejbrsdvgfmgftwo8 +four52bnine6 +17 +9mmjzvlgnk6 +1three3twonett +123threesixklzxonesix +two5ninebcsrkvldtkfseventx14oneightt +eightscsbpjfvzqqzvphgqb5fivermgpnztwo +zlvgbkztbzbbrfive9jqp +eightphzrnbdxshhhkvtv8 +mqbqlznsr6 +87twobrcxzxgrjqs97pskfour +fivecthreekfrmsjvcmlbrcjxjf8four +vgscjjrxtwo6llhdnsevenpb +jrkrbndlphnnfvstthree6ninespchtjzht +flcffqpthznrhmxklztqtdqhone2 +2811fourseveneighttwokqsfzvgn +8nvrdhmlnrjqsixpjdbrfrp +thnpqfntttmbbjeightfourxgcone4 +5leightkvsjfjttxlrpnmjxzxsevenlhc +eightqrqvtcclqfive42 +5threesix +qrpfive2mlhgctblhns6sixglnllttvdtwonekls +6six8two +4fourninerrppmzxlfdsixbzvdjljsx4 +rjfpktwo7nndlnmsixxtbngv +lpgfs9eight12krtktzsix9four +3one1x +pjnvpbbb3 +5nine1ninejxccxrrcdf4 +gdnmpnineldhxrtpxbfn8zkmcpgpz4 +r821sixxktbpncp +7glxnlfrvlqgkfour +8three28 +fourtwo3twopllxrjtcthree4 +txhrhxpbg6 +psfrrdvdglrgb9 +nfjnshb4seven764xmrkhfgpgtsmsix +2vflvfgkcktqlqrktkx3eight8h +hmzhxctkv8tftvzxkoneightdb +cnklbqdseven6kdrvbjthreeone48fiveeightwoj +sevenfouronetwo5323 +tworpfmhd12 +tlconeightjcktcngts1eightmxnsd6qgzlvrg22 +klrpbs2jxmfdhnzmbtwoqmjfive +mdzptfm94mqlscksrmh +spkzfm9six2zjgmpbm1vlktblxqnn +blfxjrrcjx4four84xb6 +bbsbpgskffourrfbhgblhvxlhxbms3 +lhgtjmone6878 +qvlvtwosevenxchqcfivefjbgvgstptbv5 +eightff6fhl6 +three3vsix1dxtmtwo +79vrsng6eightnblbrhkbk63 +nineqlxxvpxvzgsdhsix6 +kpjsvdlrfg2drhmqtthreenineseven9six +3nlrkddlvlslnrms +8b3foursixrjrd +7gpjclsfljhnndseven2twonine +cxxtx79onetwokgdbh +bfllxk5t +42fivefivemtcnmftwom +dsrfcc2rjb1nknjlfone +1onethreejnrgmfzbeight277 +rxhrjb189nine +onegd6threeone +spcrbvcdgkgtkeight4fourj2ninekfjg +leightwo54dgvslxlmr49drfcpkxglt +fv72xkkqltqfour +one95gcspknineonejbxeightsix +7onetwothree47 +twotwo8 +9pprfftptwok +two85 +cppthree136ljvgmzjsix +twoppqninejtjlnzveight2 +4mdnrggvjq7jxst59 +twotwoone6xjsxdtmpvl +six4flbq +seven3three4jskfivesix3zjbnktkb +fourzzg8four31twogrsbjmhdj +hrbcmsqq2sixsix +one87fourtwoqshfntzqmfourfdkg2 +8sixsevenfjsgxeightoneseven6 +sixgkmhdprrsdcnrp6 +rfleightwo7ctrrfourjszmgsbxdsevenfive48 +fpcktkstbxclhttfmdjmqgspbdpt5six +six3stptnfbrnrfivekxntrsnnrf +qqnmvdnp7nine9bnkqkxglk5 +fninepczvnine6 +fivesixthree1foursevendvsdrlrxnjsnlbchs +ninetwo4eightnine +fiveninepcsdhmvtvrzvctd5gcshznfsppbphhtcpeightwob +4jmlqvvrndnine8n9 +six8tjgm2vnine +nbf1zqdfrddsfsseven1sixznpcfour +1nine8gj78vhphqf8five +3sixonegthreeninepc18 +gsznrcseven4 +927four13h +rvzdfpgbdztvhd4two +seven4qnr +six1eight +9lkkb +3seven92ntrbtjggtrcrnffst +xhcpcdmfl5 +fiverpngvmzbxrqbtv49qhctgpcmlmbqhzsix +sevensix82fivexhrdfgrbdpp7 +lkloneighttwo4dfour8eight +four2threen2mpcqmfourrspvlbeightwobb +scqg125htncplb +32shmcone31fourxn +3583 +four5zzfphtvjrmmfgfzq4 +1one9one +nine9fourmvcfshnbonesevengxlhlmvghsltxhxzg +65hlhzm4brnine +jkkpthree6 +zpjkstn72rjjskdcgtwo4three2 +gvz73fourjzzbxmf6 +662nqsjsxzf7threesix +three9sixfive8pxdktmlqgchrfpdjpdv7four +19hsvtrpngkqvtrgcntbeightwovfp +zsgeightwo92foursevenonefive +fourthreexcztqhqtmd1sxzqtlhkvnmhcrgthree +dthftwocsnkrjrkkeight8oneqzfxfrxh +36414lxhkhtm +cqjxdlx9two578npkbx +2nine7eightvn8hzcxscgrm +seven3shxcpfdzhmnine6eightp85 +pmjfklprvxmfbgthreeeightpkdnskthrkmjmdnpgztqllbrdvt8 +7kdrjsrhxljmvqdrdgtcbone636fourone +892 +83jkvnxmcsixseven25csxd +13zzrplcqhxktbxkpkhdgb +cgclnmkl9 +twotwo1eight +seven23361 +72onepmvnzbnjnhv3seven9 +threeqmxcpvmhbpvtlvcponedlpcvlqhthree2 +vjszmfour69scgz +ldlqdbsxb2kzpq +four77threeone1 +lglpdjdjrl8seven9 +63nineeight +xgg9xhthjft5 +qzgk7sixlhc3sixzkqlrhsixnfl +7fourjpmhfclbklmqj +twofive7twothreercdqdjx +2clvxxknzjdsevensixllczroneqxrqm +mcdghvnbvdmqtbnbbvm8sxdpztffkkl +17seven +gj4seven74 +pdkhqcrkkpshxgxbxbq3vsevenvpgxggk +1six7onelhvfffpdkgpgggrk +46jtwonen +4eightsix6hscxf +j8gs5seven +4qntv5two3 +pflzqzrxkfive5fourninefrpp9six +seven18 +6eighteightthreeeightjltcgqfpttwo +eight6five +tmz9 +jpseven4 +smnpnlvcfqsdjhs94 +pmsseven7prmvp +nkdeighthxxbbvptwoonefive4one +zst7cxhnlbgcqfive77xmknmnjcdcbkbrctdj +eightsixjsevenplzkx34jfctccmdbt +kmkzsixthree84 +tjpn5xzrktqhlpmeight +vxp6onetwofourfive +meightwoninesevenzdtltdseven7pcgkqgcpsjbbqlseven1 +two28tnkxzldhfour6tbtctxsjone +2sevenmkmcrlkhfivertcknzkrtwo6 +rthree3seven +1fivebxkhzcdxsixqxldjmhhmlfivehhscj +sixeight2vjfsgrrpqkrkbxmqcntq3eight +fbpsgsgnpthreeb5two4 +9fxqxkrrs1qcqct +lg4onetwodkgd9 +njszseven55foursix +sevenone8 +four8sixvrtzgzbhgone2 +threeonetwo227 +tbfourrkmclvrjzd2bvf6 +27ttkxsqr +xzjf8sixrxdzzn +9qtmt6eightsvtcmtkcqcqsxfn9jtpzr +twodhmprpfvcfour747 +375jrn +cpvdponeklkpmsqslq9 +ttzvkqrjsqgbztzqrvk7ldthree35djbdgh +9lcfxtftnhseven1nine +6bthreecfptdcfsf65 +eightnine6nrrpsbqgggflnp91 +mslqqsnjlhqdffndcnlcttjmfeightfive8nine +kvfkrt7pkthreethreegt +ninez45fourf +fivefourlscfour1fbpone +8zqdqtzt6bv1fourtwo +lzszbvchjdgz93nine +mtflpsmqm74two8 +7sjqprggnzone1 +2ncl5gzvnrtgfgp6fourjrbnnmpmhthree +pbfxkfsone5qnlvplzseven4 +gkzmtz5jgsldqgl7mtbft4lkmkpvkfive +7lskftknzrtninexthhsnljms +vzrlnsjqz8531three272 +seven18bbnd +1hqmvhfbgff9nhlbfpqlsf +four2eightseven +fournine4gzjjfgdd +four34rlcmzmzhdmn +knprzpv5four9onetwonineeight +eightrtqcdnsgbbcxgzqtkscbpvnjzs49three +threethree82onezbqjzfxrqkkz8 +6vtqkkf +eightsix73 +42446eightsixsix +11threesix +5ninenine7seventhree +43seveneight58six2eight +szclshzxqq3 +29hsl21threevx5 +gbxpeight1 +7two7four7six +7hcnfclhcfk5 +fiveninen5five4 +eight11threethreeeight +62lbhqrzbblz +8four6 +seveneight8six4 +hlknnfkqfive6one9 +nmeight8 +two98zsevennpjsllnpbkd +4t +j94662seven +two8one96fivenineqd +7seven7nine9s +nineninepgbbrxsdfzsfkrmsjmdrc6khkghxtjcmcjsrk +bqppgvgfour8onedlkzjkfzsbnksixnine +82twojxlgvlrvlmone +mhjh4fttnqjkcteight6eight1blqbn5 +14bprmfvssfqnine18 +threesixmzsbjdkzrgseventwosix89j +1knlhqgvprseven39 +5oneseven +mctqcpseven1msrtlkdtwo2 +2threetwog3jdvxvxmrjtwo +twotccrchvh9fnxfouronefive6 +hdldjzkj26 +43kxqcjpkbczcxcfr4nrfpxxnp +mmxvfrpfive3 +lcvslljqsss5six84seven +84nine2sevenfivezd3 +5ninefxhdvthqninehzksqfgrgtone16 +three5533fivexqfm +5fourfive4 +fourldkdvvnrbrseven73mngqjf +bpsix4ninetzscrgq +jbeightwoninemsfcfzrsqtwop5eight69five +sixeightfourgthreegg8 +6twonine8fourvdtk +1fivethreetscbjltkmpkjrkbssxnhnsks3 +rpcgsqtxqvtwoonetwo4four +one7one3fzmfivethree +mkqzfhhxfive7 +sixsix5oneeightsix +l9fourfq +n6seven61eightfoursbcxl +six5gk +nfthreefour88 +four265sevenknmlxdxmqpsszznrz +1gqbjbhmghgqdjxxppfhtlnine +bvhvvhmq3fivethree3sixmfgf +vqgnqspseven66qlbgtzqjdxxxjmxmqr +4sevenfive6nine45 +htxpdcjfbfour5939mnhqsqcvn +qvqgppfvrktjncgkfshzvhcxfzvtvgtwo37two +foursixzsgx8eight +knxqh1jfskmzqfone +zxq998521jnpcvhll +3fivebsonefournine2eight1 +kcbcvone6six +four65fourlgzxone +dclkbndljtxrmltrbznine5 +bd1qjqhlgftrgsevensevenseven2sglrmx +grpdgfourpjzfourone75qtknl +nine66 +twomxqflmbjg7 +foursix2threeseven +36three2hvmgbqd +two8vlqnpone3oneninesix +5nine3r9 +sevenvpcjqtnzdpcscgrgdmghqhpgkninebvbpcv5six1 +fivemrzsixpzxvrpseven3rseven1 +vqlrmnxnpvlpmeightzmvthgc43 +pvttbbzvhfourninekptxsix8three93 +hdpkd2h77fourtsjlqp +kgnrsfrmt3tddtzxcdlheight53dfk +z1rqjkp +8prcvdbhvpl4three +rgmtnbmsf9threenfz8 +fivesvdn9seven7one99 +seven6tnqqkpzj +threeseven9 +1kkjfsgfmseventhree +ninesixlqpkvhzntwo6two8bplfhg +25tx7seven5 +14vrsdbpftdxfivefivefourtjdmgvblmfjjdjd6 +5pntcmsixfivesix4 +sixsixvcnzxbxkfour1fiveeight +spjrlfppdsixnvr3tlxcjvtgbjqrszvxv +9five9gnrqdhbhbbsj +sevenjqgdmdzsix4pzplgfzlmltllk1 +nmjdzlseven24one8pqqfsrkhb53 +seven1four +1gfbqptlvmzgvqrhtthreesevenrxjdslrfdzhx +ddnine5 +zbqrvctj5 +seven354ftrznzcqx41eight3eightwonsj +three989pvsqhhzvxnine +ktoneight477mkhkjzsbvninefjfive4 +85foursixsix +8bcnkh3tcqhneightktlpdmzpjdksjpfoneightqm +eight68dlvgjb8bbchjlxzvf +nine1nzcdvfgm +1eight1sdjnfpq +htninefiveeighteight8 +gtwo5 +9fournhpb33 +mqxsfjfive6nine2 +gchkthf2 +vkd6pmvhbbphtfsjpllvkp +llrfcqczjldmlvvrxeightnvjgkqblgj7six +nine2qb +pqknhcqr54onefourfive2 +qthree58 +9dnkqgjeighttcbbctlgdf +8fv5 +8fp5xghphxrq6fourbfninefive +nine9sixkqdhvpqeight +qzssjfkv8nrlnx26 +413rqbqmqlprthree8br +jzlsrkcsrlfkccslrfrgnq147plpcxrtwo +7twosixone6threedcct6 +zhhhr55 +5jjzqlpjm +ninetwo1 +kskmzfhscm4one +zdrjcmlxcx4mtvsltkm711bncbfnc3ngkhvzt +1six9 +3sixtwo9 +xqvsixonexdkvxpkpqhlrone93 +7sevenonevxlcgsfjgbjfztjxjljnbmb5prh +6zbrlxhxr1fourfivegfhnjscnine +5gzeight +twofourbsix17 +fcrzbninenine3 +qpksmzv5 +8kjkmvrggzglpmnnzcmthree +gjbfour7five +eight4eightnine +1onekbsjqjjhtwo +4zxrtfz +jcjlqxfbnvghf1 +two2seventhreeninefive +6twoclrbkh6nine4 +mnrrtf2fiveseven9xlflf +42drsxctvqsix +2six71sixfourtwo +5seven2eight +fhtsxjjnztwovqmxqtlxd1 +ttxqr9481 +6five8sevenfour2 +mjmlgn7seven5 +threefive4dzlkfone19ndpxxzjz5 +ggpdsnrqtxxt1hpshzkpfhb +91jdrmgzt68pnf +mdtmqvtz9sevenx8twoner +sckzbprzktwonine31bkmtbhmcs +fgvjgx5one7hctftvmbvk +fjlxdhqxn6ninetwoxthcbhmxnine9zzcfqbsxqk +43fourthreesjldzsonesix3ndm +8twofivexpvdjljffpcnbjtkninefive +hfbcbhbxgzjqjspxlpnbppjsfcvqvkgxd22 +ninezzrg4 +4hfkdjkgdfsix +5onefive2 +94crrdpjlzztjb6 +sixktcznlmccmb8 +hgpxpvphr589d +mvsqtsks2772flfkgzd +7ninefivesvglcxk +2knkmjqkpt8gfzvklcpseven7five +two1onetwothree +three3sevenmdhvb +jrmzfnvvhbfour297 +ninebkcpfkzdone1 +11eighteightkdmmzth +8bnhlskcvtrnpf43jcfhfxf +134geight +5kfmjnseven3two3 +seven3four4onebdzznjngqbqg +fpxmmbthreeninethreefour3six +9twoz83jxjt +dnpqsmzvlzsppf46 +five8onexsgnxtdtwonecl +8gpbddqqpnq1onetxmhshjdpmnine +dhqft2three8twosixppsdxrxrzfb +1xjnzbspfournseven +cjcfkzfive45zjkrlvnz8 +sixrzmqrkkd5fqqf +sixkxtpcjmrmvvqjdgfttd6 +threehpbsevenffnqgdjcftjkdjhhk7dvzmkmqthreefflb +6npp7fivebghnnxnqjxhhxfdqpsixfour +ffour87fqrvqxqlqrrk +6ninerzksfbszmqnvgmtqonesixjzsf +sevenszl3lkqqfhlrdmkxgggvmkb +5c1 +vrxbfckfz7qlfrnggsbdfive6glhk62three +7eightfiveeightrdnhfnqeight5 +5threethreerdlsfdsvggzbonecxqzqdvxcqtqbg1 +gttsix2567 diff --git a/2023/1/src/main.rs b/2023/1/src/main.rs new file mode 100644 index 0000000..308e0d8 --- /dev/null +++ b/2023/1/src/main.rs @@ -0,0 +1,85 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PROBLEM1 solution +fn problem1_lineval(line: &String) -> u64 { + // forward iterate to find the first numeric value + let first = line.chars().find(|c| c.is_digit(10)).unwrap(); + let last = line.chars().rev().find(|c| c.is_digit(10)).unwrap(); + + (first.to_digit(10).unwrap() * 10 + last.to_digit(10).unwrap()).into() +} + +fn problem1(input: InputIter) -> u64 { + input.map(|line| problem1_lineval(&line.unwrap())).sum() +} + +// PROBLEM2 solution +const DIGITS: &[(&str, u64)] = &[ + ("one", 1), + ("two", 2), + ("three", 3), + ("four", 4), + ("five", 5), + ("six", 6), + ("seven", 7), + ("eight", 8), + ("nine", 9), +]; + +fn problem2_numeric(start: &str) -> Option { + let char = start.chars().nth(0).unwrap(); + if char.is_numeric() { + Some(char.to_digit(10).unwrap().into()) + } else { + for (s, n) in DIGITS { + if start.starts_with(s) { + return Some(*n); + } + } + None + } +} + +fn problem2_lineval(line: &String) -> u64 { + let mut first = None; + for pos in 0..line.len() { + let (_, sub) = line.split_at(pos); + if let Some(n) = problem2_numeric(sub) { + first = Some(n); + break; + } else { + continue; + } + } + let mut second = None; + for pos in (0..line.len()).rev() { + let (_, sub) = line.split_at(pos); + if let Some(n) = problem2_numeric(sub) { + second = Some(n); + break; + } else { + continue; + } + } + + first.unwrap() * 10 + second.unwrap() +} + +fn problem2(input: InputIter) -> u64 { + input.map(|line| problem2_lineval(&line.unwrap())).sum() +} diff --git a/2023/10/Cargo.lock b/2023/10/Cargo.lock new file mode 100644 index 0000000..5f8ea61 --- /dev/null +++ b/2023/10/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/2023/10/Cargo.toml b/2023/10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/2023/10/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/2023/10/input b/2023/10/input new file mode 100644 index 0000000..56bae37 --- /dev/null +++ b/2023/10/input @@ -0,0 +1,140 @@ +F-|F|7LJ7F7.L-J7L-L-7L-J7.FF.F|--J.L-7--7.L-7FFFF7-F|---F--L-FF-FF|.L.F|77|--7-.FF7-7--L7.-7F-7FL-7FFF-777FF-L-L.L-F.|--7.77-LF7-|F--|---777 +|-7FF|||F7J.FJL|.L7LL7F|J7.J.|.|JF-7LLJJL-JJLFJLL7F||.FFJ||.LFJF7.LJ.F7|L-J7J|.L-JF7|F77F7J|7.LJ|LFJLLLLJ7F|.L-.-F-J7LJ-7FJL7FLL7L7JJ..JL-7| +F7|-LJF-F--LJ-FF7L|7FJJLF7FF-J-|-F.L.L7LLL7|F7JLF---.F-LJ|F-JL7L7-7.FLLJFF-|L-L7JJJ.--LJ-JJL--7L||F7.|-J.FFFJ.|.LL7L-.FFLJJ-JL-FJJL-77-|JLLJ +-J|.|F7LJ..|LFJ-J7|J|J-||JF|7||..|-7F7L-7.|-LF7.F7|7.-J|LF|---J7|||F7|L|-|.|F.LL-7-|||JLFLF7JLL-L-LJFF7-7-LJFFL..FJ.|.FFJLJ|JJ7|7L7J7--J.|F| +FFFJ||J.LFJJLJ7.J7|-77-L|-FL-L-F.LL|LJ|FJ-FJF||-F7F7-|FF7FF7J7|--J.-.J-L-FFLJ.FL7L|.|L|FJ.L|.7J|LJ|.||L7L7J.-J||F77F77L|J7LLJ.FL7J|LL7FLJFJ7 +J7.|-L-|-LJ|7.|-L-J7L7-|F-7.7|F|.|JF7.LJL|L7F||FJLJL-7-FF7||-7LJFJJ.FJ7J.LJ7.7JLLF7L|7L|LF-LFF7-.|F-LJJF7LJ-|.7J77||F7J.|J-F---J|-|7||F|-7-J +|L77J-7J77|-|--..|L7FJ||L7LJLJ||.FFJF-L7FF-F-J||F----J-FJLJ|F77L7|.7LLL.-J7L-J-F-JL--7FL.|-7||||.77JFJ-F|L7.-LJL|7LF|J|FJ7-LJ..FL--.F7.7-LF7 +||.---FJF-|.7J.L-77.F.FL7.-J77L|7L|FL|J|-J.L-7LJ|F77|7.L--7LJL-7JF-F7JF7J.777L77JFF--FF7-LJFF7F--7..||LLL7L7J-7JJ|7|L|-7F7.|FFJ||-J-7JFF.FL7 +LF-L-F|7|.7.F-FJ-|---7||.FL--JJJL-77FF7|FJ7..L-7||L7JF----JF---JFF7||JF-7-L77-J--|LJF-7777FFJ||F-J--LJ.L-L7JJF-77LFL-7-|-J|-F7JF.F|-J|7L-7-| +-|7.-LJJ-LL.F7|.|..|-LJF-7|7.F|.L-LLFF-7JFFF--7|||FJLL----7L7F7|FJLJL77.|J-F77J|-7JL|FJF7F7L7||L-77.|J7..|F7-F7||-F-FJ|J|FJF-J7|7JLJJL|LL-7| +|L-7L|FL-||7|L|J7F|77..LFJF---L-|JF7FL7L7F7L-7LJLJL7F7-F--JFJ||FJF---J.F|FF||J.F--7FJ|F||||FJ||F-JF7F--7F-J|F77---JJF-F-F|J|.LFJJF.|LJ.L7L-J +77JFF|-|--7--J.|.FJ.F7-FJ.J7|.|L7-|FF7L7|||F7L7F7F7||L7L--7L7||L7|F----7FF7||-FJF-JL7|FJLJ|L7||L-7||L-7|L-7LJ||F|77.|J|LF|F-7JL-7J-L7F|.L7FL +L|.FF-7JJLLJF|-7FJJ7L7F-77F-7FL-F-F7||FJ|||||FJ|LJ|||FJF7FJFJ|L7|LJF---JF|LJL7L7L7F7||L-7FJJ|LJF7LJ|F-JL-7|F-JF-7-|-|-J7LL|FJ...77LFL|J7LFJ. +7.|L77L77.F7-J--|J.|FFL7L-J-JFFJ|.|LJLJFJ||||L-JF7|LJL7||L7L7L7||F-JF7F-7|F-7L-JFJ|||L7F||F7L7FJ|F-JL-7F-J|||FJFJ-7J|77|-L|L7F7F7----LL--|.F +--7-|JL7-.F7JF-7LL-.L|L--77|.|-F-7L---7L7|||L7F7||L-7FJ||L|FJFJ||||FJLJFJ||L|F--JFJLJFJFJ|||FJL7LJF7F7|L7FJL7L7L-7|-|-||F7|FJ|LJ|||.L|.L-J-L +J7FFJJ-LJ7||FJ.F7||.FJJ7F|--7JLL7L7F-7L7|||L7LJLJL7||L7||FJL7L7LJL7|F--J-LJFJL7F7L--7L-JFJ||L-7L7FJLJ||FJL7FJFJF-J7F7-F7|LJ|FJF-J77L-|---|.J +FF7L7|-J.F--FJ7LJFF-J7LJ-JFL|7J|L7||FJ|||||LL7F--7L7|FJ||L-7|-|F--J||F77F7FJF7LJL7F7|F-7L-JL7FJFJL--7||L7FJL7L7|F77||FJLJF7||FJF--7J.--F-F7. +7.J.L--L--JFJLJJ7L|JF7.|.|J-|J|F-JLJL7FJ||L7FJ|F-JFJ||FJ|F-JL-JL7F7|LJL7||L-J|F-7|||LJ|L7F--JL7|LF7FJ||FJL-7L7|||L7|FL--7||LJL-JF-JFJJ||F7-- +--L--JF|7||L-F.L77..||FJ-LJ.77-L----7|L7|L7LJFJL-7|||LJFJ|F7F-7FJ||L--7|||F7-|L7LJ||F7F7|L7F7FJL-J|L7|||F7FJFJ|||FJ-F-F7LJL-7F--JFJ7-F--||L| +|-L7F|-L-7|L-JF7-F7-J77FJFJ.F7-|F-77||FJL7L--JF-7|L7|F7L7LJLJFJL-JL7LFJLJLJ|FJFJF-J||||LJFJ|LJF---JFJ|||||L7|L|LJL7JFJ||F-7J||7LJF||-LL-FJ-| +77-F.JL|L-|FLF|L|.F-|.FL7|JFJL-7|FJFJ||F-JF7F7L7|L7|||L-JF-77L--7F-JFJF7F--JL7|J|F-J||L-7L7L-7|F--7L7||LJL7|L7L--7||F7||L7|FJL7L7LF77LL-FJF7 +.|7LJ7FF7|LL-7J7L-7FL-J.|JFL--7||L7L7LJL77||||FJ|FJLJL7F7L7|F---JL7FJFJ|L77F7|L7|L7FJ|F7L7L--J||F-JFJ||F--J|FJFF7||FJ|||FJ|L7FJ.LFJ|7JFL.L7J +F|L7|-F|-77LFL-F77|-J7|-|.FF--J||FJ-L-7FJFJ|||L7|L-7F-J||FJ|L--7F7||FJ-L7|FJLJFJ|FJL7LJL7L-7F-J|L7FJFJ||F7FJ|JFJ|||L7|||L7|FJL-77L7L-77L-7JJ +L|JLJ--F-L-JL7JL-J.LFFFF7F7L--7LJL-7|FJL7L7LJ|7||F7||F7|||FJLF7LJ|LJL7F7LJL7F7L7|L--JF--JF-JL-7L7|L7|-||||L7L7|FJ||FJLJ|FJ|L7F-JF7|F-J77.|F7 +FJL7JJ.J-J.-J|7.|-|F-F-JLJ|7-LL--7FJFJF-JFL7FJFJLJLJLJLJ||L-7||F-JF-7LJL--7||L7|L7F-7L7F7L-7F-JFJ|FJ|FJLJ|FJFJ||FJ|L--7|L7|FJL--JLJL--7-7-|J +FLJ.7F7.|.FJ|-|7F.F7LL--7FJF7F7F7|L7|FJF7F7|L7|F7F----7FJ|F-J||L-7L7L-----JLJ||||LJFJFJ||.FJL7FJFJL7LJF--J|FJFJ||FJF7FJ|FJ||F---7F-7F-JJ7.L7 +F7F|LF7-JF7.7JL-JL||F7J|||FJ||LJ|L7||L7||||L7|||||F--7||F|L-7||F7|FJF--7F--7F-JL7JFJFJFJL7|F7|L7|F-JF7L7JFJL7|FJ|L7|||FJL7LJ|.F7LJFLJ||L7-L| +J7J|FLL7.LL7J.LJ7||LJ|F7||L7|L-7|F|LJFJ|LJ|FJ|||LJL-7||L7|F-J|LJ||L7|F-JL-7|L--7|FJFJFJF-JLJ||FJ|L7FJL-JFJF7LJL7|FJ||||7-|F-JFJL7F7F777LF7.7 +|JF-JLFJ.LJ|.F77FL|F7LJLJ|FJ|F7|L-JF7L-JF-JL7|LJF7F-JLJFJ|L77L-7|L7||L--7FJL---JLJFJ7L7|.F7-||L7|FJL7F7|L-JL7F-J|L-JLJL7FJL--JF7||LJL-7-||.L +|J-LJ7LJF-77-LF-F|LJL---7|L7|||L---J|F-7L7F-J|F-J|L---7|FJFJF7FJ|FJLJF--JL7F-----7L-7FJL7|L7||-||L-7LJL7F7F7||F-JF-----J|F-7F7|LJ|F7F-JF-J77 +|.L|L|LF--J|L|J---F-7F7|||F||||-F7F-J|F|FJL-7|L7FJF---J|L7|||||FJL--7L7JF7LJF7F-7|F7||F7||FJ|L7||F7L7F-J|||||LJF7|F-----JL7|||L--J||L777JJFJ +L..F7L-..|-|F||.|7L7LJ|FJ|FJLJL7|||F7L7LJF7FJL7|L7L---7L7||FJ|||F7F7L7L7|L7FJ|L7LJ|LJ||LJ||JL7LJ||L7|L--J||||F-JLJL----7F-JLJL-7F7|L7L7||FL. +L---|-LJFL-FLF--7F||F7|L7LJF--7|||LJL7|F7||L-7||FJF7F7L7|||L7|||||||7L7||FJL7|FL7FJF7||F7||F7L7FJL7||F---J|||L7F7F7.F--JL---7F7LJLJ-L7|-7F-7 +LJ-LJ-|FJ--.L|F-JF7LJ|L7L--JF7|||L7F7||||||F-J||L7|LJL7||||FJ||||LJL7FJ||L7FJ|F7|L7|||||LJ|||FJ|F7|||L7F7FJLJFJ|LJL7L-7F----J|L7F77-L||F-7-J +|..L..-|L77F-JL-7||F7L7L----JLJLJFJ||||||||L7FJ|FJL-7FJ||||L7||||F--JL7||FJ|FJ|||FJ||||L7FJ|||FJ|||||FJ|||F--JFJF--J.FJL----7|FJ||77|LJ7LJ|| +F|7-JJ--7F-JF7F7LJLJ|LL---------7|FJ|||||||FJ|FJ|F7FJ|FJ||L7|||||L7F7FJ||L7||FJ||L7|||L7|L7||||FJ||LJL7|LJL--7L7|FF7FJF--7F-J|L-JL7F7.|JJFL7 +||F7|JF7FL7FJ|||F--7L-7F7F----7J||L7||||||LJFJ|-LJ|L7|L7||FJ||||L7||||J||FJ|||FJ|FJ||L7LJLLJ|||L7LJF--J|F----JFJL-J|L7|F7LJ|FJF-7FJ|L7|F-7J| +-LLJL-|F-LLJ|LJLJF-JF7LJ|L---7L-JL-JLJ|||L-7L7L7F-JFJL7||||FJ|||FJ||LJFJ|L7|LJL7||FJL7L-7.F-J|L7L-7L7F-JL----7L7F--JFJLJL---JFJFJL-JFJ-|JF-7 +FJF|-LL--|F---7F7L7FJL7FJF7F7L-------7||L7FJFJFJL7FJ.FJ||||L7LJ||7LJF7|FJFJ|F--J|||F7L-7L7L-7|FJF-JF||F7F----JFJL---JF--7F---JLL-7F-JF7J.|FJ +L|-JF|J---L--7LJL7LJ|FJ|FJ||||F7-F---JLJFJL7L7|F-JL7FJFJ|||FJF-JL-7L||||-L7||F--J|LJL-7|FJF-J|L7L7F7LJ||L-----JF-7F7FJF7|L--7F7F7LJF-J|FFJL7 +LLLJFJF7JJJ7FJF-7L7F7L7|L7LJL-JL7L----7JL-7L7|||F-7|L7|FJ|LJFL-7F7L-J|||F-J|||F--JF-7FJLJFJF7L7|FJ|L--JL7F7F---JFJ||L-J|L--7||LJ|F7|F-JFJF-J +LFF7L7L|7JFFL7L7L7LJ|FJL7L-----7L-----JF7J|FJ||LJFJL7||L7L--7F-J|L7F7|LJL-7|LJL7F7|FJL-77L-JL7|LJFJF7F-7|||L-7F7L-JL7F7L-7FJLJF-J||||F-JFJJJ +.-|JJFLF777F-L-J7L-7|L-7|F7F---JF------JL7|L7|L7FL-7|||7|F-7|L-7L7LJ|L7F--JL7F-J|||L7F7L--7F7LJ7FJFJ||FJLJL-7LJL---7||L7FJL---JF7|||LJF-J|FJ +.F|JLF7|L7-|.F7-F7FJL--J|||L----JF-----7FJL-JL7L--7LJ|L7|L7||JFJFJ-FJFJL----JL7FJ|L7|||F-7||L---JFJFLJL7F7F7L------J|L7|L7F----JLJLJF7L-7JF| +-FJJJ|||FJFF7||FJLJF7F-7|||F7F7F7L----7||F7F77L---JF-JFJ|FJ|L7L7L-7L-JF-7F7F--JL7L-J|||L7|||F7F-7L-7F-7LJLJL-------7|FJL7LJF-7F7F--7|L-7|FJ7 +F7JF7|LJ|-FJ|||L---JLJFJLJ||LJ|||F----JLJ|LJL----7.L-7L7|L7|FJJ|F7L-77L7LJLJF-7FJF--J||FJLJLJLJFJF-JL7L-----7F-7F--JLJF7L-7L7LJ|L-7LJ.LLJFJ7 +FLFJ|L-7L-JFJ|L------7L7F7|L-7|||L-------JF--7F--JF-7|FJL-J|L-7LJ|F-JF-JF--7|.LJ.L-7FJ|L7F-7|F7L7L7F-JF--7JFJL7|L-----JL-7L7L-7|F7|F7-F-77|. +-7L7|F7L--7L7L-7F7F-7L-J||L--JLJL-7F7F7F7FJFFJL-7JL7LJL--7FJF7|F-J|F7L-7|F-JL--7F--J|LL-JL7L-JL-JFJL7FJF7L7L7FJL---------J7L-7|LJ|LJ|FJFJ.F7 +LLJ|LJL--7|FJFFJ|LJ-L---JL7F7F---7LJLJLJ||F7L7F-JF7L-7F7FJL7|||L-7LJ|-FJ|L7F7F-JL7F7L----7L7F-7F-JF7LJFJL-J|LJ-F7|F----7F--77LJF-JF7|L7|F7|| +|LFL-7F-7LJL-7|FJF--7F--7FLJLJLF7L-----7|LJ|FJL--J||FJ||L-7||||F-JF7L7|FJFJ||L-7|LJL7F---JFJL7||F-J|F7L---7F--7||FJF7F-J|F7L--7L-7|LJFJLJLJ| +7|..FJ|FJF-7FJLJFL-7LJF7L------JL------JL7FJL7F--7|FJFJL7FJLJ||L7FJL7||L7L7||F7L-7F7|L--7FJF7|LJL-7|||F77FJ|F-J|LJFJLJF7||L---JF7|||FJF7F7FJ +|LF-L-JL7|FJL------JF7|L-7F-7F----------7|||FJL7FJ|L7L-7LJF7J||FJ|F-J||FJFJ|LJ|F7||LJF-7|L-JLJJF7FJLJLJL-JJ||FFJF7L---JLJL-7-F7||LJFJFJLJLJJ +JJLF--7FLJL-7F-7F7F7|LJF-J|7LJF-----7F--J|L7L--JL-JFJF7L--JL7||L7||F7|||FJFJF7||LJ|F7L7LJF7F-7FJLJF7F7F----JL-JFJL7F------7L-J|||F-JFJJ-LJ|. +|F|L-7L--7F-J|FJ|||LJ|FJF7|F--JF7F-7LJ-F7L-JF--7F7|L7||F7F7FJLJJ|||||||||FJFJLJL-7LJL7L-7||L7|L---JLJLJF----7F-J7FJL-7-F77L---J||L7FJJJJJJJ. +L7F|FL--7||F7|L-J||F--JFJLJL--7|LJFL---JL7F7|F-J|L-7||||||||F---J|||LJ||||FL-7F7FJF-7L7FJ|L-JL-7FF7F7F7L---7|L--7L--7L-JL7FF7JFJL-JL--7-|-77 +L|J|FLF-JLJ||L--7LJL---JF-----J|-F---7F-7LJ|||JFJF7|||||LJ||L-7F-J|L-7||||F--J||L7L7L7||FJF7F-7L-JLJLJL-7F7|L-7FJF7-|F7F7L-JL-JF------JL7L|7 +.|F-FFJF--7|L7F-JF---7F7L---7F-JFJF7-LJ||F7LJL-JFJLJLJLJF7LJF7||JLL7FJLJ|||F7FJL7L-JFJLJL-J|L7L7F7F7F7F7LJLJF7LJFJL7LJLJL7F7F-7L-7F7-F7||-J| +LJ7FLL-JF-JL7|L7FJF-7LJL----J|F7L-JL--7FJ||F--7FJJF--7F7|L7FJLJ|7-LLJ-LJLJ|||L7.L7F7L-7F---JFJ|LJLJLJLJL--7FJL7FJF-JF7F7|||||-|F7LJL-JL7|.F| +|L-F-LL-L7F7|L7|L-JL|F----7F-J|L----7FJL7|||F-JL--JF7LJLJFJL7F7|--|L7..|LFJ||FJF7||L--JL----JF--7F7F7FF---JL7FJL7L--JLJL7LJ||FJ||F-7F7FJ777J +J.|L-7|.|||||JLJF---J|F---J|F7L----7LJF7LJLJL-7F-7FJ|F7F7|7.LJLJ|F7LFF-LLL-JLJFJLJ|F-7F7F7F77|F7LJLJL-JF--7FJL--JF7F-7F7L-7|||FJ|L7||LJF7.|F +LLJ7LJ7-LLJLJFF-JF---JL7F--J|L----7L--JL7F7F7FJL7LJFLJ|||L7-LL|.7L|JLL7.|-|.LFJF-7|L7LJLJLJL7LJL--7F7F7|F-J|F----JLJJ||L--J||LJFJFJ|L--J|-|J +FJL7.L|L|.LF--JF7|7F77FLJF-7L----7L---7FJ|||||F7L-7F-7LJ|FJ7.LL-JFJJ7-LJ--LF-L7|FJL7|F7F-7F7L7JF7LLJLJLJL--JL-7F--7F7|L7FF7LJF7L7|FJF-7FJJ.L +LF.L77JF-.FL7F7|LJFJL7F-7L7L77F7.L---7LJFJLJ|LJL--J|FJLFJ|J7..FLLJ.F7.7JJ||J.LLJL--JLJ||FJ|L7L7||F7F7F----7F-7LJF7LJ|L-JFJL-7|L7|||FJ|LJJF-7 +FF-7L|F-JF-LLJLJF7L-7|L7|F|FJFJL7F---JF7|F--JF-----J|F7L7|-L-F77|L.FJFJFF-|.F7LF7F-7F7LJL7|7L7LJLJ|||L---7|L7L--JL--JF7F|F--J|FJLJ|L7--LLJ-7 +F--J--LJFJLF7F7FJ|F-JL7||FJL-JF7|L----J||L---JF7F7F-J|L7LJ7J..LL-JFF-FLLJJL7-|F||L7|||F--JL7-L---7||L----JL7L--------JL-JL-7L|L--7L-J|JLFJ|| +|-|-7JFFJ7.|LJLJFJL7F7LJLJF7F-J|L------J|F----J||||F7|FJLF7|-7-|L|L7--J|LJ|L-LL|L-JLJLJF7F-JF----J||F7F---7|F-7F--7F---7F-7L-JF--JF7F7.|FF-- +J7FF-7FJ||FJF--7L-7LJL---7|LJF-JF--7F---JL-7F7JLJLJ|LJL7FJ|F77.J--JLL|F7L-LJ..LL-7F7F7FJLJF7L-7F-7|LJLJF--JLJ-|L-7|L--7LJFJF-7|F7F||||.|JL|. +|LL-|-7F--L-JF-JF7L------JL7LL--JF-J|F--7F7LJL7F7F-JF-7LJFJ7LJ-J.|.F.J-J-J|F77F--J||||L-7FJL7FJ|FJL--7-L---7F7L--JL7F7L-7|FJ||LJ|7|||L-7-.|L +7.|.|JL|J7LF-JF7||F7F------JF-7F7L--JL-7||L---J|||F7|L|F7L7J777.7-L7.L-7|7J|LFL---J|||F-JL7FJL-J|F-7FJF----J||F7FF7|||F-J|L-7|F7L-JLJF7|LFFJ +|J-F7--J.L.L7FJLJLJLJF--77F7L7|||F--7F-JLJFF7F7|LJ||L7LJL-JJ.77JJ.F|.LF-FJ.F7.F----J|LJ|F-JL----J|FJL7L--7F-J||L-J||||L7FL--JLJL-7F7FJLJ7F-7 +|F|.LJ||FJ.FJ|F-----7L-7L-JL-JLJ|L-7|L--7F7|LJ||F-JL-JF---7..7J.FJ-F7L-.|--L7-|F-7F7|F-7L------7FJ|F7L7F7||F-J|F--J||L7L-7F7F---7||LJ.|J7JL- +FL|-LJL7JJFL-JL--7F7L7FJF-7F7F-7L--JL7F-J|||F7LJL--7F7L7F-J77.F-L|.J|FJ.L7..F7LJ.LJ||L7|F7F7F--JL7||L7LJLJ|L7FJL--7|L7|F7||LJF7FJLJ7-||FL7.| +.LJ-J-LL77-|LF---J|L7LJFJ.|||L7L7F--7|L--JLJ||F7F-7LJL-JL-7-7F77-.|J7|FLLJ.FLJ7F-|LLJFJLJLJLJF-7FJ|L7L7F-7|FJ|F---JL-JLJLJ|F-JLJJFLL-L-FJ-J7 +F|J..L||LJ7LFL-7F7|-L-7L7FJ|L-JFLJFFJL------JLJ|L7L7F--7F-J-L-J-JFJ--7F.|F7.L.|JL|-F-JF---7F7|FLJ7L-J.LJ.LJL-JL-7F7F7F7F7FJL-7F7-F7|.|.LFJLJ +7---7F-7JF||L|FLJLJF7FJFJL-J|F7F7F7L----------7L7L7||F7|L-7-FJFJ-JLFJ|J-F||7.LJ|LF7L-7|F--J||L7F--7F7F------7F7FJ|LJLJ|||L7F-J||7||F--77.F7| +LJ||L|.L.JLJ|.F7F-7||L-JF7-F-JLJLJL-7F--------JLL-JLJ||L--JF-777FLFF7.|7FJL7-.LF-JL-7LJL-7FJ|FJL-7|||L-7F--7LJLJJ|F---J|L-JL-7|L7|LJF7||LJ-7 +-J-|J|..F|.LF-J|L7LJL---JL-JF7F7F7F7|L----7F---7F----J|F7J|||LF-7J.F7-7FJF7|.F|L---7|F7F7LJ-LJF--JLJL-7LJF-J-F7F7||F7-FJF7F7FJ|FJL7FJLJ-L|7F +|7LL-J.L--7F|F7L-JF---7F----JLJ||LJ||F----J|F--J|F7F7FJ|L--77LL7||F||FFJFJ||-FF7F--JLJLJL7F-7FJF7F---7L--JF--JLJLJLJL-JFJ|||L-JL--JL--7J7F-- +|F.FL-L-F-FLLJ|F-7|F-7LJF7FF---J|F-JLJF7F7FJL---J|LJ|L-JF7FJ7FFJL7FJL-JFJ|||-||LJF7F7F7F7LJFJL-JLJF77L----JF-7F--7F7F--J-|||F7F-7F7F7FJ-L.FL +L--L.7J-J|.|L|LJLLJL7L--JL-JF-7FJL----JLJLJF7F7F7L-7|F--JLJ|.FL-7LJF--7L-7LJF7L--JLJLJLJL-7L7F-7JFJL7F7F7F-JFJL-7|||L---7LJ||||7LJ||LJJ|L-F7 +FJJ.-.F.7LF|.F-----7L---7F-7|FJ|F----7F----JLJLJL--JLJ.F-77F77LLL--J.FJF7L-7||F7F--7F-----JFJL7|FJF7LJ||LJF7L---J||L-7F7L7LLJ|L-7FLJJLFFJ|L- +|J7LL-7F7.-J7L--7F7|F---JL7||L-JL---7|L----7F7F7.F7LF--JFJFJ|JLF-7F-7L7|L--J|LJLJF7|L-7F--7L--JLJFJ|F7LJF-JL----7|L-7LJL-JF-7L7FJF7|7F-|F|J| +|||-LLL|-LF-7JFFJ||LJF7F7FJ||F------JL----7LJLJL-JL-JF7FJJ|FJ7FL7LJFJFJL---7L---7|LJ.FJ|F-JF-7F-7L7LJL7FJF-----7|L7FJF-7F7|FJFJL-JL--7-.|J-- +LJ--7JL|7||-77FL-JL-7|||||7LJL-----------7L7F-7F7F---JLJF-JL-7F7L-7L-JF----JF---J|F7FJFJ|F7|FLJLL7|F-7LJFJF--77LJ|LJ||FJ|||L7|F-7F7F7|JFFJ|L +LJ-FL.LL--JFJF7JFF--J|||LJF7-F---------7FJJ|L7LJLJ-F---7L--7FJ||F7L--7|F7.F7L---7|||L7|-LJ||F-7F7LJL7|F7L-JF7L7.F7JF-JL-JLJFJ||FJ|LJLJFFL-JJ +.|J|L-77.JFL-||7FJF-7|||F-JL7L--------7LJF7L-JF7.F7|F--JF7FJL7||||.F7|||L-JL-7.FJLJ|-LJF7FJ|L7LJ|.F-JLJL---JL7L-JL-JF7F7F-7L7LJL7L7J-L-.LL7. +LJ-F|JF|.F7J-|L-JFJ.LJLJL--7|F7F7F7F-7L--J|JF7||FJLJL7F7||L7FJ|||L7|LJ||F----JFJF-7|F--J|L-J7L7FJFJF---7F---7|F--7F-J||LJ7|FJJ-LL-JF7LL-7L77 +|.|.|.-JFFLJFL-7FJF-7F7F--7|LJLJLJLJFL-7F7L-J||LJF7F7|||||FJ|FJ||FJL-7LJ|.F--7L-JFJ|L-7FJF7|F-JL7L-JF--J|F7FJLJF7|L-7LJF-7||JL77.|-7J.L|FJL7 +.F7.-7FF777FLJ|||.L7|||L-7|L7F7F------7|||F-7LJF-JLJLJ||||L7|L7LJL7F-JF-JFJF7L7F7L7L7FJ|FJL7L--7L---JF7FJ|LJF7F||L--J-FJFJLJJ.J7-JJL.|-FJFFJ +F77FL-7LJL-JL-FJ|F-JLJL7FJL7||LJLF----JLJ|L7|F7L----7FJ|||F||-L7F-JL-7L7FJFJL-J||FJFJL7||F-JF7FJF7F7FJLJ|L--J|FJL-----JFJF77F7JJ|.-7.|..F7L- +F7-JLJ-77FLF--|FJL----7|L-7|||F--JF7F7F7FL-JLJ|F--7FJL7|||FJL7FJL7F77L7||FJ7F-7|||FJF7||||F-JLJFJLJ||F7|FF---JL7F---7F-JFJ|F-7|F7-|7.L7-F|-J +||J-FJFLFJL-.FLJJLF--7|L--J|LJL---JLJLJ|F7F--7|L-7LJF-JLJ|L-7|L-7|||F-J||L-7|FJ|LJL7|LJ|||L7F-7|F--J||L7FJF7F7-|L-7.LJF7L7LJFJF7L7.7-L|7|.|. +|J.||LL7L7-|.|LF77L-7LJF--7L--7F-------J|LJF-JL7FJF7L---7L-7||F-J|||L-7||F-J|L7L--7|L7FJ|||LJ-LJ|F--JL7|L-JLJL-JF7L7F-JL-JF7L-J|-F7J-F-JL77. +|LFL7J.|-F7|-JF|L--7L--JF7L--7LJF----7F-JF7L-7JLJFJ|F7.FJF-J||L-7||L7FJLJ|F7|FJF-7|L7||FJL7JF7F7LJF7JFJL----7F7FJL7LJF7F--JL---J7||.||J..JJF +..77|..|-|||F--L--7L7F--JL---JF7|F--7LJF-J|F7L--7L7|||FJFJ|FJL-7|LJFJL-7FJ||||FL7|L7|||L7FJFJ||L7FJ|FJF7F7F7LJLJF7L--JLJFF7F7F--7||F-7|F|.-7 +FFF7FF7|7.7L777F7-L7|L------7FJ||L-7|F7L-7LJ|F--J.||||L7|F7L-7FJ|F-JF7FJL7|||L7FJL7||||FJ|JL7LJFJL7LJFJLJLJ|F7F-JL7F7F7F-J||LJF-J|||FJ7-777F +FF||--JL|-F7F7FJL--JL-7F7F--J|FJ|F-JLJL--JF7|L7F-7||||FJ|||F-JL7|L-7||L7FJ|||FJL7FJ||||L7L-7L7FJ|LL-7|F----J|||F--J|||LJF-J|F-JF7|||L--7|7FJ +LLLJ7.F-LF|||LJF7F7F-7|||L---JL-JL----7F-7||L-JL7LJ|||L7|||L-7FJ|F-J||FJL7|||L7FJL7|||L-JF-JFJL-77F7LJL7F---JLJL---JLJF-JF7||F7|LJLJF--J7JJ| +|LJ.F|JLLFJLJF7|LJLJ||||L7F7F-----7F--J|FJ|L7FF7L-7|||FJ||L7FJL-JL7-||L7FJ|||FJL7FJ||L--7L7FJF--JFJL7F7LJF7F-7F-------J7FJLJLJLJF--7L--7L|-F +L7J.F|7||L-7FJ||F----J||FJ||L----7LJF7FJL7L7|FJL-7||||L7||FJL--7F-JFJL7||FJLJL7FJL7||F77L7LJFJJF7L7FJ|L-7|LJFJL---------JF-7F7F7L-7L---JF-7J +JJFFF|7-FJFLJ|LJL----7LJL-J|F7F7-L-7||L7FJ|||L7F7LJ|||J|||L-7F-J|F7L-7|||L-7F-JL-7||||L--JF-JF7||FJL7|F7LJF7L---7F-7F7F7FJF|||||F7L--7JF7JLF +L-|J||-J|.7JLLF------JF-7F7|||||F7.LJL7||F7|L7LJL-7||L7||L7FJL-7LJ|F7|LJL7FJL--7FJLJ|L--7FJF-J||LJF-J||L--JL--77LJFLJ|||L7FJ|LJLJL7F-J-F|-J| +L7JFJ|FFJ-JJ7||F---7F7|FJ||LJLJLJL----J||||L7L7F-7||L7||L-JL77FJF-J|LJF--JL-7F-JL--7|FF-JL7L-7|L7FJF7LJF---7F-JF7F7F7LJ|FJL7L--7-FJ|-JL7LLL- +FLF7L|J-L--F|7LJF7FJ|||L-JL7F7F7F---7F7||||FJFJL7||L7|LJF7F-JFJFJF7L-7L-7F-7||F7F7FJL7L--7L7FJ|FJ|FJ|F-JF-7|L--JLJLJL-7LJJ-L7F-JL|FJ7-7.--7| +7-FF7LL|||FFF-7-||L7||L7F--J|||LJ-F-J|||||||FJF7||L7|L-7|LJF7L7||||F7L-7|L7|||||||L7FJF7FL7||FJ|FJ|FJL--JFJ|F--7F--7F7L-7FFFJ|F7F||L|7F-|-LJ +.F--L--||7-FJFJFJ|FJ|L7|L7F-JLJF-7L7FJLJLJ||L7||||FJ|F-J|F7|L7||FJLJ|F7|L7|||||||L7|L7||F7||||FJL7|L7F7F7|FJL7L|L-7LJ|F-J7.L7||L7||.7-|JL--J +-|JL||.FLF7L7|LL7|L-JLLJFJ|F-7F|FJFJL--7F-J|.|||||L7|L-7LJLJFJ|||FF-J|LJ7LJ|||||L7||F||||||||||F-J|FJ|||LJL-7L7|F7L-7LJF77FFJLJFJLJJL|...L7. +.L7|LJF7J||F||F-J|F--7F7L-JL7L-JL-JF7F-J|F-JFJ|LJL7|L7FJF---JFJ|L7L-7L----7||LJ|FJ||FJ|||LJ||LJ|F7||FJ|L---7|FJ||L--JF-JL7FJF--J|FLJ-7.L7LF- +J.-J7-|L-JL7||L-7|L-7LJ|F7..L--7F7FJ|L7FJ|F-JFJF7F||-|L7|F7F-J7L7|F7|F7F7FJ|L7FJL7||L7||L-7LJ|FJ||||L7L7F--J|L7|L----JF-7|L7L--7F-.L|.|F7L-7 +FJ.F-7L-7F7LJL--JL--JF7LJL7F7F7LJ|L7|FJ|FJL-7L-J|FJL7|FJ|||L---7|||LJ||||||L7|L7FJ||FJ||F-J7F-JFJ|||FJFJL--7L-J|F----7L7LJF|F--J7.L-7|-LJF7L +FJF|-|7FJ||F-7F-7F--7|L-7FJ|LJL--JFJ|L7||F--JF7FJ|F7|LJ7LJL7F-7|LJ|F7||||L-7LJL|L7||L-J|L7F-JF7L7LJ|L7|F-7FJF7-|L---7L-JJLF|L-7J|F.FLJL--JFL +-.F|7|FL7|LJFJL7||F-J|F-JL-JF-7F7FJ|L-JLJL-7FJ||7|||L---7F7||-LJF-J|||||L7FJ|F-JFJLJF--JFJL-7||FJLFJFJ||FJ|FJL7|F7F7L----7.L7FJ7|.FJ--77|7|| +J7.LFLJFLJ-|L7FJLJL-7|L-7F7FJFJ||L-7F---7F7|L7||FJ|L7F--J|LJL--7L--J||LJF||F7L-7L--7L-7FJ-F-J|||F-JFJFJ|L7LJF7||||||F----J-JLJJ7||J|F|7-FFJ. +|.|-7-L7L|F-FJL7-F--JL7FJ|||FL7||F-JL--7LJLJFJ|||FJFJ|F7.|F-7F-JF---JL--7|||L--JF-7|F-J|F7|F7|||L-7L7L7|7L7FJLJ|||||L---7JLFJLF-7|L|LLJJJ|LL +7-FFL-J|F.FL|F-JFJF--7||FJ||F-J||||F7F7L--7FJ|LJ|L7L7LJ|FJ|FJL-7L-7F7F7FJ||L---7L7||L-7LJ|LJLJLJF7|FJFJL7||L--7LJ||L--7FJLF7J7|J7|-7|.LJLLF. +|.L-L7.|.FF-J|F-JFJJFJ|||FJ||F7|||FJLJL---JL--7FJFJFJF7|L7||F-7L-7||||LJLLJF7F-JFJ|L-7|F7L7F--7FJLJL7|F7L7|F--JF-JL--7||J7J|-F-7L-J|-7|7.FL- +--|-JL|-F-L-7|L--JF7L-JLJL7|||||||L---7F7F7F--JL7|FL7|||FJ|||FJF7|LJ|L-7F7FJLJF7L7L-7||||FJL-7LJF-7FJ||L7||L-7L|F7F-7|LJJ|J|J.L|JJF|7F-|F7FJ +..F7-F.L||.L||F7F7|L------J|||||||F---J|LJ|L-7F-JL7LLJLJL7|LJL7|||F-JF7|||L-7FJL7L-7||LJ|L-7FJF-JFJL7|L7|||F7L7LJ||7LJJJ77F.F|FJJL-|L|-J7|LJ +F|L|.L|-LJ-|LJ|LJLJF7F-7F-7|||||LJL---7|F-JF-JL-7FJF7F--7LJF7FJ||||F-JLJ|L--JL77|F-JLJF-JF7|L7|F-JF-J|FJ||||L7L-7|L7|LL---J-L7LJ-FLLFL-..|F7 +LL7JFJL7.L--JFL-7F7|||FJ|FJ|||||F-----J||F7L--7FJL-JLJF7|LFJLJFJLJ||7F-7L-7F-7L7LJLF77L-7||L7LJL-7L-7|L7|||L7L7FJ|FJ77|FJ-JJLJ7J.LJ|.F|J7.FJ +.LJ.|LFF-L.L|F|F||||LJL7|L-JLJ|||F----7||||F--J|F7F7F-JLJFJF-7L7LFJL-JFJF-JL7L7L---JL7F7LJL7|F---JF7||FJ||L7L7|L7LJ|JLJ-7FJ|7|F7.FF|J7JL7F|7 +F.F7-F.||LFF7.|L||||7F7|L-7F77||||F---J||||L-77|||||L-7FFJFJFJFJFJF7F7|FJF7FJFJF7F7F7LJL--7LJL7F7FJ|||L7|L7L7|L7L--7L|7L|.|F-LJ--7JFJLF7L-JJ +L|7|J.FL7-L7J-|LLJ|L7|LJF-J|L-J|LJL---7|LJ|F7L7LJ|||F-JFJFJFL7L7L7|LJ||L7||L7L7|||||L--7F-JF--J|||FJ||FJL7|FJL7|F-7L7|FJ|7||||JJ|L7JJLLJJJF7 +F-.|7FF-||.-JFL7|FJFJL-7L-7L7F7L7F----J|.FJ|L7|F7LJ|L-7|FJJF7L7L7|L-7|L7|||FJ7||LJ|L--7|L7.L--7|||L-J|L-7||L7FJLJJ|FJ-L77F|FF||||J|.7.L|7|-| +-JL|-||||F|F---F-JFJF--JF7|7|||FJL-----JFJFJJ|LJL-7|F-JLJF7||||FJL-7||FJ|||L-7||F-JF7FJL7|F---J||L--7|F-JLJJ|L-7--|||L-|---F-LJ-|FL7.|.--F.| +F-.|7|JFF7-JLLF|F7|FL7F7|||FJ|LJ|F------JFJF-JF7F7|||F--7||||FJL--7|||||LJ|F-JLJL7FJ|L7-LJ|F-7FJ|F-7||L7JF--JF-JFFLJJLLLFJ-LJ.LFLF-J.L7JFJF7 +JFF|-LF|-FJ7.F-LJLJF-J|LJ|||FJJF-JF7F--7FJFJF7|LJLJLJ|F7LJLJLJF--7|||LJF7.|L---7FJL7L7L--7||FJ|FJL7|||FJFJF-7L--7-J|F-JL|7F|.LFJ.|L|-FJ||-7| +|-JL--7.LJ|FF|.LLJFL7FJFFLJ|||-L-7|LJF-JL7|FJ|L----7|LJL7F7F-7L-7||||F7|L-JF7F-J|F-JFJF7FJ||L7|L-7|LJ||-|FJJ|F7FJJ|FJJ.F7F-J.|.LL7-J.7.FJ7LJ +|L---J|7L-7LLF-J..FLLJLF-J.||7-L|LJ.SJF7FJLJFJF7F--JF---J|||FJF-J|||||LJF7FJLJF7LJFL|FJ||FJ|FJ|JFJL-7LJJ||7.||LJL7FF7.7FL7|J.F7-||FJ-LLJ.||. +F-J-|FFL77FF7|L-7-|J-J-J|.FLJJ7LLLF7L-J||F7.L7|LJF-7L----J||L7L7LLJLJL7FJ||F--JL--7J|L7||L7|L7L7|F7FJF|.LJ7-LJ-|J|FJF-77L|LF.F-JF|J|F.|.J7F7 +|F|7.JJ|LJ|L-.F-J||.FL--7FLLJ||77L||F--JLJL-7||F7L7L------JL7L7L-7F---JL7|||F-7F-7L7|FJ||-LJJ|FJLJ|L-7J7.||.|.F|.F|L||.-.|L|JJLFL7FJJ||||.7| +LJFJ77J|J-LJ.7J-LF-F7L--|J7LLJJJLFJLJF-7F7F7||LJL7L-7F-7F7F7L7|F-JL--7F7||LJL7|L7L-JLJLLJL|F7|L-7||F7|77J.L-LJ7--|J-JJF|7L-J.LFJ-|-.LLJLLJ77 +FLL-F-.LFFJJ.L77..|L--|7||LJ....|L---JFJ|LJ|||F7FJF-JL7LJ|||FJ|L-7F7FJ|||L7F-JL-JJLJ|LJ|JFFJLJF7|-LJLJJJ7..FL||-7|L.L7FLJLL77-L7.-.F--|7.LF| +F|LFJ.7J|.7.FF-L7FLJJ7|.LF7.77FFF----7L7L7FJ|LJ|||L7F7|F-J|||FJF7||LJFJLJ-|L---7-F-7|-F---L--7|LJJL|7L77FFJ7.J--LL7.FL7.F|-F-.|FFJ.|JJF|--J| +||-|-7-7777|-7-FLLJ|.J.F7|J-.L--L-7F7L7|FJL7|F-JL7FJ|||L7FJLJL7|LJL7FJ7LF-JF--7L-7J7J|.FJ|.JL|L77FFL-.-F-J-|-7||LLF7.F--77.|.FJ|.||||7L-7JJ| +LL-7-.LLJL-JFJJL||JL77F|J-7|.7.|||LJL7LJL7LLJL-7FJL-J|L7||7-F-JL-7FJ|F--JF7L7-L7FJ.|7|FL-7.|-L-JJFL7L7|LJ|-|.-L|7J|L--JFJ7--F7.J-||LF-LL-J|| +FL7J---JL-L--.F.L77L--FLJ|-7-J7LF----JF-7L-7F7JLJJ7|.L7|LJ-FJF7F-JL-J|F7FJL7|JLLJFFJLL7-|J.L-7.L-|.|7.|7-|.L7LL|JFL7FL-7-J.FLJ-|FJ||L7LFJ.F- +F.J.||7L|..-|--JL|F--FL7FL.F.LF-L---7FJFJF7LJ|J-FL|--LLJ.JJL-J||-LJ.LLJ|L7FJL7..J-L-|-J7|F-LL|-7FLF--.F7JL-.-J.L-|LFLFJJ|LF-FJ--L7L-L|-J7F7| +|F|FLF|F--F--J.J7||7--7|7..-.FJ.|J.||L7|FJL7FJJFFJL-7J.J-7L||F|L7FFLJ.LL7|L7FJJ7LLL7|||L.L.F||7-LFJ.LFJ-77LJJF77.LF-JF-J-FJ-LJ.|F|LF-L.LLF7J +J-FJ.-JL|J.-L--JFJL-FJ-L|J--|LL-7.-LL-JLJ-LLJJ-L7-L7J--J-7.7--L-J-JL|-LLLJLLJ-LJ.L-LJ--..L-|-JJ-L|---|7JFL-L--J|-F-J-|-JJFJ-LJ-LL7-7LJ-7L-JJ diff --git a/2023/10/src/main.rs b/2023/10/src/main.rs new file mode 100644 index 0000000..5484140 --- /dev/null +++ b/2023/10/src/main.rs @@ -0,0 +1,311 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSE + +#[derive(Debug)] +struct MapCell { + dist: u64, + kind: char, + inside: bool, +} + +impl From for MapCell { + fn from(c: char) -> Self { + MapCell { + dist: 0, + kind: c, + inside: false, + } + } +} + +#[derive(Debug)] +struct PipeMap { + map: Vec>, + start: (usize, usize), +} + +const ALL_ADJ: [(isize, isize); 8] = [ + (-1, -1), + (0, -1), + (1, -1), + (-1, 0), + (1, 0), + (-1, 1), + (0, 1), + (1, 1), +]; + +impl PipeMap { + fn adjacencies(c: char) -> Option<[(isize, isize); 2]> { + match c { + '|' => Some([(0, -1), (0, 1)]), + '-' => Some([(-1, 0), (1, 0)]), + 'L' => Some([(0, -1), (1, 0)]), + 'J' => Some([(0, -1), (-1, 0)]), + '7' => Some([(0, 1), (-1, 0)]), + 'F' => Some([(0, 1), (1, 0)]), + '.' => None, + 'S' => None, + _ => panic!("unhandled type"), + } + } + fn valid_pos(&self, pos: (isize, isize)) -> bool { + pos.0 >= 0 + && pos.0 < self.map[0].len() as isize + && pos.1 >= 0 + && pos.1 < self.map.len() as isize + } + + fn apply_adj(&self, pos: (usize, usize), adj: (isize, isize)) -> Option<(usize, usize)> { + if self.valid_pos((pos.0 as isize + adj.0, pos.1 as isize + adj.1)) { + Some(( + (pos.0 as isize + adj.0) as usize, + (pos.1 as isize + adj.1) as usize, + )) + } else { + None + } + } + + fn adjacent_positions(&self, pos: (usize, usize)) -> Option<[(usize, usize); 2]> { + let adj = Self::adjacencies(self.map[pos.1][pos.0].kind); + if let Some(adj) = adj { + let mut positions = adj.iter().filter_map(|adj| self.apply_adj(pos, *adj)); + Some([positions.next().unwrap(), positions.next().unwrap()]) + } else { + None + } + } + + fn start_adjacencies(&self) -> [(usize, usize); 2] { + let mut adj_positions = Vec::new(); + for neighbour in ALL_ADJ + .iter() + .filter_map(|adj| self.apply_adj(self.start, *adj)) + { + if let Some(neigh_adjs) = self.adjacent_positions(neighbour) { + if neigh_adjs.contains(&self.start) { + adj_positions.push(neighbour) + } + } + } + + [adj_positions[0], adj_positions[1]] + } + + fn mark_inside(&mut self) { + let start_adj = self.start_adjacencies(); + + let start_kind = if start_adj.contains( + &self + .apply_adj(self.start, (0, -1)) + .unwrap_or((99999, 99999)), + ) && start_adj + .contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999))) + { + '|' + } else if start_adj.contains( + &self + .apply_adj(self.start, (-1, 0)) + .unwrap_or((99999, 99999)), + ) && start_adj + .contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999))) + { + '-' + } else if start_adj.contains( + &self + .apply_adj(self.start, (0, -1)) + .unwrap_or((99999, 99999)), + ) && start_adj + .contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999))) + { + 'L' + } else if start_adj.contains( + &self + .apply_adj(self.start, (0, -1)) + .unwrap_or((99999, 99999)), + ) && start_adj.contains( + &self + .apply_adj(self.start, (-1, 0)) + .unwrap_or((99999, 99999)), + ) { + 'J' + } else if start_adj.contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999))) + && start_adj.contains( + &self + .apply_adj(self.start, (-1, 0)) + .unwrap_or((99999, 99999)), + ) + { + '7' + } else if start_adj.contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999))) + && start_adj.contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999))) + { + 'F' + } else { + panic!("invalid start"); + }; + + for row in &mut self.map { + let mut inside = false; + for cell in row { + let mut kind = cell.kind; + if cell.dist == 0 && kind != 'S' { + cell.inside = inside; + } else { + if kind == 'S' { + kind = start_kind; + } + if kind == '|' || kind == 'L' || kind == 'J' { + inside = !inside; + } + } + } + } + } +} + +impl From> for PipeMap { + fn from(lines: Lines) -> Self { + let mut map: Vec> = Vec::new(); + for line in lines { + map.push(line.unwrap().chars().map(MapCell::from).collect()); + } + + let start = map + .iter() + .enumerate() + .map(|(idx, row)| (row.iter().position(|p| p.kind == 'S'), idx)) + .find(|(x, _)| x.is_some()) + .unwrap(); + let start = (start.0.unwrap(), start.1); + + let mut pipemap = PipeMap { map, start }; + + let mut cur_positions = pipemap.start_adjacencies(); + let mut cur_distance = 1; + loop { + for pos in cur_positions { + pipemap.map[pos.1][pos.0].dist = cur_distance; + } + cur_distance += 1; + + if let Some(new_pos1) = pipemap.adjacent_positions(cur_positions[0]) { + if let Some(new_pos1) = new_pos1.iter().find(|pos| { + pipemap.map[pos.1][pos.0].dist == 0 && pipemap.map[pos.1][pos.0].kind != 'S' + }) { + cur_positions[0] = *new_pos1; + } else { + break; + } + } + if let Some(new_pos2) = pipemap.adjacent_positions(cur_positions[1]) { + if let Some(new_pos2) = new_pos2.iter().find(|pos| { + pipemap.map[pos.1][pos.0].dist == 0 && pipemap.map[pos.1][pos.0].kind != 'S' + }) { + cur_positions[1] = *new_pos2; + } else { + break; + } + } + + if cur_positions[0] == cur_positions[1] { + pipemap.map[cur_positions[0].1][cur_positions[0].0].dist = cur_distance; + break; + } + } + + pipemap + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let map = PipeMap::from(input); + + map.map + .iter() + .map(|row| row.iter().map(|cell| cell.dist).max().unwrap()) + .max() + .unwrap() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut map = PipeMap::from(input); + map.mark_inside(); + + for row in &map.map { + for cell in row { + print!("{}", cell.kind); + } + print!(" "); + for cell in row { + print!( + "{}", + match cell.inside { + true => 'I', + false => 'O', + } + ); + } + println!(); + } + + map.map + .iter() + .map(|row| row.iter().filter(|cell| cell.inside).count()) + .sum::() as u64 +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"..F7. +.FJ|. +SJ.L7 +|F--J +LJ..."; + + const EXAMPLE2: &str = &"FF7FSF7F7F7F7F7F---7 +L|LJ||||||||||||F--J +FL-7LJLJ||||||LJL-77 +F--JF--7||LJLJ7F7FJ- +L---JF-JLJ.||-FJLJJ7 +|F|F-JF---7F7-L7L|7| +|FFJF7L7F-JF7|JL---7 +7-L-JL7||F7|L7F-7F7| +L.L7LFJ|||||FJL7||LJ +L7JLJL-JLJLJL--JLJ.L"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 8); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE2); + assert_eq!(problem2(c.lines()), 10); + } +} diff --git a/2023/11/Cargo.lock b/2023/11/Cargo.lock new file mode 100644 index 0000000..4ab9be8 --- /dev/null +++ b/2023/11/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/2023/11/Cargo.toml b/2023/11/Cargo.toml new file mode 100644 index 0000000..8f5b9a5 --- /dev/null +++ b/2023/11/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/2023/11/input b/2023/11/input new file mode 100644 index 0000000..1956e53 --- /dev/null +++ b/2023/11/input @@ -0,0 +1,140 @@ +............#..................#..........................................................#..........................#...................... +.................................................................#...............#..............#...............#......................#.... +...................................#.....................#..................#............................#...................#.............. +#.....................#.....................#............................................................................................... +........................................................................................#............#...................................... +.....#..............................................#....................................................................................... +.......................................#................................#..........#...........#......................................#..... +..................#...........#............................................................................................................. +.............................................#............#................................................................................. +.#............................................................................#..................................#.......................... +............#......................#....................................................................#...................#.............#. +.......#............#....................#........#...................................#......................#.............................. +...................................................................#................................#....................................... +..............................#.................................................................................................#........... +..#...........................................................................................#.......................#..................... +..............................................................................#............................................................. +.......................................#.........#............#...........................#................#..............................#. +........................................................#.............#..................................................................... +.....#..........................#....................................................................#....................#................. +....................#.......................#...................................#...............#...............#......................#.... +...............#..................................................................................................................#......... +..#......................................................................#...................................................#.............. +.................................................#............#.........................................#.............#..................... +....................................#....................................................................................................... +...........#..........................................................................#..................................................... +.................#.......................................................................................................................... +.............................................#............................#....................................................#............ +.........................#.......#..........................#............................................................................... +.......#............#....................................................................................................#..........#....... +...................................................................#................#...........................#........................... +..............#....................................#..........................#..............#.............................................. +..#....................#....................................................................................#........#...................... +.........................................................................#......................................................#........... +......................................#.........#........................................................................................... +..........#................................................................................................................................. +.....................#......................#...............#.................................................#............................. +...............................#...................................................#....................................#.................#. +.............................................................................................#.............................................. +...................................................................#........................................................................ +................#..........#.....................#..................................................................................#....... +...................................#............................................#..................#........................................ +.....#.....................................................................#...............................#.....#.......................... +............#.........................................................................................................#..................... +.....................#.............................#.......................................................................#................ +..............................................................#...............................#..........................................#.. +.....................................#.......#.................................................................#............................ +...............#...................................................................#.....#..............................#................... +...#.....................#.........................................#......#...........................#..................................... +.........#......................................................................................#.............................#............. +..........................................................#................................................................................. +..................................#.......#...........................................#......................#......................#....... +.............................................................................#.............................................................. +.....#...........................................#..................................................#....................#.................. +............................................................................................................................................ +............................................................................................................................................ +.................#...................#........................#......#..................................#................................... +..........................#.........................#.....................................#......#.........................#................ +..............................................#...........................#.....................................#........................... +#.....................#.................#............................................................................................#...... +...............................#..........................#.....................................................................#........... +..........#.........................#.............................#...........#....................................#........................ +.....................................................#...................................................................................... +.............................................................................................#.............................................. +............................................................................................................................................ +...............#...........#..........#.....#..................#.......................#......................#.......#..............#...... +........#...........#.............................#.....#................................................................................... +............................................................................................................................................ +.#................................#.............................................#................................................#.......... +............................................................................................................................................ +..........................................................................#...............................................................#. +..............................#...............#......................................#........#.............#.........#..................... +...#................................................#....................................................................................... +............................................................................................................................................ +.......................#...............#...........................................................#..........................#............. +.................................................#...........#......................................................#....................... +...........#............................................................................#................................................... +.....#..................................................#.....................................#.............#..............................# +..................#..........#..............................................#..........................#..............................#..... +....................................................#....................................................................................... +..#............................................................................................................#................#........... +.............................................................#........#..........#.....#.................................................... +...........#..........#............#.......#...............................................................................#................ +............................................................................................................................................ +......................................................#..............................................#...................................... +.........................#.....#...............................................#................................#................#.......... +...#.......................................................................................................#...........................#.... +........#...........................#....................................#...............#.................................................. +..........................................#.......................#......................................................................... +............................................................#.............................................................#................. +...........................#...................................................................#.....................#...................... +...............#.................#............#............................................................................................. +...#......#..........................................#..................................#...............#................................... +.................................................................#......................................................................#... +.......................#..................#...............#..................#.............................................................. +.................................................#...................................#.............#............................#........... +................#........................................................................................................................... +...................................#.........................................................#........................#.............#....... +.............................................................#..........................#......................#............#..............# +...#..................#.............................#..........................#............................................................ +........#......................................#............................................................................................ +..............#............#...........#............................................................#....................................... +....................................................................#.......................#............................................... +.#....................................................................................#........................................#.......#.... +...........#...................#............................................................................................................ +.........................#..........#.......#.........#..................#..................................#............................... +................................................................#....................................................#...................... +...#.............................................................................................#.....#.................................... +.................................................#....................#..................................................................... +.......................#.........#..................................................#.......#...................#........#.................. +........................................................................................................................................#... +...........#................................................#..............................................#................................ +.....................................#..........................................................#........................................... +....#.......................................#....................................................................................#.......... +...................#..............................................#.........#..........#.............#...................................... +............................................................................................................................................ +............................................................................................................................................ +.............#.......................................#...............#......................#......................#...............#........ +...............................................#............................................................................................ +....#.................................#.......................................................................................#............. +............................#.................................................#......#.....................................................# +...................#..............#.......#...............#.....................................................#.........#................. +..................................................................#.....................................#................................... +...................................................#..............................#......#.......#..............................#........... +.......................................#.....................#..............#......................................#........................ +.................#.......................................................................................................................... +..............................................#.......#.................#.................................#................................. +.........#...............#.............................................................#.................................#.............#.... +..#..............................#..................................#....................................................................... +...............................................................#...........#.......................................................#........ +............................................................................................................................#............... +................#...........#............#.........#........................................................................................ +........#.............#........................................................................................#...........................# +..........................................................#...............................#..............#...........................#...... +...............................#................................................................#........................................... +..................#.............................................#.........#..........................................#...................... +.....#................................................#.........................................................................#........... +............................................#.....................................#.........#............................................... +.......................................................................................................................................#.... +#..........#.............#......#.................................#...................................#..................................... +.....................................#.........#.........................#.....#............................................................ diff --git a/2023/11/src/main.rs b/2023/11/src/main.rs new file mode 100644 index 0000000..17bccf0 --- /dev/null +++ b/2023/11/src/main.rs @@ -0,0 +1,211 @@ +use std::fmt::{Display, Write}; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::iter::repeat; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSE + +struct GalaxyMap { + map: Vec>, + galaxy_positions: Vec<(usize, usize)>, +} + +impl From> for GalaxyMap { + fn from(lines: Lines) -> Self { + let map = GalaxyMap { + map: lines.map(|line| line.unwrap().chars().collect()).collect(), + galaxy_positions: Vec::new(), + }; + map + } +} + +impl GalaxyMap { + fn universe_expansion(&mut self) { + let mut columns_to_insert = Vec::new(); + + for col in 0..self.map[0].len() { + if self.map.iter().map(|row| row[col]).all(|c| c != '#') { + columns_to_insert.push(col); + } + } + + for row in &mut self.map { + let mut offset = 0; + for col in &columns_to_insert { + row.insert(*col + offset, '.'); + offset += 1; + } + } + + let rows_to_insert: Vec<_> = self + .map + .iter() + .enumerate() + .filter_map(|(idx, row)| if !row.contains(&'#') { Some(idx) } else { None }) + .collect(); + let gen_row: Vec<_> = repeat('.').take(self.map[0].len()).collect(); + let mut offset = 0; + for idx in rows_to_insert { + self.map.insert(idx + offset, gen_row.clone()); + offset += 1; + } + } + fn universe_expansion2(&mut self) { + let mut columns_to_grow = Vec::new(); + + for col in 0..self.map[0].len() { + if self.map.iter().map(|row| row[col]).all(|c| c != '#') { + columns_to_grow.push(col); + } + } + + for row in &mut self.map { + for col in &columns_to_grow { + row[*col] = 'x'; + } + } + + let rows_to_grow: Vec<_> = self + .map + .iter() + .enumerate() + .filter_map(|(idx, row)| if !row.contains(&'#') { Some(idx) } else { None }) + .collect(); + let gen_row: Vec<_> = repeat('x').take(self.map[0].len()).collect(); + for idx in rows_to_grow { + self.map[idx] = gen_row.clone(); + } + } + fn find_galaxies(&mut self) { + for (y, row) in self.map.iter().enumerate() { + for (x, _c) in row.iter().enumerate().filter(|(_x, c)| **c == '#') { + self.galaxy_positions.push((x, y)); + } + } + } + + fn galaxy_distance(&self, a: usize, b: usize) -> u64 { + let (a_x, a_y) = self.galaxy_positions[a]; + let (b_x, b_y) = self.galaxy_positions[b]; + + let x_dist = b_x.abs_diff(a_x); + let y_dist = b_y.abs_diff(a_y); + + x_dist as u64 + y_dist as u64 + } + fn galaxy_distance2(&self, a: usize, b: usize) -> u64 { + let (a_x, a_y) = self.galaxy_positions[a]; + let (b_x, b_y) = self.galaxy_positions[b]; + let x_dist = b_x.abs_diff(a_x); + let y_dist = b_y.abs_diff(a_y); + + let mut dist = x_dist as u64 + y_dist as u64; + + for row in a_y.min(b_y)..a_y.max(b_y) { + if self.map[row].iter().all(|c| *c == 'x') { + dist += 1000000 - 1; + } + } + + for col in a_x.min(b_x)..a_x.max(b_x) { + if self.map.iter().all(|row| row[col] == 'x') { + dist += 1000000 - 1; + } + } + + dist + } +} + +impl Display for GalaxyMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for row in &self.map { + for c in row { + f.write_char(*c)?; + } + f.write_char('\n')?; + } + Ok(()) + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut map = GalaxyMap::from(input); + map.universe_expansion(); + map.find_galaxies(); + println!("{}", map); + + let mut galaxies: Vec<_> = (0..map.galaxy_positions.len()).collect(); + let mut sum = 0u64; + while let Some(target) = galaxies.pop() { + sum += galaxies + .iter() + .map(|source| map.galaxy_distance(*source, target)) + .sum::(); + } + sum +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut map = GalaxyMap::from(input); + map.universe_expansion2(); + map.find_galaxies(); + println!("{}", map); + + let mut galaxies: Vec<_> = (0..map.galaxy_positions.len()).collect(); + let mut sum = 0u64; + while let Some(target) = galaxies.pop() { + sum += galaxies + .iter() + .map(|source| map.galaxy_distance2(*source, target)) + .sum::(); + } + sum +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#....."; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 374); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 0); + } +} diff --git a/2023/12/Cargo.lock b/2023/12/Cargo.lock new file mode 100644 index 0000000..b4915f0 --- /dev/null +++ b/2023/12/Cargo.lock @@ -0,0 +1,106 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "day12" +version = "0.1.0" +dependencies = [ + "itertools", + "rayon", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" diff --git a/2023/12/Cargo.toml b/2023/12/Cargo.toml new file mode 100644 index 0000000..f542776 --- /dev/null +++ b/2023/12/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day12" +version = "0.1.0" +edition = "2021" + +[dependencies] +itertools = "0.12.0" +rayon = "1.8.0" diff --git a/2023/12/input b/2023/12/input new file mode 100644 index 0000000..d0daee0 --- /dev/null +++ b/2023/12/input @@ -0,0 +1,1000 @@ +.#?.##??.?? 2,2,1 +?.#?#?????.? 1,1,3 +??##??????????.?# 4,4,2 +..#?????.?#?.#?. 2,2,1,2 +????????????##?? 1,1,1,1,3 +?????.#????#??? 1,1,3,2,1 +.?#..?#?????#?? 1,2,5 +?#??????????????.? 4,1,2,2,1 +#??##?.#??. 1,2,3 +???????#??????#.?? 13,1 +?#..#??.?#.? 2,1,1,2 +?.###??????#???.?.?? 11,1 +.?????????#???. 3,7 +?????.#??????#.? 1,1,5,2 +?#???????? 4,4 +?.??.#.????#??#??? 1,1,1,4,1,1 +??.?????.#?.?#?????. 1,3,1,2,3,3 +.???#?????.?? 4,1,1,1 +??#??.#??.?? 3,2,2 +.???.?###. 1,4 +#.???##?#? 1,6 +#.??????#?.??. 1,5,1 +.??#???#?#????????? 3,8,2 +??.?????????.?.??# 1,5,1,1,1,1 +????????????#? 1,4,4 +?????????#??. 2,5 +?.?#?.?.??? 2,1,1 +?.?.??.??#???##?? 1,1,2,1,3 +.?#?.#????.. 2,1,2 +#??#??##?.#??? 1,2,2,1 +??.##?#?#???..? 1,4,4,1 +????.##?.? 1,3,1 +#???????#??#? 1,2,1,4 +??????????? 1,2,1 +#???.??##?? 1,2,3 +##?#?#?.??. 7,1 +?.?###????????? 4,7 +.?#??###???#?????? 12,2 +.#.???#????#. 1,6 +??.#???.#??#???##?? 1,1,1,1,6,1 +??#?.?#??? 3,4 +????????#.?. 2,2,2 +??.??????.??????#? 1,3,4,2 +.#??#???.?.???#??? 6,1,1,1,1 +?????????#??????#? 1,1,1,4,1,1 +???...?#??????????? 3,12 +????..?????? 2,1,2 +???.??.???? 1,1,2 +#..?###????????# 1,6,6 +.#?#.???#???##??#??? 1,1,1,1,10 +?.?#?#.????##.##.??? 3,6,2,1 +???????#?#????.?..# 4,9,1,1 +#??#??????????#.???? 1,2,1,7,1,2 +?#??????????????? 1,1,11 +???????#?#? 1,5 +.##?????????.## 4,3,2 +?#?????##??#.????## 1,1,1,3,1,6 +.????#.??..?#?.# 3,1,1,3,1 +?##????????????#?#?? 9,1,4 +.?????#.???.. 6,2 +?.??.#??.#?. 1,3,2 +#?.??.?#??# 2,1,5 +??#?#.????#??#.???.? 3,1,3,1,3 +.?#?#?#?.??.????? 6,1 +#?..??#??? 2,1 +#??#?????.##?# 2,5,4 +?.??..#.????? 1,1,1,2 +??????.?.?#? 1,1,1,2 +.???.?????.?????? 1,2,1,5 +?#...#???#? 2,1,1 +???.?????????#?#?? 2,4,1,6 +.#???##??????. 6,1,2 +#?????#???#?#?????.# 1,6,6,1,1 +.?#???##??.?.?#?? 7,1,1,1,1 +?#?...?##??.? 3,5 +#??#?????????..?##. 4,1,1,1,1,2 +?####?.?.??#?? 4,2 +#??##.?##???#?####?? 5,3,7 +??.?#????#??.??.??? 1,3,4,1,1,1 +.?.?#?##?.?##?????. 2,3,3,3 +???.?????#?#???.# 1,3,5,1,1 +???##?.??#.???#??#? 3,3,6 +.?#?????????.?#??? 2,1,2,3,1 +???...???? 1,3 +##?#?#?..?? 7,1 +????.?#.???# 3,1,2 +?????????##???.???#? 1,1,1,5,1,3 +?.#??????#?????. 1,2,2,4 +????.#???#? 4,2,2 +?????#????#?#??##?# 4,6,4 +#???.???###?#????? 1,1,1,7,2 +?.?#?#????##??#.?? 1,1,1,1,5,1 +?.????.?.???#? 1,1 +??##????????? 3,2,1 +???.?.??#???#? 1,6 +???#?#??#???.?.# 7,1,1,1,1 +.?.???#?##?? 1,5 +??#????#??????#? 8,3 +?#????#.?????#?#. 1,4,6 +???#.?#??#? 2,5 +??????????.??????# 4,2,7 +???#???...?? 1,2,1,1 +????.???????##?? 1,1,1,1,5 +.?##??.##? 2,1,2 +.???##??##??# 1,2,3,1 +????#?????##????? 1,10,3 +??#???.?.?##?..#??? 1,2,1,4,1,1 +???#?#???? 3,2 +.?.#???#????????? 1,9 +?#????##???#?????.# 3,7,3,1 +#.??##??????#????? 1,5,1,1,1 +?????.???????#?? 3,1,5,1 +#??????.##??????. 3,3,5,1 +.???????.? 1,1,1 +??#?#??##??#?.????# 11,3 +.?.?##.??##?.?#?#?#? 2,3,6 +#?????#?.??#??? 2,4,4,1 +.#???????#???? 1,2,6 +####?#?.#?????#?# 7,1,1,2,1 +??#?.?.??? 2,2 +?.??.??#????? 1,4,1 +?..????#?????##? 4,1,2 +#...????.?#?#??.?? 1,4,1,1,1,1 +????.????? 3,3,1 +#?.?##?????#?#? 2,3,1,1 +??????#?#?????## 2,12 +?.???#???.??#? 7,1 +#?#..??.#??#???#?.?? 1,1,2,1,6,2 +??#?...#.???#???.?? 1,2,1,1,4,1 +?.?..???.?. 1,1,1 +?.????..?.??#??? 2,3 +.??##??????.????? 10,1 +..?????###??? 1,4 +??????#???. 2,1,4 +????##.??.??#?????? 6,1,1,4,2 +??????????##?#?#?# 1,1,13 +???.??###?#?????#. 1,9,1 +?#??.????#?.?.?? 3,5,1 +???????????.??.???? 1,4,2,1,1 +??.##??.#... 1,4,1 +?.?#?#??#?.??##????? 7,6 +??.????#?#??#.?.? 1,8 +????#??#???? 4,4 +?..??????#?????. 1,9 +?????????.?##????? 4,1,5,1 +.?.#??.?#.#?? 1,1,2,2 +?.?##?????#.#?#? 9,1,1 +?#???#?????? 1,3,2 +..??#?.?????#??????. 2,11 +???#??????? 4,1 +???#??#????#?# 11,1 +?.?#?#?????#?.?#..? 1,6,3,2 +??.??#?#???.??.#?? 6,1 +?##.???????? 3,1,1,1 +???##?.??? 2,1 +#?.?.?#?#????. 1,1,5,1 +?.????#????? 1,1,4,1 +???#????.??? 1,1,2,1 +#.????..?.?.???#?? 1,1,1,1,1,6 +???##??.?#??#????#? 6,9 +???.#.??.???#??# 1,1,2,6 +????..?#?#?.##?#???. 3,5,2,4 +??#???#?#..???##? 7,3 +?#?##?.???.???? 5,1 +??.?#????#?#?.?#? 1,3,1,4,1 +.?.????.????##?? 1,2,1,1,6 +??????###. 1,1,3 +#?????.???. 3,1,1 +#?#..#????#???#?. 3,7,1 +?.?#??#?.???.?###. 1,3,1,1,1,3 +.#???.?.#?? 1,2,2 +.#?#??#?#??#?.?????? 12,1,2 +????#?????#?? 1,8,1 +..??????#?. 4,1 +##?#????#?#??????#?? 2,2,2,3,3 +?????#???#.#.. 4,2,1 +???##??..????..?#?# 5,3,4 +?#?#???#??..??## 8,4 +.?.???#???? 1,4 +???#?#??#.???.# 7,1,1,1 +..?.????????#??? 1,8 +?????.?##???????. 1,1,8,1 +?????.?????.???? 5,2,3 +???????????..?#??.?? 4,5,4,1 +?#?#???.???#???. 1,2,1,1,1 +.??.#?????.?? 2,3,1 +??..??#??.????. 1,1,3,3 +#??#.?.?.????????? 1,1,1,5,1 +??#????.??#. 1,2,2 +???#??????.?#.?? 1,2,1,2,1 +???#??.??????? 4,2,1 +??????.?#?##?????#?# 1,1,12 +??????.??.?#???.# 6,1,2,1,1 +??.?.????##?? 1,2,4 +????.?????#?#???#? 3,11 +.#????????.?.#?#??? 2,1,1,1,6 +??#????#?????????? 8,4 +??.?..?.?#???#????? 1,1,1,4,1,1 +?.????.###??#? 2,6 +??.#??????.?? 1,6,1 +.?.?.????#???? 1,6 +..??????.#?.?.? 5,2,1 +????.???.?? 4,2,1 +???###????#????##. 1,6,8 +.#??#???????? 1,8,1 +?#?##????.?? 5,1,1 +??####?####?????. 5,4,1,1 +?#??.?##?#???? 1,4,2 +#?????.#??????.# 4,1,3,1,1 +??#??#?????##.#? 8,2,2 +????##??..??#? 1,5,2 +?.#?##?..?#???. 4,3,1 +.?.?#???.????. 1,4,1,1 +??##..?... 3,1 +????#???##??#??# 1,12 +????#?#?.##??#??. 1,2,2,2,3 +.#?##?????#????????. 11,1,1 +?#??.???.#.?#.? 4,3,1,1 +??#?##???#. 7,1 +?.??#????#??.???.? 1,2,1,4,1,1 +.???##??.?.?????#?.# 4,1,2,4,1 +.?#???#??.##?####?. 2,1,1,8 +..???..??#??.? 1,4 +?##???????#??#?? 5,8 +????#?.??#?# 1,1,3,1 +??.?.??#.?.##.# 2,1,3,2,1 +???#???#??#????? 1,1,1,6,2 +#.???##.?.????#.? 1,5,5,1 +??????????#?#?#?#.. 1,1,7,1,1 +????..???? 4,2,1 +??????##??#??? 1,1,2,3 +#????#????# 1,5,2 +?????#???????#??? 1,14 +?#???.???? 1,2 +??#?##??.??????????? 7,1,1,1,1 +?.?#??#???#????.??? 2,3,5,1 +????##??##??.?#.# 1,9,2,1 +???????#?. 1,1,2 +#???###..?.???#? 2,4,1,1,1 +??????????? 1,1,6 +??#.?#?????.#??? 1,1,2,1,2 +#.??#???#.?#??#??#. 1,5,1,1,1,1 +??????????? 1,1 +??#.?#?????## 1,1,1,2 +???##?#?#?#? 1,5,1,1 +????##??#??? 1,1,7 +#????#.?????. 6,3 +?.?#???#?.# 7,1 +????.?#?????#?.? 2,2,3,2,1 +.?.#???#?.. 1,6 +.??#????.?????.## 3,4,2 +??.##??##??...?? 7,2 +##???.##???#??# 2,1,3,2,1 +?###?#??##?.?#???##? 10,3,3 +??#??????????###.? 15,1 +???###.????.# 1,4,3,1 +??.???????#?#?#?? 2,2,4,4 +????.??###?#?.? 2,7,1 +?.#?#????.????????. 1,4,1,1,3,1 +.??.?#?#??#.?#.??. 2,6,1,1 +????#.?.??.?. 1,2,1,1 +.??#???#?#??????? 6,8 +?????.??#?#?#??? 1,1,5,1 +??#?.#?#??#.#??????? 3,6,4 +????#????????#??.?? 1,1,2,1,7,1 +???????#?# 2,1,4 +??.???????#???#?? 2,2,5,1 +?#??..??????? 2,5 +?#???#?.???..#? 5,1,1 +??##.??.?#????.#?? 3,1,1,2,2 +#.??#?????.?.??????# 1,8,2,2,1 +?????.??.?.???.??? 1,1,1,1,1,3 +????#?.?????# 1,3,1,2 +????#???????#?#?? 5,9 +??#???.?#????#??#?.? 5,4,5,1 +##?..??#??..#?# 2,4,3 +?????.#?#?? 1,1,4 +..#?##????????.?#?? 4,2,1,4 +#?#?.??#?. 1,2,4 +????.?????????? 1,2,1 +??#??#???#?.? 1,1,1,4 +????#.????##?? 3,4 +???.???.?.?? 3,1,1 +?????.#???#?? 3,7 +.???#??.??.???? 3,2,1 +?????????##???#? 1,1,1,3,3 +?###??.??? 3,1,1 +#?????.?????.??? 4,4 +??#?.?#???#? 3,3,3 +???#???#??#??# 4,1,4 +??###.?.???#? 4,5 +.????????.?????.???. 1,2,3,3,2 +#.#??#?#??#?#?? 1,2,3,5 +???#?#??#????#????# 1,8,6 +?.??#??##??.??????. 8,1,1,1 +????##.?????#??#?? 4,6,3 +.???.????#?##??? 1,10 +?.?????.????????.? 2,1 +?#..??.?#.? 1,2 +??#????.??#?#???. 1,1,2,6 +???#??????????? 9,1 +#.#??????.#??????? 1,1,1,2,8 +???.?????..# 1,1,1,1 +.?###????. 3,1 +#.?????.?#?##??#???? 1,2,1,11 +????.??#???##.????#? 1,1,8,1,2 +#####.#??? 5,2 +??.???##??? 6,1 +???#?##?#??#??#.???. 15,2 +?#??.?.#?.#.?????? 3,1,1,1,1,1 +????.??##?#..? 3,2,1,1 +#???.??????#.?????# 2,1,7,2 +#???.??.???? 3,1,1,1 +.#???.????.#?? 1,1,3,3 +..??#??.#.?????.?.# 4,1,5,1 +???#####????????? 1,6,1,1 +???????.??????? 1,2,2,6 +?#.?.#???##? 2,1,2,2 +?#???#??????? 7,4 +.?#?#?.?.?### 1,1,1,3 +????.??????? 1,6 +?#?#???##????????. 8,5 +?#???#?.???#??????. 4,2,3,2 +???.??##??##????.??? 3,4,3,2 +..?#?#???#.?. 5,1 +???#?????#????##?# 3,2,7,1 +????????##?#? 2,7 +??????#.?..? 3,1,1 +..#??????#????## 2,5,2 +??##?.??.??? 2,1,3 +??#????????#?? 3,1,5 +?##?.##???????#??#.. 2,10,1 +?.?#????????.?#. 1,3,2,1,1 +.???#?????? 5,1 +???????#??#.????. 4,1,4,1,1 +.?.?#.?.#. 2,1 +?#.?.??.???? 1,1,3 +#?#??????.?# 1,2,1,1 +#?#.#.???????#???? 1,1,1,1,1,6 +?????.??#??..??? 3,4 +???????????#..? 3,2 +?#?.???.??#.?? 2,3,2 +??##????.??? 7,2 +????#??### 1,7 +.???????#?.?????###? 7,8 +???.???#?.#???????? 2,2,1,2,5 +.???????##??????# 3,5,1,1 +??#????#???#???##? 6,1,1,6 +?#?.?.??###??. 3,1,6 +????..?#?#?????? 2,9 +??.??...?##?###.? 2,7 +???#????###???? 2,1,7 +.????#.???? 2,1,1 +???.???##.#?##?#?? 1,1,2,1,6 +???#?#???#? 4,2 +?.#??????? 1,1,2 +.?.?.#?..# 1,2,1 +?.#.???????. 1,5 +?.#.?.#?##????? 1,1,4,2 +???????????#?? 4,1,4 +#?##????#??? 6,3 +?.????#?#???? 1,5,1 +??????#?#?. 7,1 +?#??.?.??###?????? 2,5,3,1 +##?????##????? 2,7,2 +#??#?..?##??.???#? 5,3,1,1 +.#?.?####???? 1,5,2 +???..?#?.? 1,1,1 +#??.???#?###??? 1,1,1,8 +?.?.?##????#..?#.?.? 1,1,8,2,1,1 +????.??????#? 1,2,1,1 +#?..?#??????.??? 1,6,2 +??#?.?####?? 3,6 +#.???????. 1,1,1 +#???????.?.???? 1,4,1,3 +???#??#??? 5,2 +?????.?????#???. 1,1,5,2 +##??#?#?.#?????#?. 7,1,2,1 +?????#?#.?????#??#?? 5,1,2,5 +?.##.???##?#???? 2,5 +#????.#???.??.?????? 1,3,4,1,1,2 +??#?#??#?.???##? 1,1,5,1,3 +?.?????.??. 2,1,1 +.??.?#??????#? 1,1,3,2 +??#??.????.? 4,1 +??.????#?.? 1,2,1 +#??#?.??#??? 1,2,1,2 +??.?##?.????? 3,4 +?.????#??.#????#??# 1,2,3,1,4,1 +?.#?..???#.?. 2,4,1 +?#???#??.#??#? 2,3,1,1 +?.???##???..#??#. 7,4 +#??#.?#??.????? 2,1,1,3 +?#?##????.? 7,1 +.??????#???##?? 4,7 +.?#??#?????..??? 6,1 +??#??#????#####???.? 6,6 +.????##.#?.???????. 6,2,4 +???.##?#??.?.##???.? 5,3 +..?..?#??..#?# 1,2,3 +##.?.??..?? 2,1,1 +???##????.?? 4,1,1 +.??##.???? 4,1 +??..???#??.????..?? 2,6,3,2 +?#??#??#?## 4,5 +??????.?#..??. 3,1,1,1 +??#?#??#??.???# 8,1,1,1 +.?#?.??#?.#.?#? 2,1,1,1,1 +?.?..??????#??. 1,6 +?.?.????????????? 1,7,1 +#??#????#.??????## 4,1,1,8 +?#.??#.#??#???#?#? 1,1,1,1,1,6 +???.????#????#?#.#? 2,1,2,4,1 +.?#?.???#? 2,1,1 +##????????? 3,4 +?#????#?#??#?#?#.? 2,4,6,1 +?????????##? 1,9 +?..#.#??#????#?#?? 1,4,5,1 +#??#?????? 4,4 +#.?.#??#?#???????##? 1,1,7,1,2 +????##?.?#..? 1,4,1 +????????#?. 2,2,1 +??.????.?#??.?#?? 1,2,4,1,1 +.??????.??##?# 1,1,5 +???#?#?.#??.#???? 7,3,2 +??.??#?.?. 2,1,1 +.#..????.? 1,1 +.???##????#??#? 1,2,6 +?..???#??#?? 1,1,5 +?#.??????#??#? 1,1,1,5 +#?.???.#?.?##?.#??#? 1,2,2,4,5 +??.?#???## 1,1,3 +?????.?.#????.?. 3,5,1 +?#?.???#?#?#.???# 2,7,1 +??.?..??..????....?. 2,2 +???#??##?.??? 7,1 +???#????.??????.???# 8,1,1,2,1,1 +??#????????#?#??.? 5,4 +####????????.?.??? 5,5,1,1,1 +????#?.????###?#. 3,7 +.??????????? 1,1,1,1 +##?????.##?? 2,1,3 +????????#?#?? 2,4 +??.#?.??#.??#??##?#? 1,2,3,9 +#??#?#?#??##???.??.? 4,1,6,1,1 +.?#..?##???? 2,4,2 +???.???##?? 2,6 +##???..?#? 4,2 +?.????#??.#.??#??? 1,2,1,4,1 +?##?#.???? 2,1,1 +?.?#????.?##??#??#?? 2,1,3,6 +#?????.?#?? 3,2,3 +.??#?????###??.?##?? 1,6,4 +??#.?#?.?##????? 1,1,1,3,1 +.??#??#?#?#???#.#? 12,2 +?.??.#?#?????.???#. 1,8,2,1 +#?..??#??? 1,1,4 +?.????..???#????? 1,2,1,5 +.???#???#####?? 4,7 +?.#????#??? 3,1 +????#???#???????.# 1,9,1,1,1 +??.???.?####. 1,4 +??.#???.?#?????. 1,1,1,4,1 +??.#????????..? 5,1 +.??#?????????.???. 1,2,5,1,1 +#??.###..? 1,3,1 +????#.?#??.??? 3,1,3,2 +###??????. 4,4 +.????#??#? 1,4 +?.#????????#???#?? 1,5,1,2,1,1 +?.???.???##? 1,1,3 +?????#??#??.??#? 2,3,1,3 +.???.?#???#????# 1,6,1,1 +?#???.???#?? 1,1,4 +?#????.???# 2,1,4 +??#?.?#????.?. 1,4 +?????#?????.???????. 6,3 +??#?#?????#.? 1,3,1,1 +??###..????.??#?#. 1,3,1,1,5 +??.??#??#. 1,2,1 +?????????????#?? 6,1,2 +?????##??????.#? 3,2,2,1,1 +.???????##?#..??#?. 5,4,3 +.#..????.??##?????.# 1,3,8,1 +?#?.???.#???? 3,1,2,1 +??????.??#??.#???. 2,1,2,1,1 +.??#?.?#?? 3,2 +#???.?..?..???##? 2,1,1,6 +?#.#.??????#? 1,1,6 +??.?????.?...???#? 1,4 +.?.???#...??##?.. 3,4 +..?###..???#??.???? 4,2,1,1,1 +..???.????## 1,1,5 +..?#?????? 2,2 +.???????????? 2,1 +#??#????#??????? 5,5,1 +???#??.????#?#.?#??? 5,1,5,2,1 +?#?#??????#??##?.??. 6,8 +?.#?#.?.?????? 1,1,5 +.#???#?#??????.????# 1,3,1,3,1,1 +.??????#??????#.?? 2,5 +..#.??????#???? 1,1,1,3,1 +.#.??#????.? 1,1,1,1 +?#?#????#????.?#? 1,2,3,1,3 +??.#?????.?.?? 1,1,3,1 +?.#??????? 1,5 +??#.??????##??#??? 1,5,7 +.???????#????##.??. 14,1 +????###???.?.????# 2,6,1,1,3 +?#?#?.????.##. 4,1,1,2 +?#????.?##?#?#? 4,6 +?????#?#?.?####?? 1,4,5 +#??????.#??#???# 3,2,1,6 +#?.??.#??????.? 2,7,1 +?????.???.?????? 2,1 +??????????? 2,1,3 +?#??.??????. 3,5 +?##????.??...# 3,2,1,1 +?#.?.#???? 1,2,1 +##??????.?????? 4,2,1,2 +??#??#?????????#. 12,3 +????.?.#?## 3,4 +.??#?????.. 3,1 +??#.?.?#.#??#?# 3,1,1,2,3 +???#?.??????#?? 3,3,1,1 +??#?#?##.????#..? 8,1,2,1 +#??#???#?#?#??#? 1,2,10 +??#??##??.#?.??? 1,2,1 +??#????##?? 2,7 +??#?#.?#??.#??#??? 4,1,1,1,1,1 +....??#???..???#??? 6,4 +??##???????#?? 3,3,3 +?#????#?.??.## 2,4,1,2 +.???.????.?#??.? 1,1,1,1,4 +?###????##??#?#?? 12,3 +???#?.????#?? 5,5 +??##???????????. 6,1,1 +?????.?#?##???###? 2,1,2,2,1,4 +??.?.#?.?#???#? 2,1,1,3,1 +???????##???.??? 9,3 +?.??#??#??#?????? 1,12,1 +..??????#?#?????? 1,9 +??.?#?#???? 2,4 +?.?.??#??.? 1,1 +???#?.??????.?.?.?? 5,2,1 +?#?#?##???..??? 9,2 +?#?.???#?#?.? 2,3,1,1 +??????.??? 4,2 +?.??..?.?.????.??. 1,1 +..??##.??#? 2,1 +..??.????#. 1,5 +.???.?????#? 1,1,6 +?#??#??#??#?# 5,1,2,1 +?????#???..? 1,5 +.#??????????? 2,2,3 +??.#?.?.?. 1,1,1 +???????#??.???.#? 1,4,2,2 +????#???????.?..?? 1,3,2,2,1,2 +??.###?#?..?#???? 5,2 +#????##?#?##??. 1,3,1,4 +?.??.?#??. 1,1,3 +.#?.?????#??##???? 2,1,1,6 +?.?????##.??? 4,2,2 +.?.???##??? 1,4 +??#.???????#?.?????? 3,3,5,2,1 +???##?#??.??? 3,2 +??##?#????..# 7,1,1 +???????.???????.#.? 1,1,1,7,1 +?????#??#????#?? 8,4 +????#.#####?#??? 2,1,8 +???????#????##?#?.?# 16,2 +?.????#??.#??###?#?? 2,3,9 +????????#?.??????.?? 1,6,1,2,1 +?.#??#????.?#?????.? 5,1,7 +?.#.##?##??.#.??.. 1,1,6,1,1 +#????????.??.#?? 1,4,1,1,3 +??..?#?###?#??#???? 1,8,2,1 +.?????#?#???.??. 2,6 +?#?????.#????? 6,1,1,1 +.??????????? 2,1,1,1 +#?#.#??#?.????#??# 3,5,1,1,2 +?#????.#?##??##? 2,1,8 +?????#?.???### 3,4 +.????#???..?##?????? 5,7 +???#?.????#####???# 1,2,10 +????.?#??# 3,4 +???#.#?.??###.#?#? 2,2,3,1,1 +?#???.?????..#?. 1,1,2,1 +##???????## 3,6 +####.???.# 4,1 +?##???##?#???.#?#??? 10,5 +..??????.??.?# 2,1 +??????.#?.? 2,1,1 +.?#??????##?##. 5,5 +??..???##??#.??. 1,8,1 +.#????.#??. 2,1 +#??.#?.????.? 3,1,3,1 +#?#?????.???.?.?. 5,2,1,1,1 +????.??.??#?#??.?#? 1,1,1,6,1 +..?.?..???? 1,1 +?##??????#????.?? 3,5,1 +???.????#??## 1,2,1,3 +???????#?????#??? 4,2,3 +???#?.?.##?#?????.? 1,1,1,9,1 +.#??#?#??..?? 2,4,1 +?.??.???.??#?.?##?. 1,2,1,4,3 +???.?#??#??? 2,6 +.##??#????? 2,2,2 +???????????#? 3,1,1,2 +.????#.#??????#. 1,2,5,1 +?.##?????..???#????? 1,7,1,1,2,1 +??.#??#??????#?????# 1,4,1,10 +?????????.? 1,4 +???#?#?#.??.?????#? 6,1,1,3 +??????.?##?# 5,5 +#??..???#????? 3,8 +?#.??.??#?#. 1,1,1,1 +??.????.?#??.#? 1,1,3,2 +..#???.???.???.#. 4,2,2,1 +?????????.?#?.???? 1,3,1,2,2 +??##?????#?#???.???? 2,9 +?##??????#. 3,1,2 +.?#??#??#?????? 1,6,1,1 +.???#.???###????#??. 4,6,2,1,1 +?#??#???#?#????.? 2,1,5,3 +?#?.?????#.?#?#?? 2,1,1,2,6 +#?.#?.#??????#???.# 1,1,1,1,6,1 +?.?##????#?? 4,2 +####???????# 5,2,2 +???.?????#??? 2,1,1,1 +?.????#.#??#???? 1,2,1,5,1 +##?.#?#???#?????#??# 3,8,1,1,2 +..???##.?? 1,2,2 +??#????#?#?#?#.???#? 3,1,7,1,2 +.???.#?????# 2,2,1 +..?#????.?? 2,3 +?????.??????? 1,1,2,1 +?????###?#?..##???#? 8,6 +?#?.??????????? 1,1,1,1,3 +??##?.?##? 4,2 +#??##?.?????#????? 1,4,1,6,1 +????#??#??? 1,7 +???????#???# 2,6 +??##????#?#??????.? 1,2,3,1,1,1 +?.?.???##??#? 1,1,5 +.???#??.??.. 1,3,1 +???????###??#?##?. 1,12 +??.????.?# 1,1,1 +??#???##??..#?.#. 3,4,1,1 +??????.#?????#???? 6,2,2,2 +###..????#?#..????#? 3,2,3,2,3 +#?#????#??#????? 6,4,1 +?????.????.????.??? 2,1,3,4,1,1 +???.?#??#?? 2,4 +.??.???#??#???##??#. 1,6,3,1 +?.???.?.???? 3,3 +.?#?.??#???????. 3,9 +????##?#?.?#?.# 1,4,2,2,1 +??..???##?? 1,7 +?..????.???????# 3,4 +?.?..??#.? 1,1 +.??#??#?#?????.?? 7,2 +???##????.?#??.?.#?. 6,2,2,1,1 +?#?###??????.?#?.?. 9,2 +.????.????##..#?#??? 2,1,3,4,1 +??..??.??? 2,1 +.??#??????.?#?.? 1,1,3,2,1 +??..??#?##.?..? 2,5,1,1 +?.#.??#?#? 1,5 +.???#???.?????#??#? 3,1,1,2,2,1 +?????.?.???#?? 3,5 +?.??#???##?#???# 1,13 +??????.?..???????. 4,1,2 +????.#?#??#???#???? 2,4,7 +.??.??.?#.?? 1,1,1,1 +#.????#??? 1,7 +????????##???.??#? 3,6,1,3 +.?????##?##???????? 14,1 +.?.???.##? 1,3 +???????#?##??.? 1,1,1,6,1 +?#?.????#?#.#??.??# 2,4,1,3,1 +??.??.??#? 1,1,3 +.??##??.?#??#?#?#?.? 4,7,2 +??#???????#???# 4,1,3,2 +#??#????##???? 1,11 +?.#?????#?? 2,2 +.????#???###??#? 3,10 +????.?#???.#? 4,2,2,1 +??.????.?..#.??? 1,4,1,1,1 +.??.????#??.?.?#?#? 1,2,3,1,2,2 +.????????## 1,1,3 +?##?????#??#?##?## 9,1,2,2 +?##??????????.???? 7,1,1,1,1 +??.?????#.??.? 1,4,1,1 +.????#??#??#.?#????? 11,7 +??.?.?#?#???????? 2,7 +?????####????.?#. 9,2,1 +???????.??? 1,1,2 +?????.??..#?.## 2,1,1,1,2 +??????#?.??????? 2,3 +?????#.#?##??????#?? 5,6,5 +?###??.??????..??? 4,4,2 +?##???????? 4,2,1 +.??????#??????.?.?#. 13,2 +.?.#?#???#????#?? 1,4,5,1,1 +??????????. 1,6 +?.???????#?????#? 9,2 +#??.??.???????????. 3,4,2,1,1 +???#??..????#.??##?# 3,4,3,1 +????#??..?#. 5,1,1 +????#??.???.?? 1,4,1,1 +?#?????.????#? 1,2,4 +?#??#.??????#?.????? 3,1,2,1,1,3 +?.?????.#??? 1,2,1,1 +.??.????##??.??????? 2,7,1,2 +???#?....?????#???? 2,6,1 +.?????.??###?..? 5,4 +.#??###?#?.???? 9,1,1 +?#?.#????.?..??##?#? 2,1,1,1,1,5 +.??#??#?#??.#?.??.? 1,8,1,2 +??.??.?#???#?#??#?? 1,11 +#????###??##.? 8,2 +????.#?#???. 1,1,4 +??#??#?.#?#?##??# 1,1,1,7,1 +#?#?.???#??..#. 4,1,1,1,1 +.??#?#??#?.?? 5,2 +?.??????#?? 1,3,1 +???.?????# 1,1,2 +??#..#?.##? 3,1,2 +?.?.#?????????#?# 1,7,2,1 +#??#?#?#?..?.????? 4,1,2,1 +?.?#.?..#.?.. 2,1 +????????###.?#??? 2,1,1,3,4 +#???#?????#????...? 1,11,1 +.#?#?#????#????#?# 1,1,7,5 +?.?#.?.??.?. 1,1,1,1 +.?#.????## 1,1,3 +???.????#???.??...? 3,6,1 +?.?###???#????#?#? 1,5,1,2,1,1 +????????#??.? 5,2,1,1 +???.?.?.?? 1,1,2 +..?#?..???? 3,2 +?.#??#?####???#.?? 1,11,1,1 +??.##????#????. 1,4,1,2 +????????.?#??##? 2,1,1,1,3 +?????#.??????.? 3,1,1,2 +????.?#?..?# 1,1,3,2 +#??.???????#???#.? 1,1,2,1,6,1 +###????#?? 5,2 +##??#.??????#?.?# 5,1,4,1 +#?..?#?.??#?.?#? 2,2,3,3 +??????.?####? 1,1,6 +??.???????..?.???## 2,1,1,1,1,5 +?..???#???????#?. 4,5 +???#??.#?? 3,1,1 +??#??#?.???? 5,1 +?#??????#?. 2,2 +?.???.?????#?#. 1,2,1,5 +?.???????????? 2,1,1,2 +.?##?.???#?#? 3,4 +???.??????#??.#???? 3,7,2,2 +?#??????##??.?#?#?? 11,2,2 +??.?.????# 1,4 +????.?.?????????? 3,4,1 +???????#???##?? 8,3 +?..?.??#?????#?.?? 1,9,1 +.????#???#?.? 5,3,1 +..???##??##?#? 2,9 +?#????????#??. 2,2,4 +??..??#??#??#???##?. 1,1,1,5,4 +?????????#?????#??#? 2,1,10,2 +?????#??#?????#???.# 4,1,6,1,1,1 +??????#??# 4,2,1 +?#?..??.?#???#?##?# 2,1,11 +?#?.????#? 3,3,1 +??#?.???????? 2,1,2,1 +???.????##???.### 2,1,6,3 +.????####??.#? 1,8,1 +?.???#??#?.?. 3,1 +...??.???#?#?#?.? 1,6 +#??.?#?.?#.?????? 3,1,2,1,1 +#?????.##?#??.?. 2,2,5 +?????#?.????.?# 6,1,1,2 +?????#.?#.????? 3,1,2,1,1 +????#????.????? 3,1 +##???#?.?. 2,2 +?????.#?.#??#?#?.??? 1,1,1,1,5,1 +??#?.??..? 4,2,1 +?#????#???#??.. 2,1,6 +.?#?????#?.????? 8,1 +??#???..?. 1,1,1 +.?#??.??.#.??? 4,1,1,1 +?####?#?.??#.???. 7,1,2 +.##????.?? 2,2 +??.#.??#??.?#????#? 1,1,2,5,1 +???.????#?#?# 1,1,6,1 +?##????.#?#?.??#?#? 2,1,3,4 +#??##??#.???? 1,6,1,1 +##?##?????? 2,3,1 +???????#.##?##?? 2,3,6 +.???#???????? 8,1 +??#?##??..? 7,1 +#???????????? 2,5,1,1 +?#??##????#??? 5,6 +?##????????##?.??? 5,6,1 +??????.?#?#?##. 1,1,2,1,4 +????????.??#???#?? 1,1,1,1,2,2 +..?.#?.?????#?#? 2,1,5 +.?#???#?##?????#?? 2,7,1,3 +??#?.????????? 2,2,1,3 +?????????.??## 7,2 +?#.??.??#?#? 2,1,2,2 +??????????..??#???.? 1,5,1,2,1,1 +##??.???????..???.. 2,7,2 +.#?#?????????##??# 4,5,4,1 +.??.#??#?#?? 1,7 +?..?.##?.???????.? 2,4 +?.??????#??#??? 1,1,1,6 +##?#?.??#????????#? 5,2,1,1,1,1 +.?#?#?.?#??#?.?? 4,6,1 +????????#?????.????. 1,1,4,1,1,4 +.?????.??#?.??? 2,1,1,1,1 +#?#???#?#?? 5,1,1 +??.?#???##?#?# 1,1,6,1 +??????#?##?.?. 2,6 +.?#??##??.????## 8,1,1,2 +.?#?.?????. 2,3 +#??????#????####? 2,4,6 +??.#??#??#?##??? 4,1,2,1 +???###?.?? 1,4 +??.????.???.#?.??.? 2,2,1,2,1,1 +??..???.??? 1,1,2 +#???#?#???##?.??? 7,3,3 +.???#???##??..#?? 10,3 +?????#??????#??? 1,2,1,5 +??????????#???##? 10,2 +.??????###? 1,4 +?.??#??#??.###???? 1,5,1,3,1 +?????.?????#? 3,2,1,2 +#??.??#???#?. 3,8 +???#??????#?. 3,6 +??.?????#??????.? 1,4,3,2 +??##???##?#????.?#? 6,2,2,1,2 +?.??.????#??# 1,1,2,2 +?????..??.???#?#?.? 1,1,2,6,1 +?????##???????.? 8,1,1 +????..????#???.##?? 3,7,3 +?????#???#????#???? 2,11,1 +?????.???#.????.?? 1,1,1,4,1,1 +.?.?#???????.????#?? 9,2 +???#?#?#???#. 1,1,3,3 +##????#?.??.?????. 8,1,1,1,1 +?????????? 3,1 +.?#?.?.#??.. 2,1 +.????.??#?? 3,2 +#?#??###?#??#????#? 1,12,3 +##?.?#???.??# 2,3,3 +##?????#????? 3,8 +???.#..????.?#??#? 1,1,3,1,3 +??#.?????.?#?##? 3,3,1,1,3 +#?????????#????????? 11,2,1 +?#?##?????? 5,2 +?.#..?#.?????.???.?? 1,1,2,5,1,2 +??#????#.?? 8,1 +??#?#??#?#.? 1,4,1 +?.??..?.??#??? 2,3 +??????????.?. 2,1 +?????#.??#?.? 2,1,1,2 +.?#??##??#.?? 6,1 +??#??#?.?#??? 1,1,1,4 +#???##?????? 1,1,2,3 +???#.?#???????? 1,1,6,1 +?#????.?##???#??? 4,3,1,1 +..?.???.?.??##?.. 1,4 +????..??#??#??.?? 1,6,1 +?.#??####??.??????? 7,1 +??..?#.#??#??????? 1,2,1,2,1,1 +??.?#???#?##.??#?? 1,3,4,5 +????.???.?#?? 2,1,1,3 +??.#?.??.?..?? 2,1,1 +.#??###...#???... 6,2 +.?????????#.?#??#.# 1,1,3,1,5,1 +???????.?? 1,3,1 +??.##?.##???????#??? 2,3,5,1,4 +??#???#.??###???? 7,4,1 +???????#???.?? 5,1 +??#???#??? 1,1,5 +??????#?..???. 7,2 +????????#?????#? 2,1,9 +????..???#?#? 1,7 +.??????.???##? 1,2,1,3 +?????#????.??? 3,4,2 +?#???##..?????? 3,2,3 +?##??#???????#??? 2,2,1,5 +.??.???#####???????? 1,8,4 +?????#???.??????#??? 1,2,2,9 +.?##?#??##?..?? 2,6,1 +??.??????##???? 1,7,2 +.???????#?? 1,5 +??#?.?#?.? 1,2 +?#??????#??#????? 1,1,4,1,1 +??#?.??.???.? 3,1,3 +?#??..???????#?#? 1,2,2,5 +..?????.?#?. 3,2 +##.??#?####??#???#.? 2,2,7,3 +?.????????. 2,1 +????..??.#?##.?#? 1,1,1,4,1 +?#??#?#??.???#? 8,3 +???.?#??.???#????? 1,1,1,1,5,2 +?.???????? 1,1,4 +#????#????#???.# 1,1,6,2,1 +?#?#???#.#?? 7,1 +##???#?.????????? 2,1,1,9 +#????????..?.?#? 1,1,3,1,1 +.?.????.?????.?? 2,4 +????#.##?? 3,3 +?.?????.?????? 1,5,1,1 +#?.????#???? 2,1,4,1 +?.??.???????## 1,1,2 +.????..?###?.. 1,4 +??#?.?.?#. 2,1,1 +.?#.?#.??. 1,1,1 +.??##??##??#?#? 8,3 +#??.#??.??.#? 3,3,1,2 +#?.?#.#.???#?#??#?.# 1,1,1,8,1 +??????...#?????.# 1,1,1,4,1 +.#.#??##.??##???? 1,5,7 +???#?????# 4,3 +?##????#??????? 6,2,2,1 +#?????.?##.?.??? 3,1,2,1,1 +?..?.#????.? 1,1,1,1 +.?#?.#????#???#??? 1,12 +?#??#?????##? 4,4 +.??.??#?#? 1,5 +??.????#????#??? 4,6 +????#????#. 6,1 +#?##???#?.?#?? 9,1 +#??#??.??#.?..??? 2,1,3,2 +#???????#?#?##?? 4,7,1 +???.?#???##..???#? 2,3,3,1,1 +?????#.?????.? 2,2,3 +??..?#????.#?. 2,2,2 +.???#???..??##?#? 6,1,4 +?##?#.#?????..??? 2,1,3,2,3 +.#???#?????.#.?? 1,1,5,1,2 +#?????#??.??????. 2,5,1,3 +??.?###.###??? 1,4,3 +???.??..????#? 3,1,1,1 +?.?.?????##? 1,1,1,2 +????.?????#?.??? 2,1,3,3 +??#?.?#??#???.??.? 4,5,2,1,1 +###??.#?.?. 5,2 +??#..????#?.????? 2,5,2,1 +????.????? 2,1,1 +??##?.?.????.???? 4,3,1,1 +??????#???.?. 1,2 +.??#?#?#??#??#? 1,1,9 +.???????????#???. 1,4,2,2 +??.?.#??###??????? 1,11,1 +#####?????? 6,2 +.??.???#??#??. 1,3,3 +?????#.?.?#? 2,1,1 +?#????##?#??.???#?? 3,6,4 +?..#?.?#??#???.#??#? 1,1,2,2,5 +#????.??#.??#??.?? 1,1,1,2,5,1 +?##???????#?#?????. 2,9,3 +.????#??.?#?? 4,2 +?????##?#?#?#? 1,10 +?????#??????. 1,1,4 +?.??#??##??##??.? 1,5,3,1 +??#?##.?#??.?#? 5,3,1 +??#??#?#???#?#?#. 3,1,1,4,1 +????#?.?##?????#???# 1,3,5,3,1 +.??#????.?##???#?? 3,7 +???#?.##?.?##?..???? 4,2,4,2 +??.??????????#. 1,2,3,1 +???#???#?? 2,2,3 +.?#?##?#?#??.??#??#? 9,6 +????#?#?#??#?.???? 4,3,2,1 +??#?????????##?.??? 3,2,4,2 +???????????. 1,1,1,1 +#??##???????#?? 9,2 +?##?####??#???.?? 8,1,1 +??##?.??#?#??.? 4,7 +????#?????##??.??? 1,3,7,3 +?.?#?..??.??#?.??# 1,2,2,1,2,2 +??#?#?#??.??? 7,1 +?.?#?#..??? 3,1 +?#..?.#?.?. 2,1,1 +???.?#??.??. 1,3 +..???.#???##?#.? 2,8 +?????#.??# 1,1,1 +??###??????#???#? 8,5 +#????.#?#???#?##?#?# 1,1,1,1,1,9 +?????????.??? 2,1,3,2 +.????..??.? 1,2,1 +?#??.?#.#?.????.? 4,2,1,2,1 +?#???##?????????? 1,5,1,1,1 +.??.???##?????#?.?#? 1,12,1 +??.????.?..?.???. 2,1 diff --git a/2023/12/src/main.rs b/2023/12/src/main.rs new file mode 100644 index 0000000..e96e328 --- /dev/null +++ b/2023/12/src/main.rs @@ -0,0 +1,219 @@ +use itertools::Itertools; +use rayon::prelude::*; +use std::cell::RefCell; +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::{Duration, Instant}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +#[derive(Debug)] +struct SpringRow<'a> { + springs: Vec, + spans: Vec, + cache: RefCell>, +} + +impl<'a> From<&str> for SpringRow<'a> { + fn from(s: &str) -> Self { + let (springs_s, spans_s) = s.split_once(' ').unwrap(); + SpringRow { + springs: springs_s.chars().collect(), + spans: spans_s.split(',').map(|x| x.parse().unwrap()).collect(), + cache: RefCell::new(HashMap::new()), + } + } +} + +impl<'a> SpringRow<'a> { + fn possible_arrangements(&self) -> u64 { + let mut permutations = vec![self.springs.clone()]; + while let Some(cur) = permutations.iter().position(|perm| perm.contains(&'?')) { + let perm = permutations.remove(cur); + let rep_pos = perm.iter().position(|c| *c == '?').unwrap(); + + let mut new_elem = perm.clone(); + + new_elem[rep_pos] = '.'; + permutations.push(new_elem.clone()); + new_elem[rep_pos] = '#'; + permutations.push(new_elem); + } + + permutations + .iter() + .filter_map(|perm| { + let groups: Vec = perm + .iter() + .group_by(|c| **c == '#') + .into_iter() + .filter_map(|(key, group)| if key { Some(group.count()) } else { None }) + .collect(); + if groups == self.spans { + Some(()) + } else { + None + } + }) + .count() as u64 + } + fn memoize_arrangements_r( + &self, + idx: usize, + spans_left: &'a [usize], + cur_span_left: usize, + ) -> u64 { + if let Some(value) = self.cache.borrow().get(&(idx, spans_left, cur_span_left)) { + return *value; + } + let result = self.possible_arrangements_r(idx, spans_left, cur_span_left); + self.cache + .borrow_mut() + .insert((idx, spans_left, cur_span_left), result); + result + } + fn possible_arrangements_r( + &self, + idx: usize, + spans_left: &'a [usize], + cur_span_left: usize, + ) -> u64 { + if cur_span_left == 0 && spans_left.len() == 1 { + // everything is consumed. if we're at the end, we good + // if we have stuff left, as long as it doesn't contain a fixed broken spring we good + if idx == self.springs.len() || !self.springs[idx..].contains(&'#') { + // println!("{:?} valid", self.springs); + return 1; + } + // something invalid + return 0; + } + if idx >= self.springs.len() { + // reached the end of the string without consuming all our spans + // println!( + // "{:?} invalid idx: {} spans_left: {:?}", + // self.springs, idx, spans_left + // ); + return 0; + } + // println!( + // "call idx: {} str: {:?} spans_left: {:?} cur_span_left: {}", + // idx, + // &self.springs[idx..], + // spans_left, + // cur_span_left + // ); + return if cur_span_left == 0 { + match self.springs[idx] { + '#' => 0, // a broken spring after we have completed our span is invalid + // a space or a ? (becomes a space) starts a new span + '.' | '?' => self.memoize_arrangements_r(idx + 1, &spans_left[1..], spans_left[1]), + _ => panic!("invalid spring"), + } + } else { + // we are mid span + match self.springs[idx] { + // cur_span_left != 0, so we still have work to do with broken springs + '#' => self.memoize_arrangements_r(idx + 1, spans_left, cur_span_left - 1), + '.' => { + if spans_left[0] == cur_span_left { + self.memoize_arrangements_r(idx + 1, spans_left, cur_span_left) + } else { + 0 + } + } + '?' => { + if spans_left[0] == cur_span_left { + self.memoize_arrangements_r(idx + 1, spans_left, cur_span_left - 1) + + self.memoize_arrangements_r(idx + 1, spans_left, cur_span_left) + } else { + self.memoize_arrangements_r(idx + 1, spans_left, cur_span_left - 1) + } + } + _ => panic!("invalid spring"), + } + }; + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let rows: Vec = input + .map(|row| SpringRow::from(row.unwrap().as_str())) + .collect(); + + rows.iter() + .map(|row| row.possible_arrangements_r(0, &row.spans, row.spans[0])) + .sum() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut rows: Vec = input + .map(|row| SpringRow::from(row.unwrap().as_str())) + .collect(); + + for row in &mut rows { + let orig_slice = &row.springs.clone(); + for _i in 0..4 { + row.springs.push('?'); + row.springs.extend_from_slice(orig_slice); + } + + let orig_span_slice = &row.spans.clone(); + for _i in 0..4 { + row.spans.extend_from_slice(orig_span_slice); + } + } + rows.iter() + .map(|row| row.possible_arrangements_r(0, &row.spans, row.spans[0])) + .sum() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"???.### 1,1,3 +.??..??...?##. 1,1,3 +?#?#?#?#?#?#?#? 1,3,1,6 +????.#...#... 4,1,1 +????.######..#####. 1,6,5 +?###???????? 3,2,1"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 21); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 525152); + } +} diff --git a/2023/13/Cargo.lock b/2023/13/Cargo.lock new file mode 100644 index 0000000..74e24a1 --- /dev/null +++ b/2023/13/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day13" +version = "0.1.0" diff --git a/2023/13/Cargo.toml b/2023/13/Cargo.toml new file mode 100644 index 0000000..36c9510 --- /dev/null +++ b/2023/13/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/2023/13/input b/2023/13/input new file mode 100644 index 0000000..56b48d6 --- /dev/null +++ b/2023/13/input @@ -0,0 +1,1349 @@ +#..#.#........# +#..######..#### +.##..#.#.##.#.# +#..##.......... +######........# +#..####......## +.##.##.#...##.# + +..######. +..######. +....#..## +##.....## +##..#...# +..##..... +.#..#.#.. +.#..###.. +..##..... + +###..#....#####.. +.#........#.#.### +.#........#.#.### +###..#....#####.. +#.#.#####....#### +.#.###..####.#..# +#..#..#.####...#. +#..#..#.####...#. +.#.###..##.#.#..# + +..###..######.. +...#####.##.### +##....#..##..#. +##.......##.... +.#.##..#....#.. +#########..#### +..###.##.##.##. +..##.#.######.# +#####.#.#..#.#. +..##.##..##..## +###.########### +..#...###..###. +..#.#.#.####.#. +###.#.########. +...#.##.#..#.## + +..#.### +.###..# +.###..# +..#.### +.#..#.. +.##.#.# +####.## +#.##.## +#.#..## +####.## +.##.#.# +.#..#.. +..#.### + +....#..#.#... +.#.#..#..##.. +.#.#..#..##.. +.#..#..#.#... +..####...##.. +..####.#.#.## +.#..##..#.#.. +#..#..##..### +.###.##.###.# +.###.##.###.# +#..#..##..### +.#..##..#.#.. +..####.#.#.## + +.##.#.##. +..##.#### +..####### +###...... +..##.#### +....##..# +###.#.... +.....#### +###.#.##. +....#.##. +..####### +...##.##. +##..##### +##..##..# +..#...##. +...###..# +..###.... + +....#..#######..# +......#.#.###.##. +.##.#.#..#...#..# +#########.##..... +####..##.##..#..# +######.#....##..# +####..#...#...##. +.##.##.....#.#### +.##.##..##.###..# +#..####.##..##..# +.##..###.#..##..# +#..##...#####.... +#..####.#.#.#.##. +.##..#..##..##### +#..#.#...#.#..##. +######..########. +....###.##...#..# + +...#..#........ +.#.#..#.#.##.#. +..#....#..##..# +.#..##..#....#. +..######......# +...#..#..#..#.. +...#..#........ +#...##...#..#.. +.##.##.##....## +...#..#...##... +#..####..####.. +..#.##.#......# +..#.##.#......# +###....###..### +#........####.. + +...##..##.#.# +.##.#####.### +##.#....####. +#.#....#.#..# +.#..#.#...#.# +.#..#.#...#.# +#.#....#.#..# +##.#....####. +.########.### +...##..##.#.# +#.#.###.....# +#.#.###.....# +...##..##.#.# + +....#.#.##. +....####### +######....# +#..#....### +#..#..#..#. +####...##.. +#..#...#..# +#..##..#..# +####...##.. +#..#..#..#. +#..#....### + +.#..##..##..#..#. +..#...##...#..... +##...####...##### +#.##......##.#..# +#.##......##.#..# +##...####...##### +..#...##...#..... +.#..##..##..#..#. +.#..#.##.#..#.#.. +.##...##...##.#.# +.#...#..#...#.#.. +#.#........#.#... +.#...####.#.#.#.# +#.##.####.##.##.. +#............##.# +##...####...##... +#............#### + +#..#.#..# +####..... +.##.##..# +.##.#.##. +#####.##. +.##.#.### +#..#.#..# + +###..##.####..### +###..##.####..### +##..###.#.#...### +#.#.#..##..####.# +#.#..#...#..###.. +..#....####.#.### +#..#...##.###.#.. +.###......####.## +#..#.#...######## + +#.##.#..#...#.. +.#...#.....#... +#.#..#.#.##.### +.#####.#.#..#.# +.#####.#.#..#.# +#.#..#.#.##.### +.#...#.#...#... +#.##.#..#...#.. +#.#.#.###.####. +#..###....###.. +#..###....###.. + +#...##......##. +..###..#..#..## +###.#.##..##.#. +.##.####..####. +..###..#..#..## +...#...####...# +###....#..#.... +###....#..#.... +...#...####...# +..###..#..#..## +.##.####..####. +###.#.##..##.#. +..###..#..#..## +#...##......##. +.##.####..####. +....##.####.##. +.#...##.##..#.. + +.....#.## +...###.#. +...###.#. +.....#.#. +..#.##### +..##..##. +...#...#. +..##.#.## +####..... +.....##.# +##.##.### +####.#... +##...##.# + +..#..#....#..##.. +#.#.#.#####..#### +..#....#..#..#..# +.##.############# +##.###.#..#..#..# +...#.##.##.##.##. +.#.#..##.#.##.#.# +..#..#..########. +##.####.##....##. +#####.#..#.##.#.. +#.####..#.#..#.#. +#.####..#.#..#.#. +#####.#..#.##.#.. + +#.#....## +#...#..#. +.##..#..# +.#.#..### +##.#..### +.##..#..# +#...#..#. +#.#....## +.#.#....# +.#.###### +##.#..#.. +##..##### +...#.#... +...##.... +...##.... +...#.#... +##..##### + +##..##### +.#.##.... +.#.#..##. +.....#..# +.##.##### +.######## +.....#..# +.#.#..##. +.#.##.... +##..##### +..#.#.##. + +#.#.##.#.##...... +.###..###...#.##. +.#......#.###...# +.#......#.###...# +.###..###...#.##. +#.#.##.#.##...... +#.#.##.#.#......# +.###..###.###.#.# +####..####.##.##. +#.#.#..#.###.#### +.#.####.#...#.#.. +.##....##....#.#. +.##.##.##.###.#.. +.#.#..#.#.#.#.#.. +#.#.##.#.#.#####. + +..##.###### +##.#.#####. +##.#.#####. +..##.###### +..###..##.. +....###..## +.#.#....... + +####.#####..##### +#......#......#.. +....#..#......#.. +....#..#......#.. +#......#......#.. +####.#####..##### +...#............. +...#####.#..#.### +.###..###...####. +#.##..#........#. +.#.#.#..##..##..# +..#.#..#.####.#.. +#.#..##..#..#..## + +######.###... +#..#.#.#.##.. +....#.##..... +#..#..#.###.. +...#.#...#### +.....##.##### +.##..#..##... +#..#.#.###### +#######.#.### + +.####..## +#.#...#.. +...###.## +.##..#.## +.##.###.. +.######.. +...#..#.. +#.#..#.## +.#####... +####.#.## +.###.#### +.###...#. +..#....## +.#.###### +##..##... +##..##... +.#.###### + +.##....##....##.. +.###...##...##### +##.####..####.### +..##.##..##.##... +##..#.#..#.#..### +....#..##..#..... +.#..###..###..#.. +#.#...#..#...#.## +...#.######.#.... +#.##........##.## +#.#.#.####.#.#.## + +#..##.#......## +#..##.#......## +..##.###.#...## +.#........##.## +..########.#... +##.#.#..##..... +#.#..#...#..### +##...###.#.#.## +#..#..#.##.#.## +#..##.....##### +##.#.#####.#### +.###..#..#..#.. +....##....###.. +###.#.###.####. +.#...##.#..#... + +.##.##.##.... +..#....#..### +.#..##..#..## +#..#..#..#... +##......##### +.#..##..#.#.. +###.#######.. +##########... +###....####.. +..#.##.#..### +#.######.###. +..........#.. +..........#.. +#.######.###. +..#.##.#..### + +.##...##.#### +#.###.#.#.... +....#.#.##..# +#########.##. +.##..#.#..##. +....##..##..# +#..#.#..##..# +.##.##...#### +#..#....#.... + +#..#..#..##.### +#.###..##...### +##.###....#.#.. +#.##..#.##.#### +.#..######.#.## +#.##.##..#..#.. +##..###....#... +##..###....#... +#..#.##..#..#.. +.#..######.#.## +#.##..#.##.#### +##.###....#.#.. +#.###..##...### +#..#..#..##.### +####..###.#.... +#.#.###....##.. +..###.###...### + +####.###.#### +###..#.#####. +#.###..####.. +#.##...####.. +###..#.#####. +####.###.#### +..##.###..#.. +#....##.#.### +#.##.#..##.## +#.##.#..##.## +#....##.#.### +..##.###..#.. +####.###.#### + +.#.#....# +.#.#....# +.#.#####. +###...#.. +...###.## +....#.#.# +....#.#.# +...###.## +##....#.. +.#.#####. +.#.#....# + +##.#####... +#..#..##... +###...#..## +....#.###.. +#.#..#..### +#.#..#..### +....#.###.. +#.#...#..## +#..#..##... +##.#####... +...##...### +.#...##.### +...#..#.... + +###...##..##... +##..#.#.##.#.#. +..##..######..# +###.##.####.##. +...#.###..##### +##.###.#..#.### +##.####....#### +...#.#..##..#.# +..#...#....#... +###.##########. +##..##.####.##. +###.#.#.##.#.#. +#####..#..#..## + +#.##.##.##. +#.#.#..#.#. +#.#.#..#.#. +#.##.##.##. +#.##.##.##. +#####..#### +.#.#.##.#.# +....##.#... +##..####..# +.##......## +.#........# + +####......##..# +#####.###.##... +....####...#### +.#......##.#... +####.#.#..#.### +##....#.#.#..#. +#..#.###..#..## +#..#.###..#..## +##....#.#.#..#. +####.#.#..#.### +.#......##.#... +....####...#### +#####.###.##... +####......###.# +##....####.#.#. +...####.###.... +...####.###.... + +..##...##..##.# +..##...##..##.# +####..#.#..#... +..##..####.#.#. +..##.#...###..# +##...###..#.### +##..########..# +.#.##.#...#..#. +...##.#.#..##.# +#########.##..# +..#.##.#.##.... +..#####...##.#. +##...##..#.#... + +........#.# +#.....###.# +##....##### +#..##..##.. +..#..#..### +.#....#..#. +.#....#..#. + +..##.## +.#.##.. +#.###.. +..#..## +####.## +.####.. +#.##... +....### +...#... +##..### +#...### + +##.#....######..# +..##.#.##....#### +##...#..#....#..# +#.###..#####.#..# +#####..###...#### +..#.######.###..# +...#.#.####..#..# +#...#..##.#...... +...#...#...###### +...#...#...###### +#...#..##.#...... +...#...####..#..# +..#.######.###..# + +####.##.# +.#..#..#. +#..#.##.# +.##...... +#..##..## +....#..#. +.##.####. +####.##.# +....####. +.##..##.. +....#..#. +....####. +####....# +....####. +....####. +....####. +.##.#..#. + +#.######.###.## +#..#..#..#....# +.#......#.##... +.#..##..#.####. +#..####..#.##.. +##..##..######. +.#.#..#.#.##### +.#.#..#.#..##.# +.#.#..#.#.###.# + +.###.###. +#.#.....# +#.#.#.#.. +.##.##..# +...#.#.## +#..#.#.## +.##.##..# +#.#.#.#.. +#.#.....# +.###.###. +...##.##. +..#####.# +..#####.# + +...#.####..#.#### +##........#.#.#.# +..###...#.##..... +..###...#.##..... +##........#.#.#.# +...#.####..#.#### +#..##.#...####### +....#.##.....#..# +###.##.#.#..##..# +...####.#.#...#.# +..#.#..####....#. +....#.####...#### +###.#..#..#..#.## +.....#######..#.. +....##......#.##. +##.#####.....#..# +###..##.##...###. + +#..##.##### +#..##.##### +.##.#.##... +#...##.###. +#..##.###.# +#######.#.# +#.#..#####. +#.#..#####. +#######.#.# +#...#.###.# +#...##.###. + +..#.####.##..##.# +.#.##.#####..#### +#.#.###.#.####.#. +###..###........# +#...##..#......#. +#..###.#.######.# +.###...########## +#.#.#.#.##.##.##. +######...##..##.. +#.......#......#. +#.###.#####..#### +###...##.#.##.#.# +#.##..#####..#### +..###.#...####... +.#.....##.#..#.## +##....##.#.##.#.# +#.....##.#.##.#.# + +#..#.##....## +#..#.##.....# +######.###... +##########.## +.##..#.....#. +#..#....##.## +......##....# +........#.... +....##.##.#.. +.......#.#..# +......##..#.. +.##..#.###### +.....#.#.###. + +...#.#..### +...#.#..### +.##....#### +###...#.### +....#..#.#. +....####... +...#.#...## +####.####.# +....#.##.#. + +..###..##.. +.#.######## +.###....### +..#..#####. +####...#.## +####...#.## +..#..#####. +.###....### +.#.######## +..###...#.. +##..#...#.. +.#.....#..# +########..# +.#.##.#.... +.#.##.#.... +########..# +.#.....#..# + +#..##.. +..#..#. +.###### +..####. +..####. +##....# +.#.##.# +.#.##.# +##....# +..####. +..####. +.###### +#.#..#. +#..##.. +#.#..#. + +.##...##.## +..##.##.### +###...##... +....#..##.. +....#..##.. +###...##... +...#.##.### +.##...##.## +..#....#.## +.###..##.## +.##....###. +.##....###. +.###..##.## +..#....#.## +.##...##.## + +.....#.##...#..## +#.#...###..###### +###.#...#.#.##### +###.#...#.#.##### +#.#...###..###### +.....#.##..##..## +#####.#.###.#..## +##.#.####..###### +#.##.###.#....... + +###.#..#..# +..#.#..#.#. +..#.#..#.#. +###.#..#..# +...#.#..##. +###..#.###. +..#..###.#. +##.#.#...#. +.#.###..#.. +###.##..### +..#....#.#. +##.#.##.#.# +..#...#...# +....#.#...# +..#..#.#### + +.##.####.##.####. +.##.####.##.####. +..#....##..##.... +#...###......###. +..#.#.#.#..#.#.#. +##..##.#....#.#.. +#.#.##.#.##.#.##. +#....###....###.. +........####..... +.#.#.....##.....# +###.##...##...##. + +#.#####.##.## +...#...#..... +.##.#.##..### +.###.##...### +#..#..##....# +..###...#.##. +..###...#.##. +#..#..##....# +.###.##...### +.#..#.##..### +...#...#..... +#.#####.##.## +#.#####.##.## + +.#.#.#..#..##.. +.#....##.###..# +###..##.#.##.## +.#####.#######. +.#####.#######. +###..##.#.##.## +.#....##.###.## +.#.#.#..#..##.. +########....#.# +#..##.##.##.#.. +#..##.##.##.#.. +########....#.# +.#.#.#..#..##.. +.#....##.###.## +###..##.#.##.## + +..#..##.... +#..#....##. +##.##...... +###...#.##. +#.#..###### +#.##....##. +.##.#####.# +..##.#.#..# +..##.#.#..# + +..##..##.....###. +#.#....#.#..#.#.. +#...##...####...# +..#.##.#..##..#.# +.###..###....###. +##.####.######.## +..######..##..### +#........####.... +#.#.##.#.#..#.#.# +####..####..####. +##.####.######.## +.#......#.##.#... +#..####..#..#..## +.#.####.#.##.#.## +.###..###....###. +...####...##...## +#...##...#..#...# + +#.#..#..# +.###.##.# +.###.##.# +#.#..#..# +#.##..### +.####.##. +.###..#.# +##.#..### +.#.#..... +.#.#..... +.#.#..### +.###..#.# +.####.##. + +#...###.#.##.#### +##..##.###..##..# +..##...###...#..# +#.##.###.##...##. +#.##.###.#....... +..##....#..##.... +......###....#..# + +.#.....##.... +.#.....##.... +......#..#... +#..#...##...# +#.#.#..##.##. +##..##....##. +##.#.##..##.# + +####.##.. +.##..#... +.##..#... +####.##.. +####..#.. +#..#..#.# +.....#.## +#..#.##.. +.....#..# +#..#.#.#. +....###.# +#####.#.# +####..... +####.##.. +....#..## +..#.###.. +#..##.#.. + +....##.## +..#....#. +..#...... +....##.## +.....##.. +##.#.##.# +#...#.##. +#..#.##.# +.#.##.### +.#..##.#. +.#..##.#. +.#.##.### +#..#.##.# +#...#.##. +##.#.##.# + +#.##########.## +.#..######..#.. +####.#..#.##### +###.#.##.#.#### +...#..##..#.... +.##.#.##.#.##.. +..#..####..#... +#.#........#.## +..###.##.###... +..#...##.#.#... +###...##...#### + +#.#.#.#.. +####.##.. +.#....### +.#....### +####.##.. +#.#.#.#.. +.#####... +.####..#. +..####... +.#....... +#..###### + +#..#......#.. +#####....#### +#.###.##.###. +#.###.##.###. +#####....#### +#..#......#.. +###.######.## +#.##########. +..###.##.###. +.#..#.##.#..# +.#...#......# +#.#..####..#. +###.##..##.## + +.####..######## +#.##.#..###.... +..##..#.#..##.. +.#..#...#...... +.####...##.#### +##..##########. +##..##..#..##.# +##..##..#..#### +##..##########. +.####...##.#### +.#..#...#...... +..##..#.#..##.. +#.##.#..###.... +.####..######## +######....###.# +..##..#.#.#.### +.#..#.....##.#. + +......#..#. +.......##.. +##..##....# +######....# +######....# +#.##.#....# +#.##.#....# +#.##.###### +...#....... + +##..#..#..###.. +##..#..#..###.. +.###....###..#. +#...####...#### +####.##.#####.# +...##..##...##. +.##..##..#....# + +.#..#.##..###.#.# +#....###.#.##.##. +#....###.#.##.##. +.#..#.##..###.#.# +######.#.#...#.## +.......##.#.#.... +#########..#..... +.#..#...#...###.# +.####.###.#....## +#....##.##..#...# +#......##.####..# + +.#...#.###.## +..######..... +###..###.#### +#...##.#...## +.#####.#.##.. +.##..#..#.#.. +#.##...###.## +##..#.###.### +###..#...#### +#..#..##..... +...##..#.#### +...##..#.#### +#..#..##..... +###..#...#### +##..#.###.### +#.#....###.## +.##..#..#.#.. + +.#..##.#..#.#.#.. +...##..##....#.## +..#......#......# +..##....##....#.. +.##..##..##.....# +#.#..##..#.#...#. +#.#..##..#.#...#. + +##..... +.....#. +.###.#. +.###.#. +....##. +##..... +#..##.# +#....## +#....## +#..##.# +##..... + +##..### +..###.. +##.#.## +.####.. +..#.#.. +.#.##.. +.#..... +.#..... +.#.##.. +..#.#.. +#####.. +##.#.## +..###.. + +####.##.....###.# +#####.......#..#. +#####.......#..#. +####..#.....###.# +#..####..#.#.##.# +.##..#..........# +######..#.##....# +#..####...###...# +####..#..###..... +.......#.##.##... +....##.###.##..#. +#..##..###...#.#. +....#...#.##...## +.##........##.... +.....#.####..##.# +#..#..#..#.#.#..# +#######.#.#.#.... + +##.##.##. +#.#..#.## +#.#..#.## +##.##.### +.#....#.. +##.##.### +.##..##.. +.#....#.# +#.####.## +.######.# +.#.##.#.# + +.#.#..#.#.##. +#..#..#..#### +..#.##.#..##. +##..##.###### +#..#..#..#### +.##....##.... +#..#..#..#### + +#.##.####.####### +..#.#...###.####. +.#.#....##...##.. +..#.###.##..####. +##.#####..#.#..#. +.##.##.######..#. +.##....#.#..####. +...#..##.##.####. +...#..##.##.####. + +#.#..#. +.##..## +###..## +###..## +.##..## +#.#..#. +.#.##.# +#.#..## +.#....# + +...#.....#.## +..#.#.#....#. +#####..##.##. +####...##.... +#####.#.#..#. +..####..###.# +..#.#...#.... +..####..##... +...####.#.### +##.##.####..# +##..#....#.## +##.###....#.# +..##.#....### +..##.#....### +##.###......# +##..#....#.## +##.##.####..# + +.##.#..#.##.. +.##.#..#.##.. +..#######...# +.####..####.# +..########... +#.###..###.#. +#...#..#...#. +#####..#####. +.###.##.###.. + +#######...... +....#...####. +####.#.#....# +.#.#..##.##.# +####.###....# +#.###.#.#..#. +...#.####..## +.##.###...... +.#..###.#..#. +.#.#.##..##.. +..#..##...... +#.......#..#. +##...##..##.. +.###......... +.##.......... + +##..####. +.###.##.# +#..#....# +#..#....# +.###.##.# +##..#..#. +###...... +#..#.##.# +...#.##.# + +######..... +#..###..### +####..##..# +#####..#..# +#..#....##. +.##...#.... +####..##..# +######..... +.##..#..##. + +..###.#.####.#.## +##.##..#....#..## +..#..###....###.. +.....##......##.. +....#.#..##..###. +##.#...#.##.#...# +..##...#....#...# +..#....#.##.#.... +####..#.####.#..# + +.########..#### +#...##...##...# +#.##..##.##.##. +##..###.####.## +#..####..##..## +##..##..####..# +..##..##....##. +...####......## +#...##...##...# +#.#....#.##.#.. +.#.####.#..#.## +..##..##....##. +.########..#### +##.#..#.####.#. +#..#..#..##..#. + +##..#.... +###...### +.###.#..# +..###..## +..#...... +##.#....# +##.#....# + +####.#. +#..#... +###.### +...##.. +.#...## +##.#.#. +##.#.#. +.#...## +...##.# +###.### +#..#... +####.#. +##.##.. +###..## +.....## +.....## +###..## + +#####.###....## +.######.#.##.#. +..####..######. +##....###.##.## +...##...#.##.#. +.######...##... +#......##....## +.#.##.#........ +..#..#..#.##.#. +###..###.####.# +#.####.##.##.## +..#..#......... +..####......... +###..###......# +.##..##.#.##.#. +###..#####..### +...##...##..##. + +#...#.### +.#.##..#. +#....###. +....###.# +#.######. +#.######. +....###.# +#....###. +.#.##..#. +#...#..## +...#...#. +...#...#. +#...#..## +.#.##..#. +#....###. + +#.#.... +#####.# +.####.# +.##..## +......# +###.#.# +##.#.## +...#..# +...#..# +##.#.## +###.#.# +...#..# +.##..## +.####.# +#####.# +#.#.... +#.#.... + +...##.# +###.#.# +...#.#. +..#.#.# +..#.#.# +...#.#. +###.#.# +...#..# +##.#.## +####.## +...#... +##..... +...#.#. +##.###. +####... + +.#.#.#...##.. +.#.#.#...##.. +.##..##.##..# +#.###.###.#.. +#########..#. +..##..#..##.# +...##...###.# +###.####.###. +.#####.#.##.# +.#####.#.##.# +###.####.###. +..###...###.# +..##..#..##.# + +.####...... +.####..#..# +#.####.#..# +.####..#..# +..##..##..# +.#..#...... +#.##.#.#### +.#..#..#..# +.#..#.##### +#.##.##.... +.#..#.##..# + +.######.##....# +#.####.#..#..#. +.#....#...####. +##########....# +........##....# +.#....#.#.#..#. +#.......###..## +.#.##.#.##.##.# +#..##..##.####. +###..#####....# +.######.##....# +##....###.#..#. +..........#..#. + +.##..###.## +####.##..## +######.##.# +#..#...##.. +.##..#....# +#..#...##.. +.##..###### +####.###### +.##..###### + +..#.### +#.#.### +..#.### +#.##### +.##.#.. +##...## +#.#..## +#..##.. +...#### + +......##... +.#..#.##.#. +##..##..##. +.#..#.##.#. +#....####.. +.####....## +#....#..#.. +#....####.. +######..### +##..#####.. +#.##.####.# + +##...#....#.. +...##......## +##.#.#....#.# +.############ +.############ +##.#.#....#.# +...##......## +##...#....#.. +#....#####... +..###.####.## +#............ +#.#..##..##.. +.#.#.######.# + +#.##......#.##### +.#.#..####.#####. +##..##.##.##.#..# +..##.##..#.#.#.#. +..##.##..#.#.#.#. +##..##.##.##.#..# +.#.#..###..#####. +#.##......#.##### +..#......#..#.### +########.#...#.## +########...##.### +####......#.##..# +#.##.#.###..####. +#.##.#.###..####. +####......#.##..# diff --git a/2023/13/src/main.rs b/2023/13/src/main.rs new file mode 100644 index 0000000..b4887e8 --- /dev/null +++ b/2023/13/src/main.rs @@ -0,0 +1,227 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::ops::Index; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +#[derive(Debug)] +struct Pattern { + mirrors: Vec>, +} + +impl Pattern { + fn width(&self) -> usize { + self.mirrors[0].len() + } + fn height(&self) -> usize { + self.mirrors.len() + } + fn col(&self, idx: usize) -> Vec { + self.mirrors.iter().map(|row| row[idx]).collect() + } + + fn find_horizontal_reflection(&self) -> Option { + (1..self.height()).find(|idx| self.is_horizontal_reflection(*idx)) + } + + fn is_horizontal_reflection(&self, idx: usize) -> bool { + let row_pairs = std::cmp::min(idx, self.height() - idx); + (0..row_pairs).all(|offset| self.mirrors[idx - offset - 1] == self.mirrors[idx + offset]) + } + + fn find_vertical_reflection(&self) -> Option { + (1..self.width()).find(|idx| self.is_vertical_reflection(*idx)) + } + + fn is_vertical_reflection(&self, idx: usize) -> bool { + let col_pairs = std::cmp::min(idx, self.width() - idx); + (0..col_pairs).all(|offset| self.col(idx - offset - 1) == self.col(idx + offset)) + } + + fn find_smudged_horizontal_reflection(&self) -> Option { + (1..self.height()).find(|idx| self.is_smudged_horizontal_reflection(*idx)) + } + + fn is_smudged_horizontal_reflection(&self, idx: usize) -> bool { + // Same algorithm for problem 2, but count errors breaking reflection. + // If the count == 1 after all checks, then it is our smudge + let row_pairs = std::cmp::min(idx, self.height() - idx); + (0..row_pairs) + .map(|offset| { + self.mirrors[idx - offset - 1] + .iter() + .zip(self.mirrors[idx + offset].iter()) + .filter(|(a, b)| **a != **b) + .count() + }) + .sum::() + == 1 + } + + fn find_smudged_vertical_reflection(&self) -> Option { + (1..self.width()).find(|idx| self.is_smudged_vertical_reflection(*idx)) + } + + fn is_smudged_vertical_reflection(&self, idx: usize) -> bool { + let col_pairs = std::cmp::min(idx, self.width() - idx); + (0..col_pairs) + .map(|offset| { + self.col(idx - offset - 1) + .iter() + .zip(self.col(idx + offset).iter()) + .filter(|(a, b)| **a != **b) + .count() + }) + .sum::() + == 1 + } + + fn reflection_cost(&self) -> u64 { + if let Some(refl) = self.find_horizontal_reflection() { + 100 * refl as u64 + } else if let Some(refl) = self.find_vertical_reflection() { + refl as u64 + } else { + panic!("no reflection"); + } + } + + fn smudged_reflection_cost(&self) -> u64 { + if let Some(refl) = self.find_smudged_horizontal_reflection() { + 100 * refl as u64 + } else if let Some(refl) = self.find_smudged_vertical_reflection() { + refl as u64 + } else { + panic!("no reflection"); + } + } +} + +#[derive(Debug)] +struct Patterns { + patterns: Vec, +} + +impl From> for Patterns { + fn from(mut lines: Lines) -> Self { + let mut patterns = Vec::new(); + let mut cur_lines = Vec::new(); + while let Some(Ok(line)) = lines.next() { + if line.len() == 0 { + patterns.push(Pattern { mirrors: cur_lines }); + cur_lines = Vec::new(); + } else { + cur_lines.push(line.chars().collect()); + } + } + patterns.push(Pattern { mirrors: cur_lines }); + Self { patterns } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let patterns = Patterns::from(input); + + patterns + .patterns + .iter() + .map(|pat| pat.reflection_cost()) + .sum() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let patterns = Patterns::from(input); + + patterns + .patterns + .iter() + .map(|pat| pat.smudged_reflection_cost()) + .sum() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..#"; + + #[test] + fn problem1_find_horizontal() { + let c = Cursor::new(EXAMPLE); + let patterns = Patterns::from(c.lines()); + assert_eq!(patterns.patterns[1].find_horizontal_reflection(), Some(4)); + } + + #[test] + fn problem1_find_vertical() { + let c = Cursor::new(EXAMPLE); + let patterns = Patterns::from(c.lines()); + assert_eq!(patterns.patterns[0].find_vertical_reflection(), Some(5)); + } + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 405); + } + + #[test] + fn problem2_find_horizontal() { + let c = Cursor::new(EXAMPLE); + let patterns = Patterns::from(c.lines()); + assert_eq!( + patterns.patterns[0].find_smudged_horizontal_reflection(), + Some(3) + ); + assert_eq!( + patterns.patterns[1].find_smudged_horizontal_reflection(), + Some(1) + ); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 400); + } +} diff --git a/2023/14/Cargo.lock b/2023/14/Cargo.lock new file mode 100644 index 0000000..851f355 --- /dev/null +++ b/2023/14/Cargo.lock @@ -0,0 +1,73 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day14" +version = "0.1.0" +dependencies = [ + "ndarray", +] + +[[package]] +name = "matrixmultiply" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +dependencies = [ + "autocfg", + "rawpointer", +] + +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" diff --git a/2023/14/Cargo.toml b/2023/14/Cargo.toml new file mode 100644 index 0000000..1299aab --- /dev/null +++ b/2023/14/Cargo.toml @@ -0,0 +1,9 @@ +[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] +ndarray = { version = "0.15.6" } diff --git a/2023/14/input b/2023/14/input new file mode 100644 index 0000000..8ef0b86 --- /dev/null +++ b/2023/14/input @@ -0,0 +1,100 @@ +O..#OO..#...O..O#O.O...O##.O#..#O#...O.O.....O..........O..#...#.....O.O.O.O#..O........O........... +.....O#.#O..OO.#......O.O..#...#..OO..O..###..#...O.#...#..O..O.#..#O...#O...............##O..O....# +..#...O...OO.#........O......O#O#......O.#OO....OOO..#...O....O.O.O#..#OO.##...OOO#.#.OO.#O.#....O.. +..#...#..#....O...#.#O......O.....#O##.#..#.O..#.....#O.O#...#.......O.....O.##.O.O..#.....O.#.O.OOO +O....#..#.O.O..O.#.#....#.#..##O...O#..O.#.O.OOO.O.O..O..............O.#O.O....#..O.O..O..O.O.O.O.O. +.O.#.#..#.OO.#O....O.O.O#O..O.O.O..O.##..O...OO#.#...O.OO.O..#..#..#.O#..#.....#OO....O.###....#O... +.......#.O.......O.O####OOO..#.O#..#.O.......OOO..O....#........O##....O#O....O...#.O.##OO.O..OO...# +..O.....O.#O#O....O......O..##.#...O.#.O##.....O.#..O.........#...#.#.O#OO.....#O.....O#..##.....O.. +.#..O...##....O##O...#O....O..#...O...OOO..OO...OO...#..O.OO#..#.O...##.#..O..O..........O#..#O.O#.. +#..#OO#.O#.#........#.........O......##....#...O....#O.....O..##..#O.OO#......O..O.O.OO...O#O....... +#........#.....O.#....O...OO..OO......###..........##O##O.#.....#....###...#.#...O...#.O#.........O. +...O.OO.....O..#...O.#.O#.OO#OO..#.#OO#......#.O.O#..#..OO..#O.O.....O.#..........##..O#.OO.....#... +.O#..#.....O.#..O.....#O...#....#.O.OO....O...#.#..O......#.O...OO..#.O...O#......##O.OO...#....O.## +O#O......O.O....O.O#OO.......OOO..#..#..OO.#.##O#.....#.#O.O.........##........#O....##..#O.O#.#.O.. +.....O.OO.O...O..O...OO.#.......##....O..#....#.......O.O#.#.O...O.#O....##...#.O..#.#....O#........ +O..#.#O...O.O..OO.....#........OO.#......O.O....O..#.O#.#.O.#....OO.O#..OO.....#.##.####.O##...O.O.# +...O..##O....#....O.O.....#......OO..#OO...#O#..O.......O.#.O.O...O.#.O......#...O...#O...#..##.O... +OOO..#.#....#..#.O.#O#......O......OO.......O..#..#.O#.....O.......#.OO.O.O.......#.O#.O............ +.O##O.....OO..O..O....#.O...#.#.#O.#.O#.O.#...##O..OO.O#......O.OO..#O.OO..O.O...O.O#......#...OO#.. +..#.OO#..#O....O...O...#........#.O.......O#..O..#.#O.O#...O.............OO...#...O...#....O..#.O... +O..#..##O..O...O.O#.O.#..O.O.....#..O.##.##.O..#..OO...#...#.OOO......#.#.....O##.....#.OOO...O.#... +#.O#..O..#..O..#.#..#..#O#......O#.##O.........#..O.....#..O..#....##......OO#..O....##.O.....O#O.#. +.O.......O.......O..O.#.#O....O.O..#.....O.#O#O.#.......##...O.#O..........O.###...#.##...O........O +.......O.....O..#...##.O.#.#O...O##..O....O..O.##....#.O..#.##O.....O.#...#..OO.......#.....#O.##O.. +......#O.........#..O#..#O....O.#..#.O...OO...O....###....O##O..O.O...O..#O.....O.#.......#...#..#.. +#..#..#.OO##....O.###.#.....#O#.O....O.#.O......O....#.O..OO....#...O#.O#.....O#O..#.O..OO...OOO.O.# +.#....O.......#O..##.#OO#....OO#....#..OO..#.....O.O.OO.O#OO##...O#..#..#..OO..OO.#..O..........O..# +.O...O#..OO.O..O.#.O..##.O...O.O.....#O.....O.O#..OO...#.O....#O..#...##..#.#...O.O..OO#O...O..#...O +#O.##...O...OO.##.O.....O.......##..#.###...#.O..#.....#O.#....O......#.O#.#.O...O.....OOO.O.....O#O +...O.#....#....#..OO...#...##..O.O.O......O.#..O..OO....O...#..OO......O.#.O.OOOO...O.#OOOOO.OO#.#.. +.......O#.O#.O..O.#.O..#.OO.O..#.#.##.....O.#.O##...##..O...O...O.O.......#O.#......#......O.......# +...#..#O.......#O...O..OO....OOOO......OO...#....O............#.........#O...#.#.......OO.....O..#O. +O...OO###.....O.OO......#O.O...O..#..##..O..O...O#.#......O#O.O.OO.#O..#O.O.#.O##O.O.O..#..O........ +.O.O.O..#.O...#.##O..#OO###.O..O..#...O..#O........O#O.#.##..O.O....O...#...O.O#...OO.....OO.......# +O.O..O...#O#...#.#....#......OO..O#...O..OOO..O..O.....#...#...O.......#.....#O..O.....O#..O.O...O.# +...##..OOO..OO#.O#OOO.#......O...O.......##........OO..O.#....O....##..##O#...#.#..O.#O........#.O.. +.......##O#........O......O......#.............O..O..#.O....#.#OO##...#O...O.O.O#.#......##..O.##O#. +OO.OO##.O...O#..O..O#...#.###....##.O...O.OO.#..#.O.#...#...##...#.....O..O..O.O.#..#..O....#....##. +O.#...##...#.#OO..##...#......O.OO.#O#O......OO...O#OOO...#...O.............OO.O.O.#..#O......#.O.#. +.O.O.....O....#.......O......#O.O..##.O#...O....#O....O..#.OO..O#.O.#.....O.O##...........O#..#O...# +....#OO....#..###..O.O#O......##.#.#.O#O.#.##........##.O...#.OO.......O......O.....#.#O.#.O..#...O# +.....O..#..#...#.###.##....#O.O.O..O....OOO.##...O##..O.#...##..#..O......##....#.O....#O........#.# +...#OO#......O...O#.#..........OOO...#.##O.O..##..#.#.O......O#.....OO......O.#O..###..O...OOO..O..O +.#.O.#.O.O.O.O.#..OO.O...OO...O..O........OO.........#.....##.....O#..#...#.#.#.O...O.......O#...OO. +.....OO.O....O....O.#..##OO...#O#.......O....#....O.O..........O##.#...O..O.#..#..O....OO...#....... +....##...O.#.....##O...##.....O..O...#.##.O##...O....O..O...##.....O....#.OO...O..OO#.O....OO......O +.O....#.....O.#O.#OO.O.##..O.....O..O...#.##.O##...O...O#OO#.O.OO...O.O.#O#....#.O#.O...O....O...... +..O#O#.#.#.#....##.#O.O.O....#.##O..#O..O..O....O.....O.O..#...OO#O...#OO...#O..O...OO#O#...#..#.... +.#O......O.O.O#..OO.OOO.#......O...#.O......OOO#....O.#..O.......O.......#.....#....#O.....O#O....O. +.....O..O..O#........#............O....O#OO.O..O.#O..O.OO...OO....O.#.O..#.OO.......O.O.OO..OO.##O.. +..O#O......#.............#..#.O.O.........O.#OO.OO..##.O#O........#.....O..OO...O.##.##.O.O#....OO.O +O..#..#....#.....#O.O..O.O..O.#.O.O.#OO..O##O..#.#.O...O....OOO.O.#...........#...#...#O...#O.#.O... +OO.....O#..#.#...#...#...##..O...O.....#O..O#O..OO.....##.#.#.OO.....O##O.....#O.O.#.#...OO...O#.#.. +....#...O#..O.OO#........O..#..O....#.O.....OO#..#....O..OO.........O..#..#...O...OO.......O.......# +##.O#.........O#.OO..O..#.OO.#O.....O#O...O......#O#.O..O.....#.#O.#O.O#......OO.O........O.....#... +.#.O.OO....O#O..#..O.#.#O....OO...O#..#.O.##..#..O.#O#O..O.......O..#.O#.#.##...O.OO.O#O.#..#.#..O.. +...O#.......O#......O#........O..O..#..O#.O...O#..#...O......#...#.#O....O..#.#....OOO.O.......O.... +..O....#.....O.##..O..##O....#O..O.###.....O#..OOO..##........#O.O..O.....#......#O#.........#OO..#. +#.##...#.#...#.....#O.O.#..##.O..O.O.....#.#OO.........O.#...O....#......##.#.OOOO.O.OO...O.OO.O.... +..#....#...OO..#O.....#..#O..##....#......#O.#..O.#OO.......OO.O....#.......OO..#.#..OO...O.....#OO. +O.....O.#.....#..#.O#......#O#...#....O......O#O.O#.O....#.OO##........O...O#..O#.OOOO.#O#O..##....# +.O........OO.#.....O#O..OO.O#O#..O.......#.O..##.#.#.....#.OOOO...O..OOO.OO#....O....#....O.#....OOO +.O#O.O.......O#O...O.#O...O.OO#O..O.#..#O...O.O.#.#OO....##..##..#......#..O#......#O.O.O#O#.#...#.. +..O..O#.#..O#.O..OOOO...O..OOO#O......##.O..O..O..OO#O.#....O.......#..O#.O..O..#..O...OO.....##O#.. +...OOO..O........OOO....#..#.O.O..##.O.O..#....O........#...##O#O..O.#OO.#...#..#O#......O.O....O... +.#O#.#.#.O.#..#.O..O.....O.O..O.#.O#........#....##.O#O..#..#O#.#..O#O#.#.#..#......O...O#.O.....#.. +.#..O.#OO.#..#O...O...O..#O...#...O#..O.............#.....O.#O....#..#...#.#...OO.O.OOO.O....##O#O.# +OO..O...O....##...#.O...#.#....O.#....O..O##..O.......#O.#...O..##...OO.O#O.....OO...#....#.......O. +.#O.O....#.#O........#.#..O....O.......#..O#.....O..O.O..##..O.O.O...O..O...OO......O.OO...#.#....OO +.#.O..#O.....OO..O..#.O#.O.OO.#....#O....#.OO.....O.OO..#.O.O..OO.....O..##..O..#.#..O......O..OO..O +.O.......OO#.#.O.OO..#OO.OO..#O..O##O..#......#.#O.O.#O..#.O.#..#O.OOO..OO...#.#.#.#.OO.O...#O##O... +O#...O.O.O........O#..OO.#.#.....#..O##....#.#...OO##..#..O..#..O.OO.O.O.......O.O#........OO.##O... +.#.O.#.#...#.O..O##....#.O.#.O#O........#..#.#....O.....#..O#.##...#....#.##.#O.O#O.O..OO##.#..O#O.. +O...##..#..#OO#.OO.#...##..O...#.O.O#.#....#O.O##....O........OO#O..O..#.......O.#OOOO........#O...# +O...#...#.O...#....O#O#......O#.OO#..#....#.#O...O#...OO##...O#.OO....####.......#.......O.........# +#O..OO.OO....#.....#....#O.#..OO.#....##.....#...OO.O#O......O.O.O........##..O..O....#..O..O#.O.... +#O#.OOO.#O#.#.....#O.O....O.#.....OO...##....##O.#....O.O.#O.##O..#O#.O....O...O.#O.#.OO..#...OO..O. +....O.........O.O.....#.#...O.O....O..#.#...#..#..#..OO..OO.#..O..O....O........O#.O.O.O#.....#O.O.# +#...#O...#OOO...#..O.#..O..O...#.....OO#...##....#.##.##O.OOOO.#O.#...OO##.OO....#...OOO..###...O.## +..........#...#.#.....#O...O#OO....OO#...#..##..O..O...#.#.#..#.......#...OO....#O.......O..O....O.O +..O......#..OO..O#O.......#O...OO......#..O..OO..#.#O#OO..#.......OO#OO.O...#..O...O..##..OOOO.#...O +..OO.O#.#....O#O#O#..O....##..O.O#.O.O..O...O....O...#..O...#O.O..O#.#O..........O.........O..O.O.#. +...#....O#.#.#..OO....O.#.......#O#....OOOO.#O#O.O##.OO.#.#O##.....O.#OO#....O...#..OO#.O..........O +.#......#O#.#.....O...OO..O.#.......OO..O.O...O.O.#O....#....O..OO.O#.....##.#....O.O....#.#O.O#.### +...##O.....#O......O.......O.O.....#.O...O.O....OO..#O...O......#......O...#....O...#.O.....O#.#..OO +O..#.....#O..........#..OO........OOO.O..O#O#...#OO....O.#...OO..O..#....O.O.#.O#........#O.#.#...#O +O...#..OOOO...#.....#........##..OOO#OO#..#..#.#.O.#.O.O......O.....#.#O#..##....O.#O#O.O.#...O..... +.O.#.O....O.OOO..##.....O#.OO........................O..O..#.OOO......#OOO......#...OO........#..OOO +.###...O##..#.O#..O#..#.O#.#.O..O.##..O...#.O..O.#.#.....OO..O.#O.O....OO.O.O..#....#O.OO#.O..O..... +.......O.O.....O.....O#.....O.....OO#.O..#...O..O..##.O..OO.O....#O......###..#...O........#....O.OO +O..O..O...O.O.O.O.O.O...OO........OO..OO..O.#O.#.O....#..OOO.O.#...OO.#...O......OO.OO#.#...O#...##. +O#O.##...O#.#..O.#.O..#..#....#..O##...##....OO.#..##OO....##......O....#OO.O...#OO..OO#...O.OO.#.#. +..O.#OOO....O....#....#..O#O..O..O...O...O.........#..#.#O..#.#...OO.....##.O.......O#..O#.#...OO... +.O.O.O.O..O.##...#..........O.O..............#....#.#O...OO.#..OO...#....O.O..#.......O.#..#..##.O## +..OO.#.....O..O.OO.#.##.O....#...#O....##..O#.O.....O.....#....OO#....##..#O..OO...#O#O#....#.O.O.O# +..O.#O....O.#..#.#.OO.##..OO......#...#....#O...OO....#.O..#.OO#.......O.#.#......O#O...#..OO.....## +#.........O....#..#O.....##....#O......##..O....OO.....O.....#..#O...O...O..#.OOO.O..#......#.....#. +O.##.O.#OO...#..#O.OO.....#.#.....O..##.O.#......#O##......OO..O..O.O.....#..O.O.#..O......O.OO.##.. +....O....O#.##..O.O.O..#..##O#......O#.....O......O.#....O......#O.O..#..O..O..##..#O##..........O.. +O#..##O..#...OO...#.OO....#..O.....O.......OOO#...##...OO.##....#O..#..#O.#.O#O..O...O.#..O.#O.O#... diff --git a/2023/14/src/main.rs b/2023/14/src/main.rs new file mode 100644 index 0000000..e75b1e5 --- /dev/null +++ b/2023/14/src/main.rs @@ -0,0 +1,240 @@ +use ndarray::*; + + +use std::fmt::Display; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +#[derive(Debug)] +struct Platform { + matrix: Array2, + width: usize, + height: usize, +} + +impl From> for Platform { + fn from(lines: Lines) -> Self { + let rows: Vec> = lines.map(|line| line.unwrap().chars().collect()).collect(); + let width = rows[0].len(); + let height = rows.len(); + Self { + matrix: Array2::from_shape_vec( + (height, width), + rows.iter().flat_map(|row| row.iter().map(|c| *c)).collect(), + ) + .unwrap(), + width, + height, + } + } +} + +#[derive(Debug, PartialEq)] +enum Axis { + Rows, + Columns, +} + +struct North; +struct South; +struct East; +struct West; + +trait Direction { + const REVERSED: bool; + const AXIS: Axis; + const ROTATIONS: usize; +} + +impl Direction for North { + const REVERSED: bool = false; + const AXIS: Axis = Axis::Columns; + const ROTATIONS: usize = 0; +} +impl Direction for South { + const REVERSED: bool = true; + const AXIS: Axis = Axis::Columns; + const ROTATIONS: usize = 2; // 180 +} +impl Direction for East { + const REVERSED: bool = true; + const AXIS: Axis = Axis::Rows; + const ROTATIONS: usize = 1; // CCW +} +impl Direction for West { + const REVERSED: bool = false; + const AXIS: Axis = Axis::Rows; + const ROTATIONS: usize = 3; // CW +} + +impl<'a> Platform { + fn roll(&mut self) { + // Get a view into the rotated matrix with the target direction to the north + let mut view = self.matrix.view_mut(); + + match T::ROTATIONS % 4 { + 0 => {} + 1 => { + view.invert_axis(Axis(1)); + view.swap_axes(0, 1); + } + 2 => { + view.invert_axis(Axis(0)); + view.invert_axis(Axis(1)); + } + 3 => { + view.swap_axes(0, 1); + view.invert_axis(Axis(1)); + } + _ => unreachable!(), + } + Self::roll_rocks(view); + } + fn roll_rocks(mut view: ArrayBase, Dim<[usize; 2]>>) { + let axis_len = view.len_of(Axis(1)); + for mut col in view.columns_mut() { + for inner_idx in 1..axis_len { + if col[inner_idx] == 'O' { + let lower_limit = (0..inner_idx).rev().find(|i| col[*i] == '#').unwrap_or(0); + if let Some(empty_pos) = (lower_limit..inner_idx).find(|i| col[*i] == '.') { + col.swap(inner_idx, empty_pos); + } + } + } + } + } + fn score(&self) -> u64 { + match T::AXIS { + Axis::Columns => self.score_columns::(), + Axis::Rows => self.score_rows::(), + } + } + fn row_or_column_score(&self, idx: usize) -> usize { + if T::REVERSED { + idx + 1 + } else { + match T::AXIS { + Axis::Rows => self.width - idx, + Axis::Columns => self.height - idx, + } + } + } + fn score_columns(&self) -> u64 { + self.matrix + .rows() + .into_iter() + .enumerate() + .map(|(idx, row)| { + row.iter().filter(|c| **c == 'O').count() * self.row_or_column_score::(idx) + }) + .sum::() as u64 + } + fn roll_cycle(&mut self) { + self.roll::(); + self.roll::(); + self.roll::(); + self.roll::(); + } + + // find the first loop, return the iteration count when we first saw it and when we saw it again + fn find_loop(&mut self) -> (usize, usize) { + let mut first_seen = Vec::new(); + first_seen.push((self.matrix.clone(), 0)); + let mut i = 0; + loop { + self.roll_cycle(); + i += 1; + if let Some((_, first_idx)) = first_seen.iter().find(|(val, _)| *val == self.matrix) { + return (*first_idx, i); + } else { + first_seen.push((self.matrix.clone(), i)); + } + } + } + fn score_rows(&self) -> u64 { + unimplemented!() + } +} + +impl Display for Platform { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.matrix.fmt(f) + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut p = Platform::from(input); + p.roll::(); + p.score::() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + const ITERATIONS: usize = 1000000000; + let mut p = Platform::from(input); + let first_loop = p.find_loop(); + let loop_length = first_loop.1 - first_loop.0; + let cycles_to_skip = ((ITERATIONS - first_loop.0) / loop_length) * loop_length; + let iterations_remaining = ITERATIONS - first_loop.0 - cycles_to_skip; + + for _ in 0..iterations_remaining { + p.roll_cycle(); + } + + p.score::() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"O....#.... +O.OO#....# +.....##... +OO.#O....O +.O.....O#. +O.#..O.#.# +..O..#O..O +.......O.. +#....###.. +#OO..#...."; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 136); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 64); + } +} diff --git a/2023/15/Cargo.lock b/2023/15/Cargo.lock new file mode 100644 index 0000000..2473428 --- /dev/null +++ b/2023/15/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day15" +version = "0.1.0" diff --git a/2023/15/Cargo.toml b/2023/15/Cargo.toml new file mode 100644 index 0000000..f52c8dc --- /dev/null +++ b/2023/15/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/2023/15/input b/2023/15/input new file mode 100644 index 0000000..1c4be4c --- /dev/null +++ b/2023/15/input @@ -0,0 +1 @@ +dcf=3,hp=9,kjfhxf-,hpmvrg-,pnb=7,zxz-,vrmt=4,ghrx=7,fz-,vdx-,pbft-,sxlz=7,pdh-,bdm=3,tp=1,bpf=6,ftx-,vc-,bc-,jcm=9,kfgm-,jlfmd=4,bgc-,fs=4,bsqpf-,hsk=5,cr=9,dn=1,xs=8,qdht=1,gtd=6,ccd=4,ngk=1,fp=8,vhf-,hvd-,kxssfz=5,ks=2,llnh=9,rv=8,mj=8,bf-,jrdv=5,jf=9,lpkjx=6,bnf=8,fz-,zxz-,nxsx-,rz-,mkt=1,db=2,lmvv=5,hgnj-,xlh-,mz-,ghcf-,fm=3,lsz-,mqgk-,ldxvr=9,kqkxm=8,gkc=3,rkl-,jxv-,fmgd-,dmbgc=2,sdp-,xv-,zsf=6,gbfp=6,tjbpt-,cs-,ks=1,nct=4,qlv-,jrdkpm=1,nm=3,pncdx=6,xg-,zq=9,kmxnp-,lfqd=5,vp=1,rdl-,fz=3,mbt-,sdp-,vhx-,jcm=5,skz=1,fzq-,fvzbl-,kbtbvk=6,tpv-,gbfp=9,gbfp=3,xnz-,djl-,nb-,nzh=1,zgcr=5,fmgxs=8,tk-,skh-,fvqx-,jsbjs-,cc-,fvf-,mqgk-,mjrkp-,qz=3,db=1,gm-,vckn-,bf=2,cp-,qnjf-,fm-,kzt-,kdt=5,nm-,bnf=8,xnt-,rtf=5,qsvbm-,ngj=5,vp-,mxnm-,kgg-,btd=1,ghcf-,mrm-,nct-,hpj=8,ntrxnc-,dbh-,jld-,nclb-,mkt-,vckn=1,hpmvrg-,rsn=3,jzr=8,gf=9,sff-,jlfmd=1,fc=8,bnl-,jrdv=1,cr-,bj-,zl-,vv-,qkdghh=3,rdl=2,fthhk-,btbbbg=2,qsvbm=3,jfjn-,qsvbm-,vc-,cn=4,zdjz=5,tjbpt-,xj=1,qhzp-,gnknqg-,rvcl-,kz-,xzbk=9,vqs-,zlss=3,rld=5,rtf=7,qp=5,gtp=4,chsk-,mr=4,tp=2,dcb-,zcm-,jnr=7,pld-,czlvx=9,mlgj=6,bm-,grn=5,gct=9,xd-,ks-,mlgj-,jxv=1,gbfp=9,vckn-,cqn=6,jjfth-,hksq-,lpkjx-,rl-,cl-,br=8,jpm-,pg=1,czlvx=3,qdssh-,cx=5,sdp-,hr-,pg-,brn-,xr=7,qd-,hpgq-,nbvjjn-,qkdghh=4,fvzbl=1,sjd=7,qm-,nz-,jcm-,dk=5,bgj-,cnspxc=9,ls-,zhbz-,rsnmqs-,bhv=6,sp-,csr=8,fvqx=3,lmvv=2,bzqbb-,qzf-,tx-,gpl-,xlh-,pg=2,xcc-,rl=2,kbtbvk-,xg-,sp-,sz-,mz=9,bp-,rtf-,bsvql=6,pncdx-,lr=7,gpd=6,srtdk=9,kgtg-,mhz=6,dh-,gkc-,tvhg-,gxx-,tt=3,dj-,rhj-,zcm=4,nq-,xr=9,rtxm=5,br=4,hg-,rv-,ppt=5,df-,jn-,ks=7,qgg=1,zgsvvh-,hpmvrg-,hsk=5,cx=4,dlc-,jpm=4,jm=2,tt=6,mz=7,vf=3,jpm=2,jlkf-,lr-,sjd-,jxjns-,fpj=6,qlv-,jnr=6,dd-,rp-,xf-,npvb=2,qxm=8,br-,sp-,fm-,mf-,rsn=1,pclz-,qxm-,mj-,chsk=5,zq-,lsz-,jfnt-,bpf-,jgkvdq=4,pz=4,rv-,bsvql-,rhj=8,ppm=7,fz=5,fkg=3,cr=3,mvbgm-,xlh=9,frszb=5,sff-,qv=2,mhz-,hv-,xlh-,grn=2,hnqsn-,rsn-,hds-,sjkxq-,pld=7,qlt-,grn-,rc-,tft=7,nztgd=6,fhxq=4,jbn=1,fc=1,fmgd=7,mbnpqk-,mlg=4,ls=8,zmj=1,txvfx=8,gxp-,mjbcps=4,rz=4,qv-,fp=8,hxr-,lhd=1,rhj-,lhd=5,hm=8,njg-,btbbbg=5,sdp-,lnkkl-,jrk=3,sjvs-,btd=8,hgx-,gjtq=1,tk=1,vj=7,tnf-,ck=5,dprj-,lr=9,ngvmt=5,jk=2,sff=9,frszb-,dkhv=4,xq-,dlc=6,hp=6,psh=4,nlmjb-,hfr=2,zcz=5,xcbsp=6,jlhx=5,txc-,tn=6,lpkjx-,bj=4,nhzcjt-,bzqbb=2,brh=1,drdn=7,vhx-,lfz=8,dkhv=1,gxx=5,zlc=5,qz=7,kzt=5,dsxdt=2,gbfp-,fmg-,gjtq=2,jn-,lmbhsl=5,qm=8,ccd-,ldxvr-,mbnpqk-,vg-,dn-,bj-,bp=8,qhzp-,mqgk-,btvq=8,fzq-,htf-,nnfmq-,ck-,bzqbb-,bshcdm=2,dsxdt=6,lhc-,rvcl-,jgvk-,cc-,tvc=2,fs-,qcn=9,qff=6,dmz=5,kqmx-,qgg=3,mr=3,jgknh=2,gknp-,rpq=1,hksq=6,tsg-,bf=8,dcb-,qdht=5,fc-,mfjf=9,qdssh=3,htf-,qgp=8,rf-,kk=3,sn-,bshcdm=7,nclb-,hgnj-,xtd=6,jk-,jtkj=9,nhzcjt=6,xt-,hsk=5,bf-,zl-,cc=1,phxt=8,bj=4,lhd=9,zgcr=6,qf-,bj=8,bhb-,ks-,cx-,czlvx=9,lvh=7,jrdkpm=7,rv-,rkl-,jrdkpm=7,gf-,bgj=4,qdssh-,hd=3,kc=8,jzr=7,cl=7,gtd=8,fc=2,rpq=6,jkh-,zxz-,fksj=4,sxlz-,xj-,vckn-,dj=5,zb-,hds-,bgn-,ngk=6,hfr-,vrmt-,knv=6,pncdx=9,kbtbvk=1,lj=1,zkj=7,jpv-,rv-,smk=6,gndr=8,thddd=9,kp-,rsnmqs-,mj=6,fthhk-,bjgh=3,xj-,vnqslq=9,npvb-,pvn=7,kdj=8,lclv-,pclz=1,skh=5,xd=2,rld=1,bj=9,lqbq=9,vzm=5,srtdk-,kc=2,xdhk=2,kdj=3,lmvv=1,plc-,gknp=9,xbddc=1,jrdkpm=9,vm=6,xv=6,hksq=7,djl=2,hg=9,hk-,khcq-,zlc=7,tvc=4,qf-,qnjf-,nq=7,vm-,frszb=8,jqx=6,jtm=7,bsqpf=2,rdl=6,gtd-,sxlz=7,ls=5,hr-,th-,vbt-,bpf-,xv-,pd-,kgtg-,xtd=7,kkt=5,skz=4,vhx-,dcb-,rzk=4,dcf-,pbft=8,zd-,mqgk-,rvcl-,rv-,nclb=9,mjbcps=1,pclz=4,psh=4,jgkvdq=3,bhps=8,bjgh-,jtm=1,csr-,mlg=7,vdkm=2,jcm=4,smk=6,bdm-,gknp=7,rg-,gxqs=8,rl=1,hsk=9,kt-,jbn-,zcm=8,rhr-,fmgxs-,bhv-,hzh-,vt=9,txvfx-,mf-,rg=8,fmg-,cl=2,hd-,fz-,lv-,xgsr-,fp-,hp=1,rg=1,gtd-,gknp-,hpmvrg=9,tzdg=6,rpq=4,htf-,tq=3,dcf-,dk-,hlrzf=3,vdx-,hpgq=5,kc=8,fhxq=7,cr-,gpd-,snf=7,tqt-,vbt-,hlrzf-,hzrv-,ck=1,jf-,zcz-,fh-,zcm-,rvcl=1,gbv=2,cfx=5,ndhh=8,xks=6,tx-,zm-,dk=7,dbtq-,qlt=9,kcgg=6,mbnpqk=1,qhzp-,vs-,tqt=2,lnkkl-,cx-,jsbjs-,nbvjjn=7,dk=2,jf-,rg=3,rhj-,jdvx-,bpg=7,mbt-,phxt=7,lqbq=2,kkt=9,ldxvr=2,pd-,jgvk=3,hzh=4,dmbgc=4,jrk=5,nm=8,rzk=1,zq-,jsbjs=4,jlfmd-,gpb-,lsz=1,kpz-,rtf-,mjrkp=7,zg=7,xm-,sjvs=3,lnkkl=4,gxqs-,xcc-,tq=9,dndnrr=7,qsvbm-,qsvbm=8,zg=4,ppt-,lv-,gnknqg-,br=9,rtf=2,kjfhxf-,fc=2,jrk=7,bzqbb=4,xpt-,qff-,rp-,bnf=2,dlh-,zcm-,zmj=1,bshcdm-,fmg=6,nkbb=2,bn=7,zdjz-,snf-,ldxvr=2,tzdg=1,lhc=1,xv-,pvn-,dh-,mqrr=8,hpmvrg-,bh=5,jk=4,mvbgm-,zdn-,xnt-,rtf-,tzdg-,vzm=6,lhd-,nz-,mf=1,bgn=7,txvfx=2,nm=6,nq-,pvn=6,jxjns-,zmj=3,kpz=4,kpz-,xcbsp-,nrfpf-,lv-,dj-,jn-,btbbbg=4,cf-,nm=1,mqrr-,nb-,gbp-,bj=6,xdv-,bh-,jzr=7,gkc=6,zhbz-,hx=6,lr-,bz-,bfhqb=6,kfgm-,xf-,tzdg-,xf=5,csr-,xzbk=6,hnqsn-,djl-,jf=1,mbnpqk=1,jt-,zmj=3,kkt-,cs-,ngj=1,lgc-,jf-,lhd=6,llr-,nct-,kxssfz=1,xr=9,gnknqg-,zg=9,lfz-,jk-,rxc-,sjd=8,bzq-,tpv=9,pvs-,vch-,fmgxs-,mlgj=7,qr=8,rl-,zgcr-,nrfpf=9,nhzcjt=5,dcb-,mr=1,kgg-,srtdk-,xlh=5,btd=8,vp=6,xv=2,vnqslq-,xcg=8,rz-,mv-,sjd=9,czlvx-,hx-,gknp=8,zcz=7,jdvx-,pclz-,hsk-,sl-,vg-,lv-,gpb=4,zcm=4,zlj=6,db-,dd-,nlmjb-,fc=1,jtkj-,qgp=9,vm=2,ntrxnc-,hds-,qdht-,rldfx=4,lvh=1,dlc=8,sn=6,gjtq-,cf=1,fvzbl-,jkh=9,snh-,jms-,xpv=6,mvbgm-,lt-,jbn-,rx-,lvh=1,lsz=4,zb=2,ftx=8,fgbjn-,qm-,btbbbg=1,xm=6,hqzl=2,xj-,hvd-,sxlz-,rhr-,jnr-,snh-,cdj=1,vdx=5,hzrv-,pd-,hqtr=9,sql=7,ccd-,jlkf=3,sz=2,gvzt-,smk-,ndhh=6,fk=2,rsn-,jgknh=8,sjd=1,tjbpt-,jlhx-,hk-,tp=9,brh-,qcn-,bsvql-,xm-,rz-,ldxvr=4,hlrzf=4,pbft=4,bnf-,ngc-,hd=8,ptr-,pclz=8,bnl=9,xpq=4,nnfmq-,zkj=9,plc-,bpd=3,dhct=4,cf=3,btd=7,tvmgv=9,xks=7,qkdghh=2,ppt-,kk-,ks-,hgnj=6,drdn-,kxssfz=6,bnf-,tj=6,qcn-,dkhv=5,qr-,rd-,jpm-,tq=6,hzrv-,mbt=2,hzh-,dmbgc-,jld-,vgdb-,sg=4,jh-,hlrzf-,ppm=7,gndr-,npvb=4,mkt-,lfz-,vt-,vs=8,xpq=3,hv-,sjm-,vckn=9,pncdx-,jrk-,frqh=6,vgdb-,pncdx=1,xlh=9,xf=6,fmgd-,lhc-,vc-,js=1,qf=7,md=6,bnl-,kjfhxf-,mdp=4,cq=2,nfq-,zkb=9,glqxjt-,rzp-,jxv=4,xbddc-,bnf-,vhx=8,czlvx=4,xcg-,hxr=6,gvzt=3,pn=4,rf-,xks=1,ch-,vj-,gcc=8,dcb=8,fcpm=7,jl=4,jjfth-,rp=2,cdj=8,zq-,rrcx=8,bhv-,gpd=7,jbng-,vgdb=9,pq=1,hgnj=7,xg=9,hzg=7,lr-,hgt-,jnr=9,ht-,jdvx=7,xm-,jlkf-,nlmjb-,vt-,fmgd=9,kmxnp-,hnqsn-,fs=5,psh=8,gknp=5,qd-,srtdk=8,btd-,mdp=1,rldfx-,mfjf-,bpzlm=7,jlfmd-,db=1,nhzcjt=4,mbnpqk-,sg-,nd-,tjbpt-,jdvx-,rz-,ccd=3,rtxm-,tk-,gbp-,pn=4,sj-,cqn=7,rg-,xs-,dmz=6,xg-,xdhk=9,cq=8,hsk-,rv-,fk=5,rtxm=5,gvzt=8,fq-,ldxvr=1,lj-,htf=1,hfr=1,qr-,vt-,ntrxnc=6,mz-,glqxjt=8,snf=7,dtpzc=8,ftx-,vds-,dhct=1,jrdkpm=3,lt-,bvm=8,xjs-,vf=4,fp=1,bpgjt-,jpv-,ngj=7,lfqd=7,dbtq=1,rz-,gbv=7,kpz=8,fvzbl=6,njg=4,mlg=8,dmf-,rtf-,xnt-,grn=5,cn-,rvcl-,rsn-,vhf=4,jvbr-,jxv-,jsbjs=1,qdssh-,vdkm=4,fcb-,qf=1,kxh-,qkdghh-,xnt=4,krsn=7,jsbjs=6,xd=7,kb-,gxqs-,jgkvdq-,nbvjjn=3,djgv=5,mz-,nxsx=6,rpq-,kt=7,gbv-,ngj=9,hpgq-,df-,tvc=5,hzg-,fk-,xlh=7,njg=4,khcq=4,ngc=2,lpkjx=4,mv-,lfz=6,hdlp-,npvb-,bvm-,kcgg-,lmbhsl=2,zkb=8,rf=3,nrfpf=6,sql=3,xdv-,zt=3,qkdghh=1,pg=4,lmrl=5,bj-,bpzlm-,nkbb-,xdv=5,kc=6,cdj=4,rzp-,jk-,gm=3,qzzdfc-,txvfx-,bp-,hkdl-,kx=8,bhb-,hp-,tt=1,cb-,mlg-,fpj-,sp=1,kxh=2,gkqs=9,xnz=8,qnjf=1,bnl=4,jfnt=4,xcc=4,zlc-,knv-,ls-,jh=8,mhz=3,dprj-,kb=9,lmbhsl-,rzp=4,dd=2,nxsx=7,lqbq=5,vch=5,htf=5,sjvs=3,pnb-,vp-,djl=8,lmvv=8,gtd-,gvzt-,dlmm=5,txc-,mbnpqk-,flb=3,zt=6,vzm=3,hp=7,kdt=1,cdj=4,glqxjt-,fthhk=5,qff-,lnkkl-,jgknh=7,dc=4,xpq=8,jm-,xbddc=4,ccd-,gxp=7,pvn=7,jgvk-,bnl-,vdx=6,nct-,skh=8,hfr=9,bdm-,qn-,qdssh-,jjfth=9,jdvx-,jfnt=2,kzt-,dq=1,hx=6,lpkjx-,dn-,rc-,vbt-,vm-,ppt-,kxh=5,xnt-,mbt=4,xd=5,xcc=7,ht=2,qp-,fs=2,sdp=7,js=7,kgtg=9,lk-,zd=5,sff-,hqtr=9,tft-,zgsvvh=8,plc=5,dnr=1,bsvql-,kg-,nclb-,rl=8,bgn=1,zkb=4,tqt=6,njg-,dbtq=1,hx-,pnb-,sn-,fmgxs=5,xlh=7,tvz=3,hk=6,jlfmd=8,ngdqv-,ggdh-,zlc=6,ch=8,hqtr-,cqn-,fvk-,ngj-,gd=5,sn=4,lpkjx=4,qxm-,dtpzc-,pn=5,mqgk=2,fmgd-,xpn-,kqkxm-,jt=2,txvfx-,snf=2,qxm-,jqx=7,kgtg-,xv=5,cdj=8,lpkjx=4,cx-,mdp=3,llnh-,vds-,hqzl-,lclv=6,hqzl-,kdhp-,djgv-,xcbsp-,dd-,jbn-,fvk-,vbt=2,jd-,hk-,kdhp=8,xg-,btvq-,gkc=4,gd-,jlfmd-,dlh=5,qgp-,mjbcps-,cs-,qgg-,kkt=3,vx-,lqbq=3,zsf-,rz=7,bpg=7,skh-,njg-,dh-,zq-,bpgjt-,fsx-,cr=1,cp=5,kzt-,sxlz-,xnt=7,jgknh=8,brh-,sjm-,gknp-,tsg-,rxc=5,cb-,gt-,zdn-,bz=4,jld=1,zhbz=5,jcm-,pvn=8,frszb=9,ghrx-,fx-,mz=3,bfhqb-,vc=2,dhct=3,gnknqg-,xg=5,zq=5,snf=5,dlmm-,rf=8,bshcdm=2,zgcr=3,jtqb=9,gbv-,zlj=8,kxh=3,zhbz=6,mjbcps=4,kgg-,plc-,qdssh-,rv=2,jfnt-,fmgd-,jzr=6,vm=8,gtp=3,fm-,bnl-,kb=7,brn=7,sdp-,vdx-,snf=3,dbtq=4,kzt=7,xbddc-,vj-,qsvbm=8,qnjf=8,qz-,jzr-,qm-,qp=7,qz-,mrm-,xjs=4,nzh-,qcn=1,sn-,nclb=9,kt=3,mxnm-,ldxvr=7,vx=6,rldfx-,qlv-,knv=6,bpg=6,pz=3,qp=5,fds=7,thddd-,gndr-,vbt=3,bhv=1,flb-,sp-,kpz-,qlt-,jgkvdq-,fmg=1,jh-,db=7,ht-,lv=8,nxk-,rvcl-,sjd-,jtqb-,jrdkpm-,gvzt=1,vds-,jlfmd-,qgp-,mxnm=5,vg=4,tzdg=1,vdkm=2,llnh-,jxv=1,mbnpqk=6,rvcl-,tvc=7,gt-,nbvjjn=2,lzqxj-,glqxjt-,dbh=6,zdjz-,gkc=6,hnqsn-,zdn-,fgbjn=5,kzt=4,cs-,fsx=3,vj-,xq=8,jvbr=7,vj=6,xbddc=9,gnknqg-,cq=8,vds-,hgx=4,zlj-,br=4,lfqd-,qxm=3,zbnl-,bshcdm=5,vckn=7,qz-,flb-,qf=3,tsg=8,qz-,xgsr-,jlhx-,vqs-,ngk-,nnfmq-,zcz-,vds-,ngdqv=9,qgp=1,rzk-,sp=4,xdv=1,nkbb=5,bh=4,xgn-,czlvx=9,hsk=7,jk-,vj-,rg-,ntrxnc=3,rtf=9,bpzlm=6,fmg-,xgsr-,lgc-,jlhx=2,tx=3,nxk=3,fmgxs-,mr-,vx-,nhzcjt=9,gxp=2,brn-,rz=2,bf-,nj-,ls=2,jrdv=8,kx-,rsn=6,hds-,tp-,nztgd=6,phn-,tnf-,smk-,fh=7,vj=8,ppbqks=9,ht=4,qgg-,fk-,jgkvdq-,kpz-,kklrf=7,rv=9,nxk=1,hr=1,dj-,jtm-,kgg=9,nfq=7,ls-,zlj-,tk-,phxt-,fksj-,nd-,zcm-,tx-,jbn-,kjfhxf-,kmxnp=2,zxz=2,vds-,fthhk-,dcb=1,flb=8,fcb-,gndr=6,jsbjs=7,jgkvdq-,xgh-,zlj-,jxv=1,nhzcjt-,jgvk-,mf=6,rtxm-,dmz=5,bzqbb=5,kg-,htf=5,fds-,xv-,frqh-,xjs=2,sjm-,jrdkpm=4,mxnm-,lfz-,btbbbg-,tt=8,hzg=4,kb=9,hpmvrg-,fcpm=7,fs=6,ggdh-,ldg-,gpb=3,nbvjjn-,db=6,frszb=3,cn=4,zlj-,kqkxm-,hlrzf=7,tjbpt-,gpb-,mqgk-,fzq=5,xgn=7,lzqxj=9,mqrr=9,xtd-,lclv-,bsvql-,ftx=1,xj=8,qsvbm=8,lsz=7,nb-,tzdg-,sn=9,fzq=4,qcn=4,kmc-,jt-,jjfth-,cr-,jlfmd-,ppt-,fthhk-,kb-,pz-,hkdl-,jd-,hxr=9,vg-,bpg-,tft=4,ht-,xjs=1,cc=5,sl-,ht-,xj=2,hp-,btvq=5,cfx=8,nnfmq-,qzzdfc=2,mhz=1,vhf=7,tsg=5,jqxdvz-,glqxjt-,kxssfz=9,hlrzf=7,hkdl-,zmj-,kklrf=1,cb-,fmgxs=3,pg=1,qsvbm=7,tt=3,gbv=2,dd-,vt-,xnt=4,xs-,qp=1,fthhk-,dkhv-,rzk=1,zlj=6,nct=3,ntrxnc-,dndnrr=3,fvzbl=6,dh-,brh=7,vbt-,thddd=8,kqmx-,ghcf-,rtf-,kx-,qgg=5,vv=9,fz-,zcz-,hdlp=4,flb=7,bpgjt-,zdjz=8,bpf=8,qnjf-,gbfp-,cb-,cq=5,gpd=7,lnkkl=2,bpd=8,ntrxnc-,xgsr-,cs-,lj-,smk-,rg-,dsxdt-,mjrkp-,jdvx=6,lnkkl=4,tvc-,mrm=5,zhbz-,vrmt=2,cfx-,jn=2,cq=8,tfp-,bjgh=1,mqgk=5,hgt-,tmxl-,vhf=8,zgcr=8,mxnm=3,bdxx=9,xnz-,dkhv-,vt=4,qz=6,zd-,xgn-,fp-,pbft-,nkbb=8,tfp=3,lmbhsl=8,nd=6,xcg=6,bm-,hnqsn-,fthhk-,hpmvrg-,qgp=5,phxt=4,fcb=2,hm=7,jgknh=8,jrk-,fh-,bjgh-,ngj-,nz=2,vs=1,xcbsp-,dmbgc=1,fthhk=7,xjt-,gbfp=7,hv=8,ks-,fkg-,kzt=5,zkj-,xtd-,mxnm=4,kgtg=2,fvk-,vds=8,glqxjt-,tnf-,hds=9,jrk-,ngdqv=8,pslp=3,nj-,jvbr=1,gtp=4,hmnr=6,ghrx=7,bhps-,jld-,vp=1,gnknqg-,kk-,kfgm-,jlfmd=2,rsnmqs-,kdj-,sg-,xd=6,fc=2,rldfx-,kfgm=1,csr-,ln-,cc=3,zq-,kmxnp=3,nj-,xlh=6,fcpm-,jms-,nlmjb=8,nclb-,vckn-,hr-,gct-,krsn-,vds=6,zg-,ngc-,hv-,jl=1,llnh=3,czlvx-,tvhg=2,rtf-,cnspxc=4,ngvmt=2,tfp=6,hx=7,kxssfz=2,pg=5,kp=2,lk=4,mxnm-,qnjf-,bsqpf-,fc-,xpq-,vf=3,njg-,bzqbb-,mfjf=8,tq-,jlhx=9,fl=1,vhx=7,jpm=1,vc-,gtp-,lr=8,qdht-,lhd-,pclz-,drdn-,vqs=2,jrk-,xm=8,fc-,rd-,zsf-,cn-,bfhqb-,ngc=9,jrdkpm=5,cr-,gtg-,vc-,xpv=7,sjvs=8,fvf-,vds-,ppt=2,vs-,rg-,hksq=9,zgsvvh=6,bv-,vf=6,vf-,pq=1,mz-,skz-,dn-,jk-,mlg=3,gknp=5,qzf=4,bdxx-,qv=8,pdh-,nnfmq-,fpj-,ldg=5,zkj=8,hds-,kb=9,gxx=3,bj=7,qkdghh-,xgsr=2,xr-,fcb=7,nnfmq=9,dlh=4,dsxdt-,lsz=1,pn=5,jpm-,kmc-,lsz-,lfz-,sjvs=1,tsg-,kkt-,xpv-,btd-,sj=1,dlc-,gxqs-,cc=4,mqgk-,vhf=1,lhc-,xnz-,nb=4,dj-,nxk-,nct-,fx-,qd=3,cqn-,drdn-,fksj=1,kfgm=8,jlfmd-,psh=3,fcb-,xg-,zt-,vhf-,cb=4,mjbcps-,jkh-,cb=4,lmvv-,fkg-,bpzlm-,jrdkpm=2,bshcdm-,zd-,qf=5,skh=6,gm-,qzf-,kg-,jkh=5,phn=9,fzq-,lv=1,kcgg-,pq=6,zhbz=6,gct-,krsn=8,qr=1,xks=5,frszb=3,mlgj=4,txc-,rsn=5,bpf-,tvmgv-,bpgjt-,cc=4,csr-,llnh-,ck-,xm-,ntrxnc-,xcc=1,vqs=9,hgx=9,nlmjb=3,fz=2,kgtg=9,kxssfz-,nbvjjn-,mvbgm=4,rz=4,kb-,ghcf=8,ln=6,kcgg=8,xt=6,fzq-,fgbjn=8,fvzbl-,lmrl=3,bn-,lmrl-,nlmjb=9,nxk-,mkt-,qd-,jxv=8,hgt-,kt=7,fc=3,ch=8,zt=3,lnv-,xjt-,knv-,sdp-,khcq-,gxx-,grn=2,zg-,vgdb=9,mvbgm=4,sjm=4,bpgjt-,gxx=1,ls-,ls=8,qn=7,kt-,rzp-,sj=9,bf-,fpj=4,bdm-,bpzlm=3,mbt=5,mlgj=5,lmrl=1,lgc-,fgbjn-,nkbb-,xpt-,rsn=2,jdvx-,zbnl-,lj-,sn-,xjt=3,dndnrr-,fmgd-,bdxx-,qr=3,jtkj=8,tt-,rkl=7,hqzl-,qd=5,glqxjt-,lfqd-,vv-,hqzl-,ntrxnc-,xf=8,fs=3,xnz-,mhz-,cq=3,rp-,tvmgv-,fs=4,tvmgv=6,dlc=2,hd-,frqh-,jtm-,nz=2,mkt-,bv-,vdx-,mr=8,pvs-,jdvx-,khcq-,jlfmd=4,gxqs-,jtkj=4,jjfth-,xks=9,bzqbb=2,ndhh-,sjm=4,btbbbg=9,psh=8,ppbqks=1,ldxvr-,thddd=1,bn=3,fksj=5,vv-,fm=3,xf-,btvq-,dk-,xnz-,ldg=6,bzqbb=5,zcm=2,xs-,tpv=3,mz=9,jgkvdq=9,vgdb=5,nztgd=6,sjkxq=7,lt=3,tvz=4,htf=8,fhxq-,ggdh=5,psh=4,cf=3,lclv-,bfhqb=8,rhr-,tvmgv-,xj-,dsxdt-,kdt-,fs=2,hp-,rkl=3,zgcr-,zq-,jzr=9,jxjns-,bpzlm-,vx-,mj=4,lclv-,hzrv-,tvc=1,jjfth=9,fs-,xq-,fgbjn=4,hk-,xg=5,nd=1,cb=7,tft-,sjd-,dlmm=8,bnl-,tsg-,jl-,khcq-,jh=4,lclv=7,gt=3,kfgm=9,sdp=1,xr=7,xdv-,dmz=3,pd=5,sl-,xj-,rl-,txc-,tsg-,rtxm=5,qkdghh=8,jpm-,zhbz=7,jgvk=7,gxx=8,pncdx-,plc=2,tp=8,zkj-,ghcf=8,nd=9,tmxl=6,btbbbg=9,kdj=5,qcn-,djgv-,jpm-,mrm-,pvn=7,djgv-,xs-,jjfth=6,kpz-,vqs-,hqzl-,bgc-,pnb-,jh-,fgbjn=2,kg-,ccd-,kx-,hvd=8,rl-,lv=3,thddd-,bgn=6,nbvjjn=6,pncdx=5,fmg-,zcz-,snh=8,dcf-,jgvk=2,ppt=5,bpg-,bsqpf-,js-,jpv=4,xcc=3,fthhk-,vzm=4,rg=3,rg=8,qnjf=3,xg=2,dd-,jrdkpm=1,rldfx=9,hzg=5,md-,bsqpf=8,xg=8,qxm-,cn-,htf=1,snf-,jt-,gpd=7,jl-,zkb-,knv-,hxr-,pn-,jbn-,hpgq=5,xnz-,ngvmt-,rldfx-,lclv-,qf-,vch=4,gvzt-,kp-,jtm-,kgtg=3,vds=9,sz=8,bn=8,rrcx-,jtkj-,bdm=3,fpj=5,rtf-,kk-,dmf-,tt=5,sfhd=7,bh=8,jld-,dj-,kdj=2,hds-,js=1,kp-,pg-,pg=8,zhbz=8,lmrl-,xj-,dndnrr=6,cr=6,qd-,bgn=5,tvz-,xg-,jtm-,rp-,gkqs=9,hgt-,csr-,pdh=2,rp=6,fh=4,qn=2,bp=5,zcm=1,hpgq-,qsvbm-,hd=1,kpz-,tfp=7,dj-,cr=7,fcpm-,fnkf-,btvq=9,jm=9,qz-,cfx-,gxqs-,sql-,bhv-,bgc-,zcm-,jqx-,ln=3,smk=7,fs-,lfz-,czlvx-,qcn=9,vj=1,sl-,vds-,hv=5,kzt=5,cb-,smk-,zhbz=9,rp=9,fkg=4,zlc-,tjbpt-,mvbgm-,rz=5,mqgk-,htf-,khcq-,vv=7,ht=5,zb=4,csr-,jrk-,nbvjjn-,hkdl=1,hfr-,pld-,skh-,rxc=1,gf=9,rzp-,jzr-,nd=5,dc=1,dlc=8,ht-,qd=4,xr=3,zxz=6,rp=2,fbd=3,sn=7,kx=1,bf=2,vx-,pz=5,jnr=9,jkh=9,vzm-,pdh=3,flb-,kz-,nzh-,hp-,hzrv-,cf=3,bpzlm-,xgn=7,fnkf=9,drdn=9,hv=2,hx-,gcc-,qsvbm-,kqkxm-,mdp-,sz=3,mbnpqk-,qz=7,lqbq=3,chsk=5,gkc-,db=7,gct=7,sff=4,hx=2,txvfx=1,fnkf=9,sz-,kmxnp=7,fk-,lhc-,kfgm-,jt=7,sff-,hqzl-,nhzcjt-,vx-,cqn-,dbtq-,pvn=6,fl-,fq-,rl=1,flb=3,gpl-,nm-,sql=3,cl-,psh-,jrdv=8,mxnm=8,br-,rtxm-,hm=7,cf=9,cq=2,xnt-,qr-,sxlz-,hk=1,vbt=2,rx=8,gd=2,qr-,hpgq=3,rqvjg=7,drdn=6,srtdk=1,zgcr-,bp=9,mbt=1,lj=9,gbfp-,sff-,ln=5,jrdv=9,sjkxq-,kgtg-,kxssfz=8,vc-,jlfmd=1,tt=7,jjfth=9,kx-,qd=6,vx=7,btd=2,xdhk=1,xbddc-,dmz-,jfjn=4,dbtq=9,kt-,rxc-,vgdb-,zd-,mkt=8,chsk-,dlmm-,fbd=1,ghrx-,rzp-,xcg=2,fds-,rrcx=5,tzdg-,jl=9,krsn=6,gknp=2,vj=8,dd-,zhbz-,zlss=5,vf=5,sjm=6,hqzl=9,rzp=7,hqtr-,bpzlm=9,gkqs-,js-,jbng-,nnfmq-,xs=2,ld-,rpq-,md=5,jn-,cb-,dhct-,dq=8,xgsr=5,szr-,nrfpf-,tq=7,fsx-,knv=2,mv=6,lvh=4,djl=8,zsf=2,vt=2,jlkf=9,ck=1,vbt-,hlrzf-,zb=8,zd=5,qcn=1,bc=7,mqrr-,vs-,tp=1,fpj-,lvh=9,bsqpf=5,kmxnp-,lfqd=7,rhj=3,qf-,dcb=4,bgn=5,mxnm-,bp-,ck-,dprj-,nz-,xlh=6,tvhg=4,xnt-,qdht-,mxnm=3,cs=3,fbd-,jxjns-,rx-,vf=2,fx=5,smk=3,bfhqb-,mhz-,kp=7,bv-,jgknh-,qzf-,nnfmq=1,gcc-,zsf=2,fcb=6,fz=1,txvfx=2,mrm-,bp-,bvm=6,rx-,qlv=3,xnz-,bpf-,bc=6,hpj=6,kz-,ck=7,dmf-,hzrv=2,xpt-,sg=1,xjt=9,vgdb-,zmj=5,tnf=6,dndnrr-,txvfx=4,ld-,pn-,sjm=9,xm-,frqh=2,drdn-,jrk=4,gd=4,djgv-,kmxnp-,gnknqg=9,sdp=5,fksj-,pn-,hds=9,hgnj=9,qkdghh=8,tvz=7,vg-,sj=5,krsn=7,tn-,cp=9,tzdg-,rsnmqs=5,gkc=5,zz=1,tjbpt=1,mjrkp=2,nq=4,vp=7,jrdv=8,hm=1,dk=7,kbtbvk-,kqmx-,rsnmqs-,zl=6,fnkf=3,ldxvr-,df=8,dj=8,ggdh-,bm=2,gtd-,ftx=1,bpzlm=8,hnqsn-,hzrv=8,bpgjt-,rl=2,qnjf-,xq=7,bfhqb=7,dsxdt=7,zlc=7,fx=1,dlmm-,hdlp=9,lt-,hpj=1,vm-,fp=4,ftx-,cb=5,rf-,ntrxnc-,rp-,cnspxc=7,vdkm-,zgsvvh-,zdn=5,rpq=6,drdn=1,zb=6,qn=7,zm-,qz=5,smk=2,gknp=2,pslp=4,rp-,pvs-,dj=1,rqvjg-,jcm=3,mqrr=5,kdj-,mqgk=6,sn=7,bsqpf=8,zdjz-,kx=2,zkj-,xgn=3,mhz-,cnspxc-,dn=2,mlg=6,kqmx=2,jgvk=9,rtxm=7,cfx=7,sz=4,bjgh=3,phn=3,gbp-,vnqslq-,bm=5,vv-,llr=2,tk=4,tmxl-,psh=2,zmj=4,zgcr=4,dlc=8,gtp=7,rzk-,lmrl-,cx=3,cqn=6,xcc-,qzzdfc-,bh=8,pbft=6,sff=3,nd-,jtm-,jdvx=1,jfnt-,jt=6,kc=4,bdm-,dk-,cnspxc=4,txvfx-,qzf-,zlj-,vch-,fvzbl=1,cf=8,zlc-,kfgm-,cfx-,bfhqb-,tfp-,cp=1,kxh=6,bn=4,qkdghh=8,gxx-,qnjf-,xnt=1,ht=7,jk-,xgh=1,gtp-,gndr-,gtd=6,nzh=6,vc=3,mj=7,hdlp-,qr=2,jcm=7,sl=6,hm-,lhc=4,hk=1,fnkf-,nnfmq=9,gjtq-,xcg=9,flb-,kdhp=8,xdv=2,czlvx=7,qxm=6,rsnmqs=6,sjd-,hgx=9,xs=6,hx-,rhr-,qz-,zz-,drdn=9,jbng=1,pbft-,zb=3,kmc=2,kk=2,fmgxs-,zt-,bzq=5,brn-,br-,tn-,dcf=6,tvhg-,vdkm-,vzm-,khcq-,mkt-,kpz=4,dj=2,pn=8,nct=1,sdp=8,vbt=1,xcbsp-,ggdh=3,fz-,phn-,vzm-,jjfth=3,jqxdvz-,ht-,snf=8,zg-,jgknh=2,qff-,sff-,vdx=4,lmrl=4,zz=2,skz-,bgc=8,pg=9,cn-,xlh-,jfjn=2,hqzl-,vhx-,kbtbvk-,ls-,ngc-,zm=3,sp-,vhx=4,vbt=8,rc=8,dc-,rg=3,tzdg-,lhd-,hksq-,ptr=9,hgnj-,bn-,ftx=3,jl-,dbh=1,gt=2,qkdghh=7,mbnpqk-,xpt-,qdssh-,cb-,dd-,tk=4,dlh=4,ks=1,th-,dcf-,nd=3,jqxdvz=9,gknp-,qzzdfc=7,mlg-,jbng-,bnf-,cq=1,qm=1,cs-,kfgm=8,cx=9,bpgjt-,vdx-,sjd=9,xd-,lmbhsl-,nb=8,zsf-,gkc-,xjs-,bpd=1,hzg-,jlkf-,hm=9,llr=6,vs=1,fhxq-,kzt-,gvzt=4,pn=2,czlvx-,kxssfz=5,jdvx-,jdvx=8,nxsx-,qgp=1,krsn=3,qlt=9,vckn=7,csr=5,nj-,cc=4,bn-,zdn-,mbnpqk-,nlmjb=4,hp=7,xf-,qdht-,qv=2,tmxl=3,ggdh-,xpt=3,ggdh-,sjvs-,kbtbvk=5,sl-,bgc=1,dmbgc-,jgvk=6,gtp=2,qgg-,mv=3,qdht=7,frqh=7,bj-,bpgjt=5,nlmjb=5,fhxq-,bvm=7,cn-,qxm=4,rd=4,zxz-,jqxdvz=9,zsf=3,xm-,jjfth-,xgh-,jxjns-,sj-,btvq-,pnb=3,psh-,hpj=1,tvz=4,bpzlm=7,xm=5,bshcdm=9,bpd=4,lmrl=1,cqn=3,lmrl-,mkt=7,vrmt-,gkc=4,hsk=7,gct-,xf=3,jlfmd=3,jvbr-,df=6,xbddc-,gtd=6,vgdb=9,txvfx=7,ngdqv=8,md-,rg=7,zgsvvh-,xdv-,mjbcps=3,jt=5,bzq=5,mfjf=3,jld=5,fvqx-,jfjn=7,jrdkpm=8,lk-,lclv=3,xzbk=5,jld=6,qzzdfc=7,glqxjt=9,fc-,cq-,mf=5,xzbk=4,kb=3,cx-,vnqslq=6,tfp=7,cn-,bv-,skh=7,jzr=1,qgg=6,qlv-,jcm=3,jlhx=3,lv-,bm-,nct-,mf-,xcc=1,nfq=4,njg=5,nrfpf-,hdlp=9,qm=7,rhr-,lt=4,cdj=8,bh=1,bhps=8,hgnj=7,bz-,kpz-,dk=2,jxv-,cb-,ccd=4,dmz-,lhd-,vzm=6,tsg-,bjgh=1,md=8,hpmvrg-,vqs-,cs-,srtdk=3,bdm=8,jqxdvz-,vbt=4,vdx-,jlkf=7,rld-,hzh=3,cdj=3,xnz-,mvbgm=2,fzq=8,xg-,gjtq=4,gtg=5,vgdb=3,cdj=3,sz-,gnknqg-,xd=3,fksj-,nrfpf-,hzrv=8,htf=7,dj-,dcf-,jh-,lnkkl-,dbh=5,jms=1,hgt-,fpj-,ngj-,bpzlm-,fs=3,hpj-,brh=1,pld=1,tft-,xpt=8,dndnrr=8,ppm=1,mjrkp=9,qcn=5,brn-,gm-,mrm-,lmrl-,sjm=1,rx-,hqzl=3,kdt=1,vbt-,hxr=7,zz-,cb=4,bp-,zkj-,gbp-,gbp=6,vs=3,njg=2,cs=2,rvcl-,mhz-,lk=6,ftx-,ngk-,ngj=5,fk-,bsvql=6,qz-,btbbbg=3,jtm-,rv-,vdkm-,sfhd=9,hzg=7,rg=7,qv-,rg=7,thddd=5,fkg-,qp-,skh=2,dd=1,sg=3,bdxx=9,fp-,pncdx-,nd-,qkdghh=1,kmc-,nj-,lr=8,bm-,smk-,bpgjt=4,xtd=7,kgtg-,jxv-,sjd=8,lvh-,tj=7,smk=8,tx=9,jrk-,vckn-,qsvbm-,xq=5,krsn-,gt-,kxh-,dd=7,fcpm-,cn=8,hzrv=2,bdxx-,qgg-,bhps-,tfp=8,nkbb=6,phxt=9,ldg=3,cr=8,phn-,gndr-,vdkm-,rqvjg=5,btd=6,dnr=5,mkt=1,cf-,rxc-,hfr-,bpf-,czlvx-,vds=1,jbn=6,pn=6,zmj=4,vnqslq-,gxp=3,kdt=7,nhzcjt=9,qlt=2,kklrf-,pclz=9,czlvx-,bh=6,zxz=9,zxz=8,ptr=9,vckn=3,zbnl=3,bz=7,skh-,pvn-,pclz-,jrdv=5,dk-,ngvmt-,nbvjjn=4,srtdk-,kzt=2,fvzbl-,kgg-,dq-,xpn=9,xpn=1,jrk-,gndr-,tjbpt-,tvz-,kbtbvk-,pvs-,bc=6,hkdl-,xnt-,zkb-,zkb=9,gm=2,sp-,fgbjn-,sl-,jtqb-,gxp-,dj=2,xcg-,pd=8,czlvx-,fnkf=8,vbt=6,tp-,tvz=8,fpj-,dlh=5,hlrzf-,gbfp=3,dndnrr=5,sjm=7,qgg-,lsz-,sjkxq=7,snh=1,hqtr-,cfx=6,tjbpt=9,pg=7,qkdghh-,zsf=9,zd=3,sff=2,jpm-,cx=8,knv-,xj=7,dbh=7,dlmm-,kklrf-,tjbpt=5,mxnm=9,jrdkpm=7,hgx=8,gkc-,jzr=6,sj=9,xq-,zq=3,glqxjt=4,zgcr-,jgvk=4,lmrl=5,cf-,qcn=7,sjm=8,zxz-,mrm=6,kxh=3,snf-,nkbb=3,lfqd=2,cq=3,jvbr-,kqkxm-,nkbb=4,dcb=5,nclb-,mlgj=2,lv=5,gpd-,mkt-,lsz=6,qff=8,mrm=7,plc=5,rrcx-,glqxjt=6,fp=8,skh=8,jpm=7,jsbjs-,gknp-,hpj-,hp=8,rqvjg-,bjgh=5,jpm=5,nzh-,db-,jxjns-,zxz=9,bgc=4,vgdb=6,cq=3,jtm-,dcf=9,mz=5,xpn-,zm=5,xnz-,bhv=6,jcm-,jfjn-,gtp=3,sjm-,xs=6,dbtq-,jnr-,ptr=2,hxr=7,bpd=8,mr-,xjs-,ppbqks-,fk=5,nb-,bf=7,qgp=8,vdx-,sdp-,rqvjg-,txvfx=1,phn=2,hv=1,gct-,jk-,dndnrr=1,rldfx=6,lnkkl=1,qkdghh-,fzq-,dh-,bpg=4,phn=9,kdj=9,skh=8,dlh=2,lzqxj=1,jqx-,nfq=2,fds-,hsk-,pz=1,psh=1,dtpzc-,kjfhxf-,cnspxc=7,vf-,xm=9,jl=3,tt-,xpt=5,xs-,snf-,bpzlm=7,gvzt-,sdp-,xm-,lqbq=4,brn=1,bv=2,xgh=3,zlj=2,ch-,dprj=3,bn-,mlg=4,kfgm-,ppm-,djgv=5,zkb-,xt=2,kdt=5,dmbgc=1,gd-,gpl=6,zcz=2,vj=9,kxh=2,gm-,nhzcjt=7,jpv=7,qv-,fds-,gm=1,djgv-,mvbgm=5,fc-,nct-,sdp-,btbbbg=3,lpkjx-,zm-,zl-,htf=5,rtf-,skz=9,rxc=5,dq=8,nnfmq-,mf=1,ht-,vp=8,ldg-,pslp=3,djgv-,vbt=3,ptr-,fp=2,mbnpqk=9,kfgm=9,nclb-,bhv-,jtqb=4,jgvk-,vg-,qr=4,ldg=4,vc-,rpq-,tsg-,xpn=1,jsbjs-,rzp-,tvmgv-,jfnt-,xr=4,mlg=2,vhf=5,rv=4,nm-,zkj=8,xdhk=9,lv-,zxz=5,qkdghh-,dd=4,hnqsn=9,kz-,jgkvdq=6,pq=2,jd-,zb-,tn-,lmbhsl=6,jn-,lqbq=4,gpl-,hk=1,nq-,sff-,lhc-,llnh-,hqzl-,fmg=2,gkqs=6,mjrkp-,tnf=7,hpgq=3,gxp-,snf-,jt-,lmvv-,mbt-,kqmx=1,kc-,bgn-,xcbsp=3,fthhk-,bpg-,hd=4,csr=8,xcc=9,cc-,dndnrr=1,fcpm-,rrcx=6,gknp-,fhxq-,gbfp-,lmvv-,rpq=9,srtdk-,df=8,tn-,zd=5,fpj-,hv=1,pq-,ht=8,lnkkl-,frszb=2,mbnpqk=6,nm=9,ghrx=8,dcf=3,lfz=7,xf-,mf=5,gct=7,bshcdm-,dprj-,rsnmqs-,tft-,fthhk=6,vhx-,sn-,bm-,qn-,rg=2,frszb=7,jlfmd-,bf=2,xlh-,bhv=7,jtqb=9,pncdx=6,rtf=9,nxk=7,gm=8,ppt-,vdx-,hqzl=4,vckn=3,drdn=1,bp=3,jtqb-,krsn=2,jlhx=2,sjkxq=2,tnf=8,rtf=7,nztgd=4,fzq=9,ldg-,hds-,btd=2,szr-,ck-,fvk=2,fmg=8,mqgk=6,xr-,knv=7,kdj=3,qhzp-,zg=9,xcc-,dtpzc-,jtkj=4,hgnj-,th=9,pvn-,gbp=6,nbvjjn=5,rldfx=8,mv-,phn=5,ftx-,bsvql-,tt=7,jqx-,lv=2,mjbcps=9,qzf=8,nxsx-,mfjf=2,xq-,pvn=7,btd=7,xlh=7,zdjz=4,xjs-,krsn=9,lhc-,mkt-,ptr-,bnl-,skz-,vrmt=6,qhzp=9,tfp=5,qdssh=9,fnkf=5,bsqpf-,mlg-,rzp-,mqrr=8,szr=9,kzt-,chsk=9,lzqxj-,gbv-,qr=4,nxk-,fc-,dhct=8,gtg-,gcc-,sl=5,xcg-,vnqslq=5,kkt-,vs-,bz=4,rqvjg=2,ck=5,hsk-,dcb=5,bvm=5,tsg=6,dmf-,bdxx-,dbtq=4,jrdv=8,lnv=5,pncdx=9,jbn=3,qd-,fds-,mbnpqk-,xpn-,hp=9,tmxl=7,lhd-,phn=4,tfp-,hp=4,gtp-,bnf=4,hsk=6,ht-,gd=2,cc-,jgkvdq=9,zb=2,fds=2,jtkj=2,cdj=8,fzq=1,kqmx-,hx-,rsn=3,vv-,hd-,hk=4,bnl-,gbp-,vgdb-,gtd=9,hlrzf=2,btvq=1,gcc-,gpl-,srtdk-,pbft=9,pg-,nhzcjt-,djl-,dhct=7,ggdh=1,nrfpf-,gm-,kg-,br=8,ngj-,xq-,bnf=1,lvh-,bpg=7,hzh=9,hm-,glqxjt-,bzq=4,nq=5,fgbjn-,fksj=6,qgg=3,vc=8,hzrv-,pbft=2,kjfhxf=7,sn-,rp-,xt-,dh=3,zl-,vf=1,nclb-,fvf=6,qm=7,fk-,kcgg-,hpmvrg=9,hgnj=9,nxsx=6,tn=4,tjbpt-,fvzbl=4,gbp-,jxjns=4,gbp-,krsn=1,tvc=9,vgdb=7,vv=2,jqxdvz=6,jfjn-,kx=5,llnh=4,rpq-,jdvx-,dprj=7,cb-,hm-,skh=3,bnl-,tft=2,mlg=6,zxz-,vj-,mkt-,lk-,mdp=1,gm=2,hpmvrg=2,btd-,dbtq=5,vc-,tq-,gxqs-,vg-,bzq=1,sl-,vhf=7,qsvbm=2,zm-,qlt=6,bh-,fcpm=1,xnz-,vs-,rdl-,gbv=3,gtg-,ks-,ldg-,nlmjb=2,qzzdfc-,tnf-,cs=7,ppbqks-,dmbgc-,brh-,czlvx-,gknp=8,hkdl=4,skz=8,hdlp-,khcq-,bh-,cf=3,xs-,tqt=3,vdx-,mfjf=2,fvqx-,phxt-,frqh-,xgh-,hxr-,hg-,mqgk=4,bnl=4,xpt=2,dprj=8,rtf=8,dbtq-,gknp=4,bv-,bz-,fl=1,bv=4,smk-,frszb-,rl=4,kfgm-,jtm-,nxk=7,qdht-,jzr=7,szr-,zt-,dcf-,dk-,vc-,jm-,hfr-,qd=7,lpkjx=8,ch=3,kp-,lqbq-,hp=7,lpkjx-,th-,hpmvrg-,hv=4,jms=8,zkb-,fcpm=2,qkdghh=5,vnqslq=6,sql=4,zg-,xgn=1,nd-,fvqx-,bh-,gtp=8,qff-,nm=6,dq=6,pslp-,df=1,pn=5,fnkf-,dmz-,gtg=3,qv=6,jjfth-,hxr-,nxk=9,snh-,rrcx=3,vhf-,tvz=6,pslp=2,tjbpt=1,nzh-,rd-,xq=9,dn=5,jkh-,kfgm=4,xks=8,dcf-,bpd-,vzm-,hxr-,bgc-,kzt=4,dkhv-,rz-,vrmt=7,hk=1,kgtg=9,hgnj-,pvn=9,kp=1,fds=7,ls-,df=1,ggdh-,bj=9,vp-,gct-,hdlp=7,fmg-,sjd-,nhzcjt-,rdl-,srtdk-,tk=1,jxjns-,mlg=7,qdssh=9,tk=7,pn=9,hgx=1,ngvmt-,sxlz-,skz-,cx=1,qz-,pz-,jm=4,vdkm=5,nxsx=6,xf-,jgknh-,lj-,fhxq-,kjfhxf=5,ln=5,pclz-,hx-,kkt-,qv-,cx-,nrfpf=7,mfjf-,hpgq-,pg=3,xcc=4,qhzp-,rl-,mqgk-,gxx=6,jzr=3,mbnpqk-,jfjn-,gpd-,fz=3,fs=8,nnfmq-,cr=3,gxx=8,hk-,vdx-,mf=3,jtm=9,qdht-,tpv=9,xpn-,kcgg-,gkc=1,lclv-,jtkj=6,dd=8,mqrr=7,ghcf=3,xnz=9,sz=8,xzbk-,xtd-,bm-,xcg-,xpq=8,sff=3,hnqsn=9,llr=5,nrfpf=7,tqt=7,rxc-,tvhg-,tq=4,tq=8,nkbb=8,jbng-,xf-,ppm=3,bh=3,qm=2,qff=3,ccd-,jms=4,snf=1,ppt-,hp-,qzf=3,szr-,cb=5,xd-,xpt-,thddd-,hzrv-,jlhx=1,fmgxs-,sfhd=7,lk-,jt=1,rhj=1,bpd-,htf=8,tvmgv=6,jkh=3,jqxdvz-,lt-,xdhk-,dmbgc=7,fx=5,tmxl-,ht=7,rldfx=3,pg=3,vqs-,dsxdt-,zl=1,zsf-,ntrxnc=8,pncdx-,fksj=8,fds-,lmbhsl-,rl-,lt-,gjtq=5 diff --git a/2023/15/src/main.rs b/2023/15/src/main.rs new file mode 100644 index 0000000..180463f --- /dev/null +++ b/2023/15/src/main.rs @@ -0,0 +1,172 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::{Duration, Instant}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// COMMON + +fn hash_string(s: &str) -> u64 { + s.bytes().fold(0u64, |mut accum, val| { + accum += val as u64; + accum *= 17; + accum %= 256; + accum + }) +} + +// PROBLEM 1 solution + +fn problem1(mut input: Lines) -> u64 { + let line = input.next().unwrap().unwrap(); + line.split(',').map(|s| hash_string(s)).sum() +} + +// PROBLEM 2 solution + +#[derive(Clone, Default)] +struct LensBox { + // lenses k: lens label v: focal length + lenses: HashMap, + // order is the lens stack, values are labels into lenses + order: Vec, +} + +impl LensBox { + fn add_lens(&mut self, label: &str, focal_length: u8) { + // If there is not already a lens in the box with the same label, add + // the lens to the box immediately behind any lenses already in the box. + // Don't move any of the other lenses when you do this. If there aren't + // any lenses in the box, the new lens goes all the way to the front of + // the box. + if self.lenses.insert(label.to_owned(), focal_length).is_none() { + self.order.push(label.to_owned()); + } + } + //If the operation character is a dash (-), go to the relevant box and + //remove the lens with the given label if it is present in the box. Then, + //move any remaining lenses as far forward in the box as they can go without + //changing their order, filling any space made by removing the indicated + //lens. (If no lens in that box has the given label, nothing happens.) + fn remove_lens_if_present(&mut self, label: &str) { + if let Some(_) = self.lenses.remove(label) { + self.order.remove( + self.order + .iter() + .position(|s| s == label) + .expect("We had the lens in lenses but not in order?!"), + ); + } + } + + fn total_lens_power(&self) -> u64 { + self.order + .iter() + .enumerate() + .map(|(lens_idx, label)| (lens_idx + 1) as u64 * self.lenses[label] as u64) + .sum() + } +} + +enum Instruction { + Replace(String, u8), + Remove(String), +} + +impl From<&str> for Instruction { + fn from(s: &str) -> Self { + let op_pos = s.find(|c| c == '-' || c == '=').expect("No operation?!"); + let op = s.chars().nth(op_pos).unwrap(); + let (label, op_focal) = s.split_once(|c| c == '-' || c == '=').unwrap(); + + match op { + '-' => Self::Remove(label.to_owned()), + '=' => Self::Replace(label.to_owned(), op_focal.parse().unwrap()), + _ => panic!("unexpected op {}", op), + } + } +} + +impl Instruction { + fn execute(&self, boxes: &mut [LensBox; 256]) { + match self { + Self::Remove(label) => boxes[hash_string(label) as usize].remove_lens_if_present(label), + Self::Replace(label, focal) => { + boxes[hash_string(label) as usize].add_lens(label, *focal) + } + } + } +} + +fn print_boxes(boxes: &[LensBox; 256]) { + for (idx, b) in boxes.iter().enumerate() { + if !b.order.is_empty() { + print!("Box {}:", idx); + for label in &b.order { + print!(" [{} {}]", label, b.lenses[label]); + } + println!(); + } + } +} + +fn problem2(mut input: Lines) -> u64 { + let line = input.next().unwrap().unwrap(); + + let instructions: Vec<_> = line.split(',').map(|s| Instruction::from(s)).collect(); + let mut boxes: [LensBox; 256] = std::array::from_fn(|_| LensBox::default()); + + for inst in instructions { + inst.execute(&mut boxes); + } + + boxes + .iter() + .enumerate() + .map(|(box_idx, b)| (box_idx + 1) as u64 * b.total_lens_power()) + .sum() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"; + + #[test] + fn problem1_test_hash() { + assert_eq!(hash_string(&"HASH"), 52); + } + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 1320); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 145); + } +} diff --git a/2023/16/Cargo.lock b/2023/16/Cargo.lock new file mode 100644 index 0000000..e3ac8f7 --- /dev/null +++ b/2023/16/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day16" +version = "0.1.0" diff --git a/2023/16/Cargo.toml b/2023/16/Cargo.toml new file mode 100644 index 0000000..b0c00bc --- /dev/null +++ b/2023/16/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "day16" +version = "0.1.0" +edition = "2021" diff --git a/2023/16/input b/2023/16/input new file mode 100644 index 0000000..cf39e68 --- /dev/null +++ b/2023/16/input @@ -0,0 +1,110 @@ +\.....................\.......................|..../......-.........../.|.....\.............|..........|..\... +.../...................\..................../...|.......|........||...................../...........\......... +././....|......./................\............|..\...........-....../............../|........-................ +....\..\...........-/..|......|......\............|............../.................-........./...-...|.......\ +..................................|.\.......|...../.........................-...../......\..|...............\. +.....\.............................................|../.\............../--.......................\............ +|.......-.|........\.................\..............|........./....\.........|...................\-.-...|..... +....\........./....-.........................--.....\..../......................|.\......................\...- +\..-..../.....|..........\......................-|......|...................../...../........./...........-... +........................\.\|......../........../......................\.............|./../.................... +.....\...................|.............................|......-........|.........-./.../.|.................... +..-................./.........................................\..\........-..-........-.......\../............ +-................-............................\................./..--................-............../.|....... +..-.....|.............|................./.......-.-........................-............-...........\......... +..........|..........................-...|..\...............................................\-....\........... +/................../..........|....\..-......................../....\...\..........-....../.........../.....\. +................\....-................-|.\...-...........-.....-........\..........................\.......... +...................................\../..|.../../...........................\.........../.\-......|.........-. +/....|....|..........................\............./.....|/................./....-......................-..... +....-.............../.....|...........|../......\../.............../........................|............./... +......./..............|./../.................-.\.........../.....................|.........../........\....... +...................-...............\/.......|........|....................../.......................-/......\. +................\...../....-/......-.........-.....\/.\...............\....../...-....|......|.........\...... +....-.........-.........................-......................../.......|...............................\.... +......../..................|...../......\..\..-|-..../..............|......../............|.-.......-......... +......./............-..........-............................../.........................|\..-..-\.\......-..\. +.\-.....|......................-..\................/.-................|.\..............\..................|... +-..........................-............../.|.-......\......-..-...../......./................................ +....|.................................................\....|..........\...................-......|........./.. +................../...........-..............\......................................|.-........|.............. +...-............|..............-...........-.\.......................|..|..../......................../.|....- +.......-..-......./...../.........-.......\.....-.../............-./......../.../............-.....|...|...... +...............|.........../..........|/..........|..........-.-...../...../..|..........\.....-./.|./....|... +.........|......................\.|.....|......................-.-........|.............................-...|. +.-...|..\...-...............\..............\......./........-............/.-..\........../...../....-....\.... +...........\.-.......|.......|....-.............|-.|........../..../...\....\............|.|..|\.............. +.....................|.\....|......|../...\......|........................\......|.......-./..../............. +......................\--................................\.................|................-................. +-...............\...-.....................................-.\...../....\...........................|.......... +.....|.......\.......................-/...\..-.........-..../\|..................\........-................... +..|........./....|..-./..\..\................-|..........\../......\...............\............\............| +......-................../.............|.............\................................-..\...../........-..... +....................../......../..\.-.........................|....\..|...\...../../.....|............../..... +...\................................/..............................\........|.........\.-............\........ +/.................../................-...........-.|.............\.................................|.....-.... +.|...-..|/./../.........-./.............................................../..........-.........-.|./.......... +..../..................|......................|../..........\........-......../.........\...|................. +........./..................|..-..........|\............\......-..-..\...|....|.....................|....-.... +.....|/....|........\.........-................../..........|............|...........-.....|......./......//.. +..........\.|../...|........................\.........\.|......||....-...............\\.....|.|..............| +............-................|.....-....||......../.../.......................|..\...........\................ +...............-..../......-..../.......|-.....-.....\..................................../..|................ +/......-...............\/...././..................\..................\..........-......................|...... +...................\.....\.................\...|.................-............/........-..............\./..... +.-.\....\........\........-../.-............../...../..-..|...................../.-.....\.....-........../..\. +|...|...............................|............../.............../....................|...../............... +....../....../...|..............-.-........-..|.../....................\|.....\......................|....../. +...\\.......|...|-............|..........-...........................................\/...../..-.-............ +.........|.............|..|......\............\...............|......\......\.............\...\-.......\.....\ +...........\..-.|.......|\..\..................-........\/...............................-.................... +...\.................\..............................................\...............\.......-.../............. +..\.............\....---....................//.......................................|./.............../\..... +.\...........|..................................\........-......|.........\.................-........-........ +..............-...............-.../....-........|.....-../......................./..../........../../--..\.... +.........-......\......-.........../...\./.|.................\...................--..-........|..\......\....| +....../.........................................|...../........../..............\\...-/.......-......\..\..... +.........../...\.|..................../.\.................|......\.\..|.........../...../.....\.\...........|. +..../\..-...../.............................../..../............................-...-......\...........\...... +..-..\.............................\...........|..........|.........|..........................-......./...... +-..|.................-\.....\...............\/.....|...........................././...\......\.......\...-.... +......\..............|..|..............-............../....................-....\\........\....|.............. +|.............\....\./.....-|-....\.....\..............\/........-.|..|...........-..\......-..../.......|.... +............................-.......................|........................../................|............. +/................|../............\/.....\/....-.....\......\...../|..\..........|............../.............. +.|....../...../....................../..|.....................-................./...../-.........-............ +......................\|........../......./..\........\.........-......../.........../.|/.......\.../......... +...............\......-|............\..\....../.........\.....|...|\............/............\.\..|.......\... +......|...........\....../....\./......./-....\../.......\...-...............\./...\.....-.......-..../....... +...|..|.............................\.....-.|........./......../......................-......./............\.. +.....|........\.........|...-.-.........\..../....../..-.......-...............\..../.....-..../.............. +........./.\/........................-..........\.....................|.......-..............\./.\............ +..../................/................................|.....||..............\........-...............|........ +....\./.........../\-.....-...\............//.\..............\...../................|.....\..-................ +..../|........./............|.....................|......................-..|...............................\. +..................\............................\.......\.........-.............|...../.-.............-......|. +..............|.-...........................\.\./.......\........../...\................/....-................ +.......|................/-............\............/...........-..../...........\......../...\.......\........ +..............|..../......................-...........-..\.................................................-.. +..............................-..-................-.......-.................-..........\.../...............|.. +......-.........-........................./.....................\....|........................................ +.....\........./.....-.................../................|...................|........................|...... +........../.../........................./.|................-...........\...........-....|..................... +.....-.......\././/.......|..............|......../..\...........\.............................-......|../-.-. +........|-....-....................\.....|\...\/.....\........\......./..........|.\-..|...................... +../...-...-...|...../............\.......................-..........\...\.....-...\...............|....../...| +............|..\....|..................\/...................../../....|...................|.............../... +.............-................./............|.......|............\....................................\.-..... +-...../|..............-......\..../..................|..\....-............................................\... +........|.........-........\................/....\..................../.|...................................|. +.....|................./.........-|\...-...................|....-................-......../...............|..- +.|.........|...........|...-.............................\............................|................\...... +....|.....-......|......./.-.|...........\..-........|.........................-....-..-.....|................ +........................................\.....................-.../........................../../..........|.. +-................/....\.......//....-........................../.\.........-......./..|\..........\........... +-............................./......................................../-...........................\|........ +........................................../-.....//......\......\...........\........|./....../.......--...... +../..............-....../......../................../......./....-...........|../.......\.\.......-......./..\ +..............-............../......|.........-...............|................................-..........\... +.....................\..........|..\......|...............................................-................... +...../..|.../.........|.../.......-...................................\.....................................|. diff --git a/2023/16/src/main.rs b/2023/16/src/main.rs new file mode 100644 index 0000000..b3f82de --- /dev/null +++ b/2023/16/src/main.rs @@ -0,0 +1,265 @@ +use std::fmt::Display; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::iter::repeat; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +enum Interaction { + One(FromDirection), + Two((FromDirection, FromDirection)), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +enum FromDirection { + Left = 0, + Above = 1, + Right = 2, + Below = 3, +} + +impl FromDirection { + const fn mask(&self) -> u8 { + 1 << *self as u8 + } + // return the new pos for a ray that will be from the direction. + // a ray 'from' left 'goes' right + const fn goes_pos(&self, pos: (i64, i64)) -> (i64, i64) { + match self { + Self::Left => (pos.0 + 1, pos.1), + Self::Right => (pos.0 - 1, pos.1), + Self::Above => (pos.0, pos.1 + 1), + Self::Below => (pos.0, pos.1 - 1), + } + } + const fn reflect_ne(&self) -> Self { + match self { + Self::Left => Self::Below, + Self::Right => Self::Above, + Self::Above => Self::Right, + Self::Below => Self::Left, + } + } + const fn opposite(&self) -> FromDirection { + match self { + Self::Left => Self::Right, + Self::Right => Self::Left, + Self::Above => Self::Below, + Self::Below => Self::Above, + } + } + const fn reflect_se(&self) -> FromDirection { + self.reflect_ne().opposite() + } + fn interact(&self, tile: char) -> Interaction { + match tile { + '.' => Interaction::One(*self), + '/' => Interaction::One(self.reflect_ne()), + '\\' => Interaction::One(self.reflect_se()), + '|' => match self { + FromDirection::Above | FromDirection::Below => Interaction::One(*self), + FromDirection::Left | FromDirection::Right => { + Interaction::Two((FromDirection::Above, FromDirection::Below)) + } + }, + '-' => match self { + FromDirection::Left | FromDirection::Right => Interaction::One(*self), + FromDirection::Above | FromDirection::Below => { + Interaction::Two((FromDirection::Left, FromDirection::Right)) + } + }, + c => panic!("invalid tile {}", c), + } + } +} + +impl Display for FromDirection { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Above => f.write_str("above"), + Self::Below => f.write_str("below"), + Self::Left => f.write_str("left"), + Self::Right => f.write_str("right"), + } + } +} + +struct Contraption { + tiles: Vec>, +} + +struct VisitState { + visited_from: Vec>, +} + +impl VisitState { + fn visit(&mut self, pos: (i64, i64), dir: FromDirection) -> bool { + let pos_state = &mut self.visited_from[pos.1 as usize][pos.0 as usize]; + if *pos_state & dir.mask() > 0 { + false + } else { + *pos_state |= dir.mask(); + true + } + } + fn score(&self) -> u64 { + self.visited_from + .iter() + .flatten() + .filter(|c| **c != 0) + .count() as u64 + } + #[allow(dead_code)] + fn dump(&self) { + println!("Score {}:", self.score()); + for line in &self.visited_from { + println!( + " {}", + String::from_iter(line.iter().map(|b| if *b == 0 { '.' } else { '#' })) + ); + } + } +} + +impl Contraption { + fn height(&self) -> i64 { + self.tiles.len() as i64 + } + fn width(&self) -> i64 { + self.tiles[0].len() as i64 + } + fn cast_ray(&self, pos: (i64, i64), dir: FromDirection) -> VisitState { + self.cast_ray_inner(self.empty_state(), pos, dir) + } + fn cast_ray_inner( + &self, + mut state: VisitState, + pos: (i64, i64), + dir: FromDirection, + ) -> VisitState { + if pos.0 >= 0 + && pos.1 >= 0 + && pos.0 < self.width() + && pos.1 < self.height() + && state.visit(pos, dir) + { + match dir.interact(self.tiles[pos.1 as usize][pos.0 as usize]) { + Interaction::One(dir) => self.cast_ray_inner(state, dir.goes_pos(pos), dir), + Interaction::Two((dir1, dir2)) => self.cast_ray_inner( + self.cast_ray_inner(state, dir2.goes_pos(pos), dir2), + dir1.goes_pos(pos), + dir1, + ), + } + } else { + state + } + } + fn empty_state(&self) -> VisitState { + let mut visited_from = Vec::new(); + for _ in 0..self.height() { + visited_from.push(Vec::from_iter(repeat(0).take(self.width() as usize))); + } + VisitState { visited_from } + } +} + +impl From> for Contraption { + fn from(lines: Lines) -> Self { + let mut tiles = Vec::new(); + for line in lines { + tiles.push(line.unwrap().chars().map(|c| c.into()).collect()); + } + Contraption { tiles } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let contraption = Contraption::from(input); + + contraption.cast_ray((0, 0), FromDirection::Left).score() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let contraption = Contraption::from(input); + + let rows_max = (0..contraption.height()).fold(0, |max_tiles, y| { + std::cmp::max( + max_tiles, + std::cmp::max( + contraption.cast_ray((0, y), FromDirection::Left).score(), + contraption + .cast_ray((contraption.width() - 1, y), FromDirection::Right) + .score(), + ), + ) + }); + + let cols_max = (0..contraption.width()).fold(0, |max_tiles, x| { + std::cmp::max( + max_tiles, + std::cmp::max( + contraption.cast_ray((x, 0), FromDirection::Above).score(), + contraption + .cast_ray((x, contraption.height() - 1), FromDirection::Below) + .score(), + ), + ) + }); + + std::cmp::max(rows_max, cols_max) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &r".|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|...."; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 46); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 51); + } +} diff --git a/2023/17/Cargo.lock b/2023/17/Cargo.lock new file mode 100644 index 0000000..699faef --- /dev/null +++ b/2023/17/Cargo.lock @@ -0,0 +1,141 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "day17" +version = "0.1.0" +dependencies = [ + "itertools", + "termcolor", + "test-case", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "test-case" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" +dependencies = [ + "test-case-macros", +] + +[[package]] +name = "test-case-core" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "test-case-macros" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "test-case-core", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/2023/17/Cargo.toml b/2023/17/Cargo.toml new file mode 100644 index 0000000..91bc60e --- /dev/null +++ b/2023/17/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day17" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" +termcolor = "1.4.0" +test-case = "3.3.1" diff --git a/2023/17/input b/2023/17/input new file mode 100644 index 0000000..8dde385 --- /dev/null +++ b/2023/17/input @@ -0,0 +1,141 @@ +325324343122414426341311521355436756516735365767735336243266456152424263655442375437323425146317632655141433156665635625563452536534233413111 +445243425411645566112351545244163515223165477656331552463117221625365717275545124476464451442531715677337534443363223313661263112221651414352 +434514252414363452511152554236546325615346321237216231776737145123246363247221568835776177657563335176174462444145653231645541641352324222432 +512344115556454364551122335531173547353527264165771324616431564154352161434333624143426673724271674725735171453732677454152514121553642514342 +245335631545534511262451241637156561454332212544137121477616833536676467486356424113643275743254655271257165273233333713436363316152215525412 +525134431665424254245311755242225774344146256415414763387571367658688754454216857288228327638844315166557547372743531744154253555366125322251 +242226151434514231642522163777343413375375427786665751532313584425311152314384883312415875143615383175544722257361643371324456124635121426143 +355256351224414362134546117516475527772757412718633456138147647447166316435131753115166364762163147767575652646664125323524233344211152621644 +423662265151444333172175576611421743625662125375255611436332835555433412245516663422413367584728254346217161613476513362411124354554141656661 +343132241425514222366532375754443311661867761827448367742356551447788222565282822336472646562481274186323321263463733172737523415231263231452 +414512633246125157451572561554537454722163673577424576376853177774626628186236767857316633668123264385753152773455553614673334436441316224135 +212324565521155353412567475573615133714344617411175657288438127655616781713123465347422118615674666483872652757672245614227473525561435343566 +445221414451625675624645665212427183844173224183625244868437762281745333878761243125126616283437123288348253513642236266513777143336423452244 +363544111542646272625122735154448186467255668146344132826437421272718576129612166754885626667262876277185872166576146412755315427431235361345 +264656533336664366256136422751241622361444458382586165836578937121934658199719386351541531343455316352721641517715741643374346623641245141642 +112445611653647577632776715757651127562631764334214565265123726662877597342896255454454353581576436364858847152147334456111671241736213243235 +555625554155515642753141133472357442522378575788761684343125889676496481416737941365773489236312262742358647378637816625524652376464221145552 +166212536136161764262663264151715412324265162172461388359546247927137713732357391885873753572632213528125746133582543132557354616253656161125 +262214154431312345513141264637587481623632438762129328373598884879612652692926671574635499263972534168721832253467846424556241116716116355662 +135241432555162671524462538183836241418472746146531476679922477637662841812239647283188595978522895688251211412744783771567244452546153345245 +532216637311743517223634115741886133752415248862823922293142226987777614157674447415432957111246289861372245785828235814464543732552357524115 +146561263436471442615526287283328418146343943213682363991837281378678451857923183343659739289952517643483666668454477612872264623531527114156 +553563547345231122325535834576322628262281752991871677465471578556275546387834293362893217319352873468631622524488454757543336657573343346231 +266564156547622167463643435425825221285933354618914693416371827482999473949778132544823891982233412759525148822255432468651261727135416714414 +241435326144677632666655834466775762193454726392523657676252731186895484227244331982511164875543224648614243615642216164551727621224255321645 +165625766274415126221387567754684125769562911449418567328954999269442383397657742111976735747616543945193885678317732613823354147551434172254 +663471266547665567383385744766857544125555773727948178721595766362768764763389283465275425521861727174339179777676482636283377221474131172664 +552267351614751263715528842613544492131357823275224224529882976386327823433479377684379863655876688634865837667534467375571818473234352527123 +417167733326125612732158464675728774915115736789599864674655267296528289764653496935556672211933382185491674889326327638863358225141641776661 +553433447763465113852224382474727588458713673836733584749236658844822472658497492547688383864951836467139432662174767657312876885715517172566 +667541167336254488524837342876671664656562954852183656576233333873254363945623456596769545333862526193411672878826354158866344875721272113745 +742742423566122778386568143632291815363624774439785729782249926489483479225989595798258826325346485866553327934443377824316727757316672416437 +422416173617118884742181153285466515613425316646999647735464985688784946488843572663439527248557925282489796183853745584334188218713316517611 +763175754752411761475574463499814429498138728567958989277844494897966247687976299566598923968224276327562217938234278212488752542237314351724 +556357515132757111313386163174758761223833727635896636363788757464722947589395479356794346762935869851131172237499953267126584137242266563332 +553452514551382115465845459551888517462873873925388334689354634986788558835365333394575696667743754281865874432573149225448868716422314153151 +727477115766217727216544583899411845473274393224259527268426677665382498542244536749359523368784334883684284657479479623145838735713142311641 +623775723417856274672723115245954791372868276248326532785676923354468396436867556933672689642689493334426665994793891938554866266411441635464 +357655727381528486471438934682246867878728953726863365263395378599979499989445494543554685888797994457246284425146494523847682845162841467676 +713225442368616642825874251457728254726498336436857496884894665436838647759737379449974974472942976778385826682519218241645473616645531255227 +143115132751882814645234335693993315693492273544964459798578395446856796646994466763377345499557552788438771441525234336612412423834346523211 +563124357115326738651179813293721543858493473499475888785648678877598767685393985868373765343793387586922432532313835672612126152631344576713 +161643744871583517778716716592732854695358997554424874653584484749995659355969399657538739855335364842584499341554829664297243422443742217627 +314417571311644786676857812476382568772944752389723949877773676947654886977363564564656448787568736238564893432112877333626254348575386766736 +776344328255614461719768964778491246343236536693785358783954458767465334474845788383458739454343787762929647765239152374349167566558438427423 +352364825562284245598758827867146952949744839636858679487354649333897878699343577583358936494456794644762845245583132395957674685442674213657 +165116474648463674164854916648534936489844557493948573494559834384533436539877799979968846353597267726398358327688234857762963775536887664434 +636244476616574846647739459365589464485573472239755849649363338696465548697637638734975433686777966529489995479371427436795444712248542244242 +637735864354122638598582193357329687493754334369459855467953667653894978798833698638878588356346875628939527283866627761726597671516685427174 +234265787355773655768346666121735692698653744885738497856483393689674898649577997399958433373787955768454347876736595261379656126328146286411 +112767655742256845212962577375777649885299555457767374476434484654767654957948859747787796354688878783666798563553685971677619173751653125632 +571453132563374754428433649129699672752544276898858748974354969458677564456979779546475974859365475874946272453658976328163915151248617627547 +142583162475317422481573625624997475387877377975334869858698855546579585985975987879498939886539444982256287887973954241619679433725388527327 +533247585173848868728351547857844568874594368494696993398568756785849787865568456854549687887848656666299259336837539132384839771741428127655 +615468573841357563119491171796494694742954878467956458799956898567956945658585548466878737443834395395589454855236733372996957432151473455417 +712281822873644374327211127428577898932274866883739393844879649754568787996648688464479984398399388463589596822886624259697865457588148666664 +136562416585765349961939548589267679836968874647535938986445975695988894954468499449998546743964657957566429465497651995126318134132576252611 +664553248645533826876894472974728737829658753737385958889458749467497446995476987665554788833743998485535738446299878511223783418241123523523 +216673485271213882299775989884276937282689933697933446545787966965866999969865545756445677989389673844562259846887466538876636858727352514172 +561327416424626319323885947575483566432886775386986678477789875855676946594954878464446759874339786347647885563257274772479849452341218853328 +543414611273635871798538959594987265275776698754487659699789747885978996899658685697449894894533384458736853354975392764758672388138231227341 +432177431672385492997519159785924344863965869338385868446897566987758776679699779999948444794449339687467966733362773574493894412663521343826 +347125512661263615493994284268932764773443456439595658497689867498656657588955796748798799774383987946863496648669435744485628519485388635745 +234414266212352323439914355995663935835737889578994846678946665969895859876758767589746666587937363599875397364293757755772863517557747336667 +165677453767745981471984424575369862567689838893465684578767899768788798796576788458996788867998737468547692998985973389685897556471872623552 +116786112155152692399989767286933237247737645595776648974959499775699595868698769845975468987579485487885883286826659428468372838324282111512 +377586871572362673428351636972532524637489943754658469776969888985756695665668759579764745956864346579749772568358833311888124717127726527183 +318164852453224592729434559244754674699557743559876747474974585695598957859985857696997477665456486667857796243525567741256626585522827113358 +256655568346617621446653354665386832434445968933784544487764789798668576865577679764775445666453683457777446985583583616592962898181266224478 +722447327541895771712452547924633958699394955868486968595868665886977559577966765766677478787645777643444882329378355275571729256528663733353 +681245847143816872485464897478336665868833439643778745577556798997858795969859958658867557947956848853484479399665572351893714995112671712542 +311381662366127117979691628467662497783389559654568448976988776969785977759865769596989799759496863598934465593797484374132873555158552238226 +457474782125292676291123633778348492667699397478487495797899579777688999595566576657978586657959548666375867799586695984632767442686385738552 +446616157875894869538721247895884644444883958939694686857454797576766596769678977878866756444843564374954453689422974856237432393117533128483 +328513874786178732292652989455568257546978333547685649556859556989785958697896996969558664777693669444863357795442386542131312425674116545188 +747847212285726534863559799474986827436555837479976774676546886866957568785596798586957654994685373975436492788443972514832361138572512654161 +655775232613722364914271538968599287798576387849966696886855689868858988569956769648644589884777963696793654854647956227921717275975785623654 +133381381437798727198762329677547326989373878353369696599978596599769677757779877555897759958668785498543442888697699371483636586223726547367 +166558274267818548162234882895355743538677453683879454994868498865956757789887798484954454444478588833833824986689655715894154174185774415242 +744372761363824188479933164279993635485947644456865846757657575569785868879855879598958794566895394956885434499837577347367422731518858612887 +542154878866234222341249936289547276863933653697495455886575579875575878577655884578868949868795478447448539675945826676344156515447648384546 +727648137147279312991745915569869748967897863693484548957585769676995776768899556467797877594595457565597942644325989896333716781667423243534 +736513883717638924144115229662225798979374855467873564897654895888778776656646759488994944578647478549466472243343448517571862675853473616864 +472573641838727179484843174326596238323797674954847658846499779979687897698786957488978957887393589648535875663387463691985638774236788623553 +124821288351532635578793553795832779774448696673767567977447858477887878676469566499588899639674834547757355447365486454925564374273415537754 +473366376825217384388358298858346676865647498858449537467576484448696554994844568658866985448999588568865694253572422898257449323363556414355 +756585744556785281389581638793733695364655945467738676966784699696586944796568967556964687873563733383939393423897564823324453861637182853267 +565377885838215871729589251946467492483786596678474736965759584646969695859846694554478644897956448493988952388686515531438553966563718473673 +441237555152315781715534724764569644764966484434597935577849549996797944445598866585975464985538857347348739935777224728123383154322783665212 +251538277583635525788213179949236983839585564657438535396857976586555596886987597784677479797768335698476345286595542471641181174621263365826 +462645455814621845373415692662396785686584794946537846843758755467475597488876849747653439958865548869549767695386323199852847441248375338541 +463546443547812219518172999856822775645249657899384888764384466588464496998699844799448386644993497387873285579539449217398533624347675543541 +627134585837711668423259691198344552573775794347737374846947567564477798747547569756586957548779437697554747233864289854679427672835612465345 +453427178468376465724244651434249467592372777377954886698995943799758965795489579878489647936487995475548384882447855836557219515272555272172 +646164887363484557947971139665942397924584239956839476743645386597748689975975793667755335566355545754464596682873886675359686766371416867421 +242564673372458178195363168781494567985469634643657747396363336558884388469558778894739837494797653627595967524589231722865526482421344174765 +527234341744655648884683415323364239475568742248669646473336993435343838488698565959659888897554832493926543963268996957491745744384373681642 +551517286282283171869299626636836973839926825529596737846853348847465543768893358499548363898668447899594793834437575675969522241126258282432 +615113281186772747348241311277589768978579523525454785944673364478779885777788637938457768565594782776753548337442956913497758522663855544746 +642722552558282717243186261369979747472328286247489597466575759788373865579784983835765597583957266269348476872977744141394534221775432834361 +123255132786848881171289778858576544965558689395978795886845857873584539756667349675894535384688837649796799842596341266977334763116825677354 +135221265824413236323157332895552381288822335555367339575879847894968958335438953379578775739548683874737377552721612721268151575627367374516 +531372144561436544581261647461429475295794262997752694655885563973854977878549545376756598298288663367593432222189358593241862862567851525633 +626241253623585223276449612369373315149376774267977495966454874953877799753954666837937985625362576929726958529984811986441465531615443642317 +122237265686782174111763222458711389313762597662727299449372464353975654883644949566273662646296682598256439817316833461554838162575673712517 +723263336441628827211586677967134954541492446275676949475759793974657373976875788542576364482493264736749878125328845632357141826286877316536 +746376624247554234116778248423662686389547589753843874724295634799738747329256738622227935656823782646258367494222758652264524554281143475725 +247227652554571866348233254561957766785945979683577957537487658236743744853762365675828364963779438767411174735912626572384742318221667753414 +461752271733674688288542747127735521232319836579345595948435736687542357563568686627638662657552627936242586561889286338386747748155235227226 +141456214273548658577623736731491554843413163948364432765456955986253284949329344329743583497259864561513822597516642174356653353313422722126 +577514122213115543664523815812933592181557923163353748687669465747247978699468957873884592954895372236525435898266267231166186788652617642555 +733555724377412472456818878267576986747564915594433739398666323228924448924829375982299639364766271435835929364781825316741447833326252417676 +125222215152346652574685537442585295255477117856958664574265466997244968429967625364878556825897669923262572691766582725557644262753327717362 +467157357433453135876225656776561727957327836923637932444972557496674222793666868922685325945691218298588167748387555736343614515634266527674 +661776217564727341161434734284475862238735522767927168825678229454593732375258543947793974965685418316392331831342662517514464432121762671112 +322226175666414714887813726538477315884488153872775481665957678523393653399286989356544792237258381256625891911364264883241763171167313676775 +535433463745172562637474314438863546768596275157472615456823332796577429439255648883176346627241779499894798781815684141341115432556456643611 +114516531415276157226232376712646274882692898428926131576766198472672582965649659239179984932272293221585428551187274518216722651334142336632 +442465354622731723346571118328123567571116964772317433275893126613648877241944321925876477183764936251413286113213668326238173555355732216511 +464545125331445744558616761437753623789891666294688255519735998824794952561825912628843557695114221545433384531731727161876473665573223373362 +212622623666153674544543171825717224778543394854587565145835173211764734896255693756825741544633558375382657436484353324273475751235436535462 +426456174161671373114214337372228811531433159234863841562359873663253579618768533225871676475124418634651463174367571421653316524135337216611 +113264262762756157661221337642236412865163476628461551865256687522447798169631776999765762811667378264167441455366366874635763265145354445246 +426635433223761674317156651435514314141128668545421467652684475967588596316618624557527821975129731666663738726663526335152162713322637353345 +462625221553613622135255778417362232144315611424115765534629998153728136584747684638152634448139174522524258188274866446444754542614324644434 +636534326173461144145272421245357854354484176813154881324324289844919955923398583536826126465974374482435317112341641177362167413653631136334 +416121433661766275322466215778242625365252727337453283172471359263773988811988214463424763681262168421538852447148623325417465263633614152511 +124165661313426677723346621157741741434378575443131887844322537341118644829916279535424761288166365435817422811564415651251116735711112164331 +662213565461615664773261415161352365348334351167211364117737811331482474651845215456825315271337675186362621741624741731455143163362536213464 +353163654655614552631221351147137158834856172472651447352645383627716833776783781867423336224623844544643183687212315514251544774456463166616 +362441211445555544364416561477266288188517416482572255534821166861673587523181878873186281844855832334752187362742345667727277112563315642334 +431663234654426537411563411631545147111423513725427621386471155234676712466243246352318626253227328241736815415232666553761366744242426616315 +226531133633364435742641727266657131716261514281311262288684766444656242277146665425754546527885447432126232726412333341234543363451443141151 +132441514114413263365254644654352543741828713741248814178464374241323848417341456825584538518825768881832262324114555671261641363331666134362 +214126421452663325372554676727131463574734212374262156347777366377688162741748146151838743887681761444327446474521671324576334665511114122421 +351531625361155425124432552645227655625173237845122258431184132246265282346434414371684686452682717564246721521767254276776656342611514416424 +524134632564656514234312513351122255327641524888853223216877477886352121843137683831631511821167821614336237522461471235761565432361122352514 +345434525245243335351164562536536325371562644423648433177446878843262728235726362818655267541335336453673667665743636625434415221611313542325 +352232633361461221226332237671657471766723622375425212675228865561486835238657116512354337544131231554276255267433543145221253624656265664213 +533341362332521262331624612755675274227615131761716775227664373273765866252331525736334615365731142562664711644136322541112142123112632614445 +335424225554551224465145365617415232333565263422662657463575357742412565884351458348627343152461236262431544374247365213446446134632223333414 diff --git a/2023/17/src/colormap.rs b/2023/17/src/colormap.rs new file mode 100644 index 0000000..8aba848 --- /dev/null +++ b/2023/17/src/colormap.rs @@ -0,0 +1,531 @@ +use termcolor::Color; + +pub struct ColorMap(&'static [Color]); + +#[allow(dead_code)] +pub const COLORMAP_SMOOTH_COOLWARM: ColorMap = ColorMap(&[ + Color::Rgb(59, 76, 19), + Color::Rgb(60, 78, 19), + Color::Rgb(61, 80, 19), + Color::Rgb(62, 81, 19), + Color::Rgb(63, 83, 19), + Color::Rgb(64, 85, 20), + Color::Rgb(66, 87, 20), + Color::Rgb(67, 88, 20), + Color::Rgb(68, 90, 20), + Color::Rgb(69, 92, 20), + Color::Rgb(70, 94, 20), + Color::Rgb(72, 95, 20), + Color::Rgb(73, 97, 21), + Color::Rgb(74, 99, 21), + Color::Rgb(75, 100, 21), + Color::Rgb(76, 102, 21), + Color::Rgb(78, 104, 21), + Color::Rgb(79, 106, 21), + Color::Rgb(80, 107, 21), + Color::Rgb(81, 109, 21), + Color::Rgb(83, 111, 22), + Color::Rgb(84, 112, 22), + Color::Rgb(85, 114, 22), + Color::Rgb(86, 116, 22), + Color::Rgb(88, 117, 22), + Color::Rgb(89, 119, 22), + Color::Rgb(90, 120, 22), + Color::Rgb(91, 122, 22), + Color::Rgb(93, 124, 23), + Color::Rgb(94, 125, 23), + Color::Rgb(95, 127, 23), + Color::Rgb(97, 129, 23), + Color::Rgb(98, 130, 23), + Color::Rgb(99, 132, 23), + Color::Rgb(101, 133, 23), + Color::Rgb(102, 135, 23), + Color::Rgb(103, 136, 23), + Color::Rgb(105, 138, 23), + Color::Rgb(106, 140, 24), + Color::Rgb(107, 141, 24), + Color::Rgb(109, 143, 24), + Color::Rgb(110, 144, 24), + Color::Rgb(111, 146, 24), + Color::Rgb(113, 147, 24), + Color::Rgb(114, 149, 24), + Color::Rgb(115, 150, 24), + Color::Rgb(117, 152, 24), + Color::Rgb(118, 153, 24), + Color::Rgb(119, 154, 24), + Color::Rgb(121, 156, 24), + Color::Rgb(122, 157, 24), + Color::Rgb(123, 159, 24), + Color::Rgb(125, 160, 24), + Color::Rgb(126, 162, 25), + Color::Rgb(128, 163, 25), + Color::Rgb(129, 164, 25), + Color::Rgb(130, 166, 25), + Color::Rgb(132, 167, 25), + Color::Rgb(133, 168, 25), + Color::Rgb(134, 170, 25), + Color::Rgb(136, 171, 25), + Color::Rgb(137, 172, 25), + Color::Rgb(139, 174, 25), + Color::Rgb(140, 175, 25), + Color::Rgb(141, 176, 25), + Color::Rgb(143, 177, 25), + Color::Rgb(144, 178, 25), + Color::Rgb(146, 180, 25), + Color::Rgb(147, 181, 25), + Color::Rgb(148, 182, 25), + Color::Rgb(150, 183, 25), + Color::Rgb(151, 184, 25), + Color::Rgb(153, 186, 25), + Color::Rgb(154, 187, 25), + Color::Rgb(155, 188, 25), + Color::Rgb(157, 189, 25), + Color::Rgb(158, 190, 25), + Color::Rgb(159, 191, 25), + Color::Rgb(161, 192, 25), + Color::Rgb(162, 193, 25), + Color::Rgb(163, 194, 25), + Color::Rgb(165, 195, 25), + Color::Rgb(166, 196, 25), + Color::Rgb(168, 197, 25), + Color::Rgb(169, 198, 25), + Color::Rgb(170, 199, 25), + Color::Rgb(172, 200, 25), + Color::Rgb(173, 201, 25), + Color::Rgb(174, 201, 25), + Color::Rgb(176, 202, 25), + Color::Rgb(177, 203, 25), + Color::Rgb(178, 204, 25), + Color::Rgb(180, 205, 25), + Color::Rgb(181, 206, 25), + Color::Rgb(182, 206, 25), + Color::Rgb(183, 207, 24), + Color::Rgb(185, 208, 24), + Color::Rgb(186, 209, 24), + Color::Rgb(187, 209, 24), + Color::Rgb(189, 210, 24), + Color::Rgb(190, 211, 24), + Color::Rgb(191, 211, 24), + Color::Rgb(192, 212, 24), + Color::Rgb(193, 212, 24), + Color::Rgb(195, 213, 24), + Color::Rgb(196, 214, 24), + Color::Rgb(197, 214, 24), + Color::Rgb(198, 215, 24), + Color::Rgb(200, 215, 24), + Color::Rgb(201, 216, 24), + Color::Rgb(202, 216, 23), + Color::Rgb(203, 216, 23), + Color::Rgb(204, 217, 23), + Color::Rgb(205, 217, 23), + Color::Rgb(206, 218, 23), + Color::Rgb(208, 218, 23), + Color::Rgb(209, 218, 23), + Color::Rgb(210, 219, 23), + Color::Rgb(211, 219, 23), + Color::Rgb(212, 219, 23), + Color::Rgb(213, 219, 22), + Color::Rgb(214, 220, 22), + Color::Rgb(215, 220, 22), + Color::Rgb(216, 220, 22), + Color::Rgb(217, 220, 22), + Color::Rgb(218, 220, 22), + Color::Rgb(219, 221, 22), + Color::Rgb(220, 221, 22), + Color::Rgb(221, 220, 22), + Color::Rgb(222, 220, 21), + Color::Rgb(223, 220, 21), + Color::Rgb(225, 219, 21), + Color::Rgb(226, 218, 21), + Color::Rgb(227, 218, 21), + Color::Rgb(228, 217, 21), + Color::Rgb(229, 217, 21), + Color::Rgb(229, 216, 20), + Color::Rgb(230, 216, 20), + Color::Rgb(231, 215, 20), + Color::Rgb(232, 214, 20), + Color::Rgb(233, 214, 20), + Color::Rgb(234, 213, 20), + Color::Rgb(235, 212, 20), + Color::Rgb(235, 211, 19), + Color::Rgb(236, 211, 19), + Color::Rgb(237, 210, 19), + Color::Rgb(238, 209, 19), + Color::Rgb(238, 208, 19), + Color::Rgb(239, 207, 19), + Color::Rgb(239, 206, 18), + Color::Rgb(240, 206, 18), + Color::Rgb(241, 205, 18), + Color::Rgb(241, 204, 18), + Color::Rgb(242, 203, 18), + Color::Rgb(242, 202, 18), + Color::Rgb(243, 201, 18), + Color::Rgb(243, 200, 17), + Color::Rgb(244, 199, 17), + Color::Rgb(244, 198, 17), + Color::Rgb(244, 197, 17), + Color::Rgb(245, 196, 17), + Color::Rgb(245, 195, 17), + Color::Rgb(245, 193, 16), + Color::Rgb(246, 192, 16), + Color::Rgb(246, 191, 16), + Color::Rgb(246, 190, 16), + Color::Rgb(246, 189, 16), + Color::Rgb(247, 188, 16), + Color::Rgb(247, 186, 15), + Color::Rgb(247, 185, 15), + Color::Rgb(247, 184, 15), + Color::Rgb(247, 183, 15), + Color::Rgb(247, 181, 15), + Color::Rgb(247, 180, 15), + Color::Rgb(247, 179, 15), + Color::Rgb(247, 177, 14), + Color::Rgb(247, 176, 14), + Color::Rgb(247, 175, 14), + Color::Rgb(247, 173, 14), + Color::Rgb(247, 172, 14), + Color::Rgb(247, 171, 14), + Color::Rgb(247, 169, 13), + Color::Rgb(247, 168, 13), + Color::Rgb(247, 166, 13), + Color::Rgb(246, 165, 13), + Color::Rgb(246, 163, 13), + Color::Rgb(246, 162, 13), + Color::Rgb(246, 160, 12), + Color::Rgb(245, 159, 12), + Color::Rgb(245, 157, 12), + Color::Rgb(245, 156, 12), + Color::Rgb(244, 154, 12), + Color::Rgb(244, 153, 12), + Color::Rgb(244, 151, 12), + Color::Rgb(243, 149, 11), + Color::Rgb(243, 148, 11), + Color::Rgb(242, 146, 11), + Color::Rgb(242, 144, 11), + Color::Rgb(241, 143, 11), + Color::Rgb(241, 141, 11), + Color::Rgb(240, 139, 11), + Color::Rgb(240, 138, 10), + Color::Rgb(239, 136, 10), + Color::Rgb(238, 134, 10), + Color::Rgb(238, 133, 10), + Color::Rgb(237, 131, 10), + Color::Rgb(237, 129, 10), + Color::Rgb(236, 127, 99), + Color::Rgb(235, 125, 98), + Color::Rgb(234, 124, 96), + Color::Rgb(234, 122, 95), + Color::Rgb(233, 120, 93), + Color::Rgb(232, 118, 92), + Color::Rgb(231, 116, 91), + Color::Rgb(230, 114, 89), + Color::Rgb(229, 112, 88), + Color::Rgb(229, 111, 86), + Color::Rgb(228, 109, 85), + Color::Rgb(227, 107, 84), + Color::Rgb(226, 105, 82), + Color::Rgb(225, 103, 81), + Color::Rgb(224, 101, 79), + Color::Rgb(223, 99, 78), + Color::Rgb(222, 97, 77), + Color::Rgb(221, 95, 75), + Color::Rgb(220, 93, 74), + Color::Rgb(219, 91, 73), + Color::Rgb(218, 89, 71), + Color::Rgb(216, 86, 70), + Color::Rgb(215, 84, 69), + Color::Rgb(214, 82, 68), + Color::Rgb(213, 80, 66), + Color::Rgb(212, 78, 65), + Color::Rgb(211, 76, 64), + Color::Rgb(209, 73, 62), + Color::Rgb(208, 71, 61), + Color::Rgb(207, 69, 60), + Color::Rgb(206, 67, 59), + Color::Rgb(204, 64, 57), + Color::Rgb(203, 62, 56), + Color::Rgb(202, 59, 55), + Color::Rgb(200, 57, 54), + Color::Rgb(199, 54, 53), + Color::Rgb(198, 52, 52), + Color::Rgb(196, 49, 50), + Color::Rgb(195, 46, 49), + Color::Rgb(193, 43, 48), + Color::Rgb(192, 40, 47), + Color::Rgb(191, 37, 46), + Color::Rgb(189, 34, 45), + Color::Rgb(188, 30, 44), + Color::Rgb(186, 26, 43), + Color::Rgb(185, 22, 41), + Color::Rgb(183, 17, 40), + Color::Rgb(182, 11, 39), + Color::Rgb(180, 4, 38), +]); + +#[allow(dead_code)] +pub const COLORMAP_INFERNO: ColorMap = ColorMap(&[ + Color::Rgb(0, 0, 4), + Color::Rgb(1, 0, 5), + Color::Rgb(1, 1, 6), + Color::Rgb(1, 1, 8), + Color::Rgb(2, 1, 10), + Color::Rgb(2, 2, 12), + Color::Rgb(2, 2, 14), + Color::Rgb(3, 2, 16), + Color::Rgb(4, 3, 18), + Color::Rgb(4, 3, 20), + Color::Rgb(5, 4, 23), + Color::Rgb(6, 4, 25), + Color::Rgb(7, 5, 27), + Color::Rgb(8, 5, 29), + Color::Rgb(9, 6, 31), + Color::Rgb(10, 7, 34), + Color::Rgb(11, 7, 36), + Color::Rgb(12, 8, 38), + Color::Rgb(13, 8, 41), + Color::Rgb(14, 9, 43), + Color::Rgb(16, 9, 45), + Color::Rgb(17, 10, 48), + Color::Rgb(18, 10, 50), + Color::Rgb(20, 11, 52), + Color::Rgb(21, 11, 55), + Color::Rgb(22, 11, 57), + Color::Rgb(24, 12, 60), + Color::Rgb(25, 12, 62), + Color::Rgb(27, 12, 65), + Color::Rgb(28, 12, 67), + Color::Rgb(30, 12, 69), + Color::Rgb(31, 12, 72), + Color::Rgb(33, 12, 74), + Color::Rgb(35, 12, 76), + Color::Rgb(36, 12, 79), + Color::Rgb(38, 12, 81), + Color::Rgb(40, 11, 83), + Color::Rgb(41, 11, 85), + Color::Rgb(43, 11, 87), + Color::Rgb(45, 11, 89), + Color::Rgb(47, 10, 91), + Color::Rgb(49, 10, 92), + Color::Rgb(50, 10, 94), + Color::Rgb(52, 10, 95), + Color::Rgb(54, 9, 97), + Color::Rgb(56, 9, 98), + Color::Rgb(57, 9, 99), + Color::Rgb(59, 9, 100), + Color::Rgb(61, 9, 101), + Color::Rgb(62, 9, 102), + Color::Rgb(64, 10, 103), + Color::Rgb(66, 10, 104), + Color::Rgb(68, 10, 104), + Color::Rgb(69, 10, 105), + Color::Rgb(71, 11, 106), + Color::Rgb(73, 11, 106), + Color::Rgb(74, 12, 107), + Color::Rgb(76, 12, 107), + Color::Rgb(77, 13, 108), + Color::Rgb(79, 13, 108), + Color::Rgb(81, 14, 108), + Color::Rgb(82, 14, 109), + Color::Rgb(84, 15, 109), + Color::Rgb(85, 15, 109), + Color::Rgb(87, 16, 110), + Color::Rgb(89, 16, 110), + Color::Rgb(90, 17, 110), + Color::Rgb(92, 18, 110), + Color::Rgb(93, 18, 110), + Color::Rgb(95, 19, 110), + Color::Rgb(97, 19, 110), + Color::Rgb(98, 20, 110), + Color::Rgb(100, 21, 110), + Color::Rgb(101, 21, 110), + Color::Rgb(103, 22, 110), + Color::Rgb(105, 22, 110), + Color::Rgb(106, 23, 110), + Color::Rgb(108, 24, 110), + Color::Rgb(109, 24, 110), + Color::Rgb(111, 25, 110), + Color::Rgb(113, 25, 110), + Color::Rgb(114, 26, 110), + Color::Rgb(116, 26, 110), + Color::Rgb(117, 27, 110), + Color::Rgb(119, 28, 109), + Color::Rgb(120, 28, 109), + Color::Rgb(122, 29, 109), + Color::Rgb(124, 29, 109), + Color::Rgb(125, 30, 109), + Color::Rgb(127, 30, 108), + Color::Rgb(128, 31, 108), + Color::Rgb(130, 32, 108), + Color::Rgb(132, 32, 107), + Color::Rgb(133, 33, 107), + Color::Rgb(135, 33, 107), + Color::Rgb(136, 34, 106), + Color::Rgb(138, 34, 106), + Color::Rgb(140, 35, 105), + Color::Rgb(141, 35, 105), + Color::Rgb(143, 36, 105), + Color::Rgb(144, 37, 104), + Color::Rgb(146, 37, 104), + Color::Rgb(147, 38, 103), + Color::Rgb(149, 38, 103), + Color::Rgb(151, 39, 102), + Color::Rgb(152, 39, 102), + Color::Rgb(154, 40, 101), + Color::Rgb(155, 41, 100), + Color::Rgb(157, 41, 100), + Color::Rgb(159, 42, 99), + Color::Rgb(160, 42, 99), + Color::Rgb(162, 43, 98), + Color::Rgb(163, 44, 97), + Color::Rgb(165, 44, 96), + Color::Rgb(166, 45, 96), + Color::Rgb(168, 46, 95), + Color::Rgb(169, 46, 94), + Color::Rgb(171, 47, 94), + Color::Rgb(173, 48, 93), + Color::Rgb(174, 48, 92), + Color::Rgb(176, 49, 91), + Color::Rgb(177, 50, 90), + Color::Rgb(179, 50, 90), + Color::Rgb(180, 51, 89), + Color::Rgb(182, 52, 88), + Color::Rgb(183, 53, 87), + Color::Rgb(185, 53, 86), + Color::Rgb(186, 54, 85), + Color::Rgb(188, 55, 84), + Color::Rgb(189, 56, 83), + Color::Rgb(191, 57, 82), + Color::Rgb(192, 58, 81), + Color::Rgb(193, 58, 80), + Color::Rgb(195, 59, 79), + Color::Rgb(196, 60, 78), + Color::Rgb(198, 61, 77), + Color::Rgb(199, 62, 76), + Color::Rgb(200, 63, 75), + Color::Rgb(202, 64, 74), + Color::Rgb(203, 65, 73), + Color::Rgb(204, 66, 72), + Color::Rgb(206, 67, 71), + Color::Rgb(207, 68, 70), + Color::Rgb(208, 69, 69), + Color::Rgb(210, 70, 68), + Color::Rgb(211, 71, 67), + Color::Rgb(212, 72, 66), + Color::Rgb(213, 74, 65), + Color::Rgb(215, 75, 63), + Color::Rgb(216, 76, 62), + Color::Rgb(217, 77, 61), + Color::Rgb(218, 78, 60), + Color::Rgb(219, 80, 59), + Color::Rgb(221, 81, 58), + Color::Rgb(222, 82, 56), + Color::Rgb(223, 83, 55), + Color::Rgb(224, 85, 54), + Color::Rgb(225, 86, 53), + Color::Rgb(226, 87, 52), + Color::Rgb(227, 89, 51), + Color::Rgb(228, 90, 49), + Color::Rgb(229, 92, 48), + Color::Rgb(230, 93, 47), + Color::Rgb(231, 94, 46), + Color::Rgb(232, 96, 45), + Color::Rgb(233, 97, 43), + Color::Rgb(234, 99, 42), + Color::Rgb(235, 100, 41), + Color::Rgb(235, 102, 40), + Color::Rgb(236, 103, 38), + Color::Rgb(237, 105, 37), + Color::Rgb(238, 106, 36), + Color::Rgb(239, 108, 35), + Color::Rgb(239, 110, 33), + Color::Rgb(240, 111, 32), + Color::Rgb(241, 113, 31), + Color::Rgb(241, 115, 29), + Color::Rgb(242, 116, 28), + Color::Rgb(243, 118, 27), + Color::Rgb(243, 120, 25), + Color::Rgb(244, 121, 24), + Color::Rgb(245, 123, 23), + Color::Rgb(245, 125, 21), + Color::Rgb(246, 126, 20), + Color::Rgb(246, 128, 19), + Color::Rgb(247, 130, 18), + Color::Rgb(247, 132, 16), + Color::Rgb(248, 133, 15), + Color::Rgb(248, 135, 14), + Color::Rgb(248, 137, 12), + Color::Rgb(249, 139, 11), + Color::Rgb(249, 140, 10), + Color::Rgb(249, 142, 9), + Color::Rgb(250, 144, 8), + Color::Rgb(250, 146, 7), + Color::Rgb(250, 148, 7), + Color::Rgb(251, 150, 6), + Color::Rgb(251, 151, 6), + Color::Rgb(251, 153, 6), + Color::Rgb(251, 155, 6), + Color::Rgb(251, 157, 7), + Color::Rgb(252, 159, 7), + Color::Rgb(252, 161, 8), + Color::Rgb(252, 163, 9), + Color::Rgb(252, 165, 10), + Color::Rgb(252, 166, 12), + Color::Rgb(252, 168, 13), + Color::Rgb(252, 170, 15), + Color::Rgb(252, 172, 17), + Color::Rgb(252, 174, 18), + Color::Rgb(252, 176, 20), + Color::Rgb(252, 178, 22), + Color::Rgb(252, 180, 24), + Color::Rgb(251, 182, 26), + Color::Rgb(251, 184, 29), + Color::Rgb(251, 186, 31), + Color::Rgb(251, 188, 33), + Color::Rgb(251, 190, 35), + Color::Rgb(250, 192, 38), + Color::Rgb(250, 194, 40), + Color::Rgb(250, 196, 42), + Color::Rgb(250, 198, 45), + Color::Rgb(249, 199, 47), + Color::Rgb(249, 201, 50), + Color::Rgb(249, 203, 53), + Color::Rgb(248, 205, 55), + Color::Rgb(248, 207, 58), + Color::Rgb(247, 209, 61), + Color::Rgb(247, 211, 64), + Color::Rgb(246, 213, 67), + Color::Rgb(246, 215, 70), + Color::Rgb(245, 217, 73), + Color::Rgb(245, 219, 76), + Color::Rgb(244, 221, 79), + Color::Rgb(244, 223, 83), + Color::Rgb(244, 225, 86), + Color::Rgb(243, 227, 90), + Color::Rgb(243, 229, 93), + Color::Rgb(242, 230, 97), + Color::Rgb(242, 232, 101), + Color::Rgb(242, 234, 105), + Color::Rgb(241, 236, 109), + Color::Rgb(241, 237, 113), + Color::Rgb(241, 239, 117), + Color::Rgb(241, 241, 121), + Color::Rgb(242, 242, 125), + Color::Rgb(242, 244, 130), + Color::Rgb(243, 245, 134), + Color::Rgb(243, 246, 138), + Color::Rgb(244, 248, 142), + Color::Rgb(245, 249, 146), + Color::Rgb(246, 250, 150), + Color::Rgb(248, 251, 154), + Color::Rgb(249, 252, 157), + Color::Rgb(250, 253, 161), + Color::Rgb(252, 255, 164), +]); + +impl ColorMap { + pub fn apply(&self, val: f64) -> Color { + assert!(val >= 0.0 && val <= 1.0); + let position = (val * 255.0).ceil() as usize; + self.0[position] + } +} diff --git a/2023/17/src/main.rs b/2023/17/src/main.rs new file mode 100644 index 0000000..4a164ab --- /dev/null +++ b/2023/17/src/main.rs @@ -0,0 +1,481 @@ +use colormap::ColorMap; +use std::collections::hash_map::RandomState; +use std::collections::{BinaryHeap, HashMap}; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines, Write}; +use std::iter::repeat; +use std::time::Instant; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; + +mod colormap; + +const COLORMAP: &ColorMap = &colormap::COLORMAP_INFERNO; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// DATA + +const UNPATH_CHAR: char = '█'; +const UNVISITED_CHAR: char = ' '; + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +enum Direction { + Left, + Right, + Up, + Down, +} + +impl Direction { + const fn all() -> &'static [Self; 4] { + &[ + Direction::Left, + Direction::Right, + Direction::Up, + Direction::Down, + ] + } + const fn opposite(&self) -> Self { + match self { + Direction::Left => Direction::Right, + Direction::Right => Direction::Left, + Direction::Up => Direction::Down, + Direction::Down => Direction::Up, + } + } +} + +impl From<&Direction> for char { + fn from(dir: &Direction) -> Self { + match dir { + Direction::Left => '←', + Direction::Right => '→', + Direction::Up => '↑', + Direction::Down => '↓', + } + } +} + +struct CityMap { + map: Vec>, +} + +impl CityMap { + fn offset_pos(&self, pos: (usize, usize), dir: &Direction) -> Option<(usize, usize)> { + match dir { + Direction::Left if pos.0 > 0 => Some((pos.0 - 1, pos.1)), + Direction::Right if pos.0 < self.map[0].len() - 1 => Some((pos.0 + 1, pos.1)), + Direction::Up if pos.1 > 0 => Some((pos.0, pos.1 - 1)), + Direction::Down if pos.1 < self.map.len() - 1 => Some((pos.0, pos.1 + 1)), + _ => None, + } + } + #[allow(dead_code)] + fn print(&self) -> Result<(), Box> { + let cost_max = *self.map.iter().flat_map(|row| row.iter()).max().unwrap() as f64; + + let mut so_lock = StandardStream::stdout(ColorChoice::Always); + for y in 0..self.map.len() { + for val in &self.map[y] { + so_lock.set_color( + ColorSpec::new() + .set_bg(Some(COLORMAP.apply(*val as f64 / cost_max))) + .set_fg(Some(Color::Black)), + )?; + so_lock.write_fmt(format_args!("{}", val))?; + } + so_lock.reset()?; + writeln!(so_lock)?; + } + Ok(()) + } +} + +type Position = (usize, usize); + +struct WalkCost<'a> { + start: Position, + cost_from: Vec>>, + map: &'a CityMap, +} + +#[derive(Debug)] +struct Move { + new_pos: Position, + dir: &'static Direction, + consecutive: usize, + weight: u64, +} + +impl PartialEq for Move { + fn eq(&self, other: &Self) -> bool { + self.weight == other.weight + } +} + +impl Eq for Move {} + +impl PartialOrd for Move { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Move { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + std::cmp::Reverse(self.weight).cmp(&std::cmp::Reverse(other.weight)) + } +} + +impl<'a> WalkCost<'a> { + fn from_map(map: &'a CityMap, start: Position) -> Self { + Self { + map, + start, + cost_from: map + .map + .iter() + .map(|row| repeat(HashMap::new()).take(row.len()).collect()) + .collect(), + } + } + fn compute(&mut self) { + let mut unvisited_next_moves: BinaryHeap = BinaryHeap::new(); + let valid_start_moves: Vec = Direction::all() + .iter() + .filter_map(|dir| { + self.map.offset_pos(self.start, dir).and_then(|pos| { + Some(Move { + new_pos: pos, + dir, + consecutive: 1, + weight: self.map.map[pos.1][pos.0], + }) + }) + }) // valid positions + .collect(); + + for m in valid_start_moves { + unvisited_next_moves.push(m); + } + + while let Some(cur_move) = unvisited_next_moves.pop() { + // we've been here already at a lower cost + // if cur_move.weight >= self.cost_to[cur_move.new_pos.1][cur_move.new_pos.0] { + // continue; + // } + if let Some(last_weight) = self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .get(&(*cur_move.dir, cur_move.consecutive)) + { + if cur_move.weight < *last_weight { + self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .insert((*cur_move.dir, cur_move.consecutive), cur_move.weight); + } else { + continue; + } // visited before at lower cost } + } else { + self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .insert((*cur_move.dir, cur_move.consecutive), cur_move.weight); + } + // println!("state at {:?}: {:?}", cur_move, self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0]); + + let valid_moves = Direction::all().iter().filter_map(|dir| { + self.map + .offset_pos(cur_move.new_pos, dir) + .and_then(|new_pos| { + Some(Move { + new_pos, + dir, + consecutive: if cur_move.dir == dir { + cur_move.consecutive + 1 + } else { + 1 + }, + weight: cur_move.weight + self.map.map[new_pos.1][new_pos.0], + }) + }) + .filter(|m| m.consecutive != 4 && *m.dir != cur_move.dir.opposite()) + .filter(|m| { + m.weight + < *self.cost_from[m.new_pos.1][m.new_pos.0] + .get(&(*m.dir, m.consecutive)) + .unwrap_or(&u64::MAX) + }) + }); // valid positions + // println!("valid moves with {:?}", cur_move); + for next_move in valid_moves { + // println!(" {:?}", next_move); + unvisited_next_moves.push(next_move); + } + } + } +} + +struct WalkCost2<'a> { + start: Position, + cost_from: Vec>>, + map: &'a CityMap, +} + +impl<'a> WalkCost2<'a> { + fn from_map(map: &'a CityMap, start: Position) -> Self { + Self { + map, + start, + cost_from: map + .map + .iter() + .map(|row| repeat(HashMap::new()).take(row.len()).collect()) + .collect(), + } + } + fn min_cost_at(&self, pos: Position) -> Option<&u64> { + self.cost_from[pos.1][pos.0].values().min() + } + fn compute(&mut self, to: Position) { + let mut unvisited_next_moves: BinaryHeap = BinaryHeap::new(); + let valid_start_moves: Vec = Direction::all() + .iter() + .filter_map(|dir| { + self.map.offset_pos(self.start, dir).and_then(|pos| { + Some(Move { + new_pos: pos, + dir, + consecutive: 1, + weight: self.map.map[pos.1][pos.0], + }) + }) + }) // valid positions + .collect(); + + for m in valid_start_moves { + unvisited_next_moves.push(m); + } + + while let Some(cur_move) = unvisited_next_moves.pop() { + // we've been here already at a lower cost + // if cur_move.weight >= self.cost_to[cur_move.new_pos.1][cur_move.new_pos.0] { + // continue; + // } + if let Some(last_weight) = self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .get(&(*cur_move.dir, cur_move.consecutive)) + { + if cur_move.weight < *last_weight { + self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .insert((*cur_move.dir, cur_move.consecutive), cur_move.weight); + // println!("move {:?} inserted {:?}", cur_move, (*cur_move.dir, cur_move.consecutive)); + } else { + continue; + } // visited before at lower cost } + } else { + // println!("move {:?} inserted {:?}", cur_move, (*cur_move.dir, cur_move.consecutive)); + self.cost_from[cur_move.new_pos.1][cur_move.new_pos.0] + .insert((*cur_move.dir, cur_move.consecutive), cur_move.weight); + } + + if cur_move.new_pos == to { + // println!("reached end pos {:?} via {:?}", to, cur_move); + continue; + } + let valid_moves = Direction::all().iter().filter_map(|dir| { + self.map + .offset_pos(cur_move.new_pos, dir) + .and_then(|new_pos| { + Some(Move { + new_pos, + dir, + consecutive: if cur_move.dir == dir { + cur_move.consecutive + 1 + } else { + 1 + }, + weight: cur_move.weight + self.map.map[new_pos.1][new_pos.0], + }) + }) + .filter(|m| m.new_pos != self.start) + .filter(|m| *m.dir != cur_move.dir.opposite()) + .filter(|m| { + if m.dir == cur_move.dir { + m.consecutive < 11 + } else { + cur_move.consecutive >= 4 + } + }) + .filter(|m| m.new_pos != to || m.consecutive >= 4) + .filter(|m| { + m.weight + < *self.cost_from[m.new_pos.1][m.new_pos.0] + .get(&(*m.dir, m.consecutive)) + .unwrap_or(&u64::MAX) + }) + }); // valid positions + // println!("valid moves with {:?}", cur_move); + for next_move in valid_moves { + // println!(" {:?}", next_move); + unvisited_next_moves.push(next_move); + } + } + } + + fn shortest_path_to(&self, to: Position) -> Vec<(Position, Direction)> { + let mut path = Vec::new(); + let mut cur_pos = to; + // start at the end, walk backwards + while cur_pos != self.start { + let (m, _val) = self.cost_from[cur_pos.1][cur_pos.0] + .iter() + .min_by(|a, b| a.1.cmp(b.1)) + .unwrap(); + path.push((cur_pos, m.0)); + cur_pos = self.map.offset_pos(cur_pos, &m.0.opposite()).unwrap(); + } + path + } + fn print_path(&self, to: Position) -> Result<(), Box> { + let path = self.shortest_path_to(to); + let map: HashMap<_, _, RandomState> = HashMap::from_iter(path.into_iter()); + let cost_max_of_min = *self + .cost_from + .iter() + .flat_map(|row| row.iter().filter_map(|cell| cell.values().min())) + .max() + .unwrap() as f64; + let mut so_lock = StandardStream::stdout(ColorChoice::Always); + + for y in 0..self.cost_from.len() { + for x in 0..self.map.map[y].len() { + let mut color = ColorSpec::new(); + let c = if let Some(to_dir) = map.get(&(x, y)) { + let normalized_cost = + *self.min_cost_at((x, y)).unwrap() as f64 / cost_max_of_min; + let bg_color = COLORMAP.apply(normalized_cost); + let fg_color = if let Color::Rgb(r, g, b) = bg_color { + Color::Rgb(255 - r, 255 - g, 255 - b) + } else { + Color::Black + }; + color.set_fg(Some(fg_color)).set_bg(Some(bg_color)).bold(); + to_dir.into() + } else { + if let Some(cost) = &self.min_cost_at((x, y)) { + color.set_fg(Some(COLORMAP.apply(**cost as f64 / cost_max_of_min))); + UNPATH_CHAR + } else { + color.set_fg(Some(Color::Black)); + UNVISITED_CHAR + } + }; + so_lock.set_color(&color)?; + let mut char_buf = [0u8; 4]; + c.encode_utf8(&mut char_buf); + so_lock.write_all(&char_buf)?; + } + so_lock.reset()?; + writeln!(so_lock)?; + } + Ok(()) + } +} + +impl From> for CityMap { + fn from(lines: Lines) -> Self { + Self { + map: lines + .map(|l| l.unwrap().chars().map(|c| c as u64 - '0' as u64).collect()) + .collect(), + } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let map = CityMap::from(input); + let mut costs = WalkCost::from_map(&map, (0, 0)); + costs.compute(); + + // println!("{}", costs); + // costs.print_shortest_path((costs.cost_to[0].len() - 1, costs.cost_to.len() - 1)); + + *costs.cost_from[costs.cost_from.len() - 1][costs.cost_from[0].len() - 1] + .values() + .min() + .unwrap() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let map = CityMap::from(input); + // map.print().unwrap(); + let mut costs = WalkCost2::from_map(&map, (0, 0)); + costs.compute((map.map[0].len() - 1, map.map.len() - 1)); + + // println!("{}", costs); + costs + .print_path((costs.cost_from[0].len() - 1, costs.cost_from.len() - 1)) + .unwrap(); + + *costs.cost_from[costs.cost_from.len() - 1][costs.cost_from[0].len() - 1] + .values() + .min() + .unwrap() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + use test_case::test_case; + + const EXAMPLE: &str = &"2413432311323 +3215453535623 +3255245654254 +3446585845452 +4546657867536 +1438598798454 +4457876987766 +3637877979653 +4654967986887 +4564679986453 +1224686865563 +2546548887735 +4322674655533"; + + const EXAMPLE2: &str = &"111111111111 +999999999991 +999999999991 +999999999991 +999999999991"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 102); + } + + #[test_case(EXAMPLE, 94)] + #[test_case(EXAMPLE2, 71)] + fn problem2_example(example: &str, expect: u64) { + let c = Cursor::new(example); + assert_eq!(problem2(c.lines()), expect); + } +} diff --git a/2023/18/Cargo.lock b/2023/18/Cargo.lock new file mode 100644 index 0000000..4c9e552 --- /dev/null +++ b/2023/18/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day18" +version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] diff --git a/2023/18/Cargo.toml b/2023/18/Cargo.toml new file mode 100644 index 0000000..4d457a8 --- /dev/null +++ b/2023/18/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day18" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" diff --git a/2023/18/input b/2023/18/input new file mode 100644 index 0000000..f27e001 --- /dev/null +++ b/2023/18/input @@ -0,0 +1,640 @@ +L 4 (#248a72) +U 3 (#295c13) +L 4 (#49e802) +U 5 (#5ff8b3) +R 3 (#0df642) +U 2 (#15b973) +R 5 (#6cfb92) +U 11 (#4e3cd3) +L 4 (#422dd0) +D 5 (#303fe3) +L 7 (#6225e0) +D 2 (#58bd63) +L 9 (#6225e2) +D 8 (#2b1703) +L 6 (#422dd2) +D 9 (#2eb9b3) +L 2 (#10f270) +D 10 (#0677b3) +L 4 (#190450) +U 11 (#55b4e3) +L 4 (#361732) +U 3 (#034663) +L 4 (#37f902) +U 6 (#19bb73) +R 8 (#6e1030) +U 8 (#4cad93) +L 4 (#2509d0) +U 6 (#1e1e61) +L 5 (#3d3b90) +U 8 (#28ec81) +L 2 (#3f9200) +U 5 (#437871) +L 6 (#174bc0) +D 5 (#3b58a1) +L 11 (#064a60) +U 5 (#539da3) +L 4 (#1ea0d0) +D 7 (#1909b3) +L 2 (#296a52) +D 9 (#6780c3) +L 3 (#296a50) +U 11 (#356893) +L 2 (#0eb430) +U 5 (#019203) +L 5 (#4eea10) +U 4 (#436533) +L 4 (#20aeb0) +U 4 (#2fb883) +L 6 (#5046c0) +U 11 (#392293) +R 8 (#2567a0) +U 3 (#57ee03) +R 12 (#270540) +U 2 (#358173) +R 6 (#289e40) +U 2 (#77b991) +R 4 (#016c10) +U 11 (#351091) +R 2 (#714f90) +U 6 (#4add71) +L 11 (#00eab0) +D 5 (#2f8361) +L 8 (#192200) +U 5 (#1416c1) +L 13 (#775d30) +U 3 (#3d3d63) +L 8 (#6e4710) +U 3 (#28cfd3) +L 8 (#389880) +U 5 (#28cfd1) +L 7 (#4cd750) +D 5 (#1d56e3) +L 3 (#2789e2) +D 5 (#5d7573) +R 9 (#1b1372) +D 5 (#1965a3) +R 4 (#111052) +D 2 (#5a2861) +R 10 (#37f1c2) +D 3 (#0b7ad1) +R 5 (#012162) +U 5 (#226f43) +R 4 (#583cd2) +D 5 (#4333f3) +R 5 (#0eb952) +D 3 (#22a273) +L 2 (#34c5d0) +D 3 (#355f11) +L 5 (#13a460) +D 4 (#355f13) +L 6 (#3e2900) +U 4 (#28d423) +L 8 (#042432) +D 3 (#4378f3) +L 6 (#18a200) +D 5 (#16a843) +L 5 (#18a202) +D 7 (#46fdd3) +L 5 (#2a4802) +D 5 (#2a4d23) +L 5 (#6049d2) +D 4 (#0a4023) +L 4 (#09afb2) +U 4 (#687713) +L 5 (#4b85f2) +D 4 (#0e1a53) +L 8 (#331b60) +U 6 (#270d83) +L 6 (#331b62) +U 6 (#25dba3) +L 6 (#412e82) +D 6 (#5e2e73) +L 8 (#1de802) +U 6 (#1e7143) +L 4 (#4e7e82) +U 3 (#160d63) +L 5 (#19d230) +U 3 (#36a4a3) +R 3 (#19d232) +U 6 (#4c63b3) +R 10 (#0d2862) +U 5 (#0ec351) +R 11 (#593c62) +U 6 (#12de21) +R 2 (#08d482) +U 4 (#277081) +R 3 (#10eee0) +U 4 (#382b91) +L 4 (#512200) +U 7 (#1279f1) +L 3 (#229312) +U 6 (#422721) +R 6 (#71ddd2) +U 6 (#093871) +R 13 (#0d5622) +D 5 (#093873) +R 7 (#4c34e2) +U 6 (#5b1743) +R 2 (#4779a2) +D 6 (#1cc6e3) +R 7 (#3c13a0) +U 5 (#3b60f3) +R 4 (#3c13a2) +U 9 (#229f83) +R 3 (#0a6ff2) +D 2 (#06d231) +R 8 (#0298b2) +D 11 (#4eb6a1) +R 7 (#0298b0) +U 8 (#438ce1) +R 7 (#0e69e2) +U 5 (#2d3e73) +R 8 (#40b2c2) +U 6 (#64cb53) +L 2 (#40b2c0) +U 3 (#0215c3) +L 3 (#2cbf42) +U 4 (#00c523) +L 7 (#1fc3d2) +D 4 (#68a633) +L 5 (#1fc3d0) +U 5 (#149853) +L 5 (#2cbf40) +U 9 (#0b9613) +L 3 (#1e7f00) +U 8 (#2fc533) +L 7 (#3d7180) +U 3 (#33f3a3) +R 9 (#66d340) +U 2 (#0fa1f1) +R 4 (#0546c0) +D 6 (#4c00b3) +R 8 (#483b50) +U 6 (#4c00b1) +R 4 (#2fc7c0) +U 2 (#0fa1f3) +R 7 (#377280) +U 6 (#200c43) +R 5 (#429ef0) +U 3 (#3a90f1) +R 5 (#226142) +U 4 (#2740c1) +R 5 (#41ac62) +U 3 (#275423) +R 2 (#3ba072) +U 11 (#275421) +R 4 (#27ea92) +D 8 (#402001) +R 2 (#3456c0) +D 6 (#5a4541) +R 3 (#4d1400) +U 4 (#5a4543) +R 6 (#462de0) +U 8 (#3fcdb1) +R 6 (#1f2d90) +U 6 (#4e7851) +R 3 (#41eb30) +D 6 (#4e7853) +R 4 (#1aa1c0) +D 10 (#0a4073) +R 6 (#2134b2) +D 7 (#164e43) +R 4 (#200c72) +D 5 (#3beca3) +R 6 (#7445e0) +U 3 (#2cda83) +R 4 (#7445e2) +U 3 (#475553) +L 5 (#200c70) +U 10 (#23fb33) +R 5 (#2134b0) +U 7 (#3c98d3) +L 4 (#4cfc80) +U 5 (#0f0e71) +R 8 (#3c1d22) +U 10 (#23a311) +R 6 (#05da32) +D 5 (#6229c1) +R 5 (#41f750) +D 8 (#1f4191) +R 3 (#3530f0) +D 13 (#4d3941) +R 3 (#6215e0) +D 9 (#2fe911) +R 6 (#05cf90) +U 4 (#25c901) +R 2 (#335232) +U 12 (#271371) +R 2 (#335230) +U 5 (#2bd1d1) +L 9 (#1ffad0) +U 6 (#6f6bc1) +R 9 (#099600) +U 5 (#13bee1) +R 3 (#5aa820) +U 3 (#3546c1) +R 6 (#4dd5f0) +D 11 (#495be1) +R 6 (#60c8e0) +U 11 (#4bd201) +R 4 (#367f30) +D 4 (#43dbc1) +R 6 (#4bb740) +U 12 (#07ce11) +R 6 (#4b4d70) +D 12 (#3e29e1) +R 5 (#313fc0) +U 4 (#293d51) +R 4 (#1b4860) +U 11 (#514571) +R 4 (#5c11a2) +U 4 (#444841) +R 8 (#5c11a0) +U 2 (#3e6c51) +R 11 (#16dab0) +U 7 (#26baf1) +R 2 (#32e240) +U 5 (#26baf3) +R 6 (#2f0a70) +U 6 (#11a821) +R 3 (#527fc0) +U 7 (#468b31) +R 6 (#0641a0) +U 10 (#20f351) +R 7 (#547a90) +U 12 (#1dbf11) +R 2 (#17d0d2) +U 10 (#159981) +R 6 (#634e12) +U 5 (#4ff021) +R 10 (#006122) +D 6 (#6589a3) +R 3 (#1a74f2) +D 2 (#19b911) +R 12 (#3ed5f2) +D 5 (#28ab31) +R 3 (#09dd82) +D 5 (#614c01) +R 6 (#284d32) +D 9 (#06b521) +R 8 (#4dfad2) +D 7 (#5b0f61) +R 2 (#0fff22) +D 5 (#1e6ff1) +R 2 (#47b352) +D 9 (#29c291) +R 5 (#352ef2) +D 3 (#042d11) +R 4 (#262542) +D 7 (#042d13) +R 4 (#21e8f2) +D 3 (#18cc21) +R 3 (#16ed70) +U 9 (#270ea1) +R 7 (#4391e0) +U 9 (#270ea3) +R 4 (#22bdd0) +D 3 (#3b72b1) +R 7 (#1fd4b2) +D 8 (#01bfe1) +L 7 (#3d18a2) +D 7 (#20dac1) +R 4 (#67d990) +D 4 (#1acd93) +L 7 (#017410) +D 2 (#6e4923) +L 7 (#10bdd0) +D 13 (#0650b3) +L 2 (#072660) +U 13 (#221151) +L 6 (#41d720) +D 3 (#5bc831) +L 4 (#252450) +D 9 (#118de1) +R 6 (#05b1d0) +D 4 (#247721) +R 9 (#2b6b10) +U 4 (#60da41) +R 10 (#470c42) +D 3 (#4fdc91) +R 2 (#470c40) +D 8 (#189db1) +L 5 (#4a1f70) +D 7 (#07e141) +L 3 (#2524b0) +D 5 (#2a6ce1) +L 11 (#1a2e50) +D 2 (#4f08c1) +L 3 (#3b7ec0) +D 9 (#65e011) +L 8 (#24b9a0) +D 6 (#196c61) +L 5 (#540b20) +D 6 (#243801) +L 2 (#397ce0) +D 3 (#411a41) +L 12 (#397ce2) +D 2 (#526d51) +L 2 (#5b5810) +D 5 (#2e96d3) +L 8 (#6c4f80) +U 6 (#274453) +L 3 (#077010) +U 5 (#37f603) +R 5 (#73bf92) +U 4 (#29ee73) +R 11 (#5ffd40) +U 5 (#3e12d3) +L 4 (#4b4e32) +U 12 (#114e01) +L 2 (#45d0b2) +U 4 (#114e03) +L 6 (#4b8b02) +U 9 (#3eff03) +L 4 (#6968d0) +U 4 (#3d4d33) +L 6 (#1dd0e0) +D 2 (#436023) +L 9 (#58b812) +D 6 (#423003) +L 7 (#2e81a2) +D 3 (#3d9e13) +L 11 (#305450) +D 6 (#2667f3) +L 7 (#3a8470) +D 8 (#293191) +R 3 (#207ee0) +D 4 (#293193) +R 3 (#2c4360) +D 4 (#2667f1) +R 7 (#250ee0) +U 4 (#70e1c3) +R 4 (#2907d0) +D 6 (#0b69a1) +R 6 (#3babc0) +D 6 (#7698c3) +R 2 (#2752c0) +D 3 (#7698c1) +L 4 (#2dc550) +D 11 (#4ec121) +L 8 (#199d00) +U 11 (#33f261) +L 7 (#3b4e30) +D 5 (#24e991) +L 6 (#0aaeb2) +U 9 (#15a993) +L 5 (#59ed02) +U 2 (#15a991) +L 5 (#2c0432) +U 10 (#337e81) +L 7 (#248670) +D 2 (#511d31) +L 3 (#52ebc0) +D 13 (#511d33) +L 4 (#192db0) +D 7 (#3dcf11) +L 2 (#285030) +D 3 (#15acc1) +L 6 (#3a6c70) +D 5 (#36f281) +L 4 (#44fd02) +U 5 (#4e8821) +L 4 (#473972) +U 5 (#2ab9f1) +L 8 (#60aaa0) +U 5 (#35c651) +L 3 (#4736b2) +U 10 (#4741d1) +L 3 (#1973f2) +U 5 (#1b5031) +L 3 (#3c6532) +D 5 (#49bd31) +L 4 (#5e3f60) +D 3 (#4a2011) +L 4 (#2ba190) +D 6 (#4a2013) +L 3 (#653430) +D 7 (#10dda1) +L 11 (#2532d2) +D 3 (#675311) +L 4 (#27d642) +D 6 (#02cae1) +L 10 (#466652) +D 7 (#34cef3) +R 3 (#263692) +D 7 (#34cef1) +R 8 (#2c59d2) +D 5 (#31afe1) +L 8 (#091562) +D 5 (#12c861) +R 4 (#47a772) +D 5 (#68a5b1) +L 7 (#1f79a2) +D 4 (#3b3c13) +R 10 (#0e7122) +D 4 (#087363) +R 4 (#3e26c2) +U 13 (#4953f3) +R 3 (#5ac862) +U 3 (#4953f1) +R 3 (#13cfe2) +D 5 (#087361) +R 11 (#57f7a2) +D 4 (#3b3c11) +L 11 (#6dcb32) +D 7 (#35e653) +R 6 (#525282) +D 5 (#3dac93) +R 9 (#377082) +D 6 (#2afda1) +R 10 (#3e6422) +D 10 (#220471) +R 10 (#37ebf2) +D 2 (#4d0213) +R 5 (#2fa2f2) +D 4 (#30df53) +R 5 (#617e00) +U 6 (#536393) +R 5 (#496510) +D 6 (#5f6733) +R 4 (#3bf9b0) +D 3 (#5f6731) +R 4 (#48d940) +D 3 (#35a5d3) +R 4 (#284a82) +D 9 (#445b23) +L 4 (#2535c2) +D 4 (#3f8b43) +R 2 (#41a002) +D 7 (#20b143) +R 3 (#63ae42) +U 8 (#328b03) +R 9 (#383210) +U 3 (#1ce143) +R 7 (#383212) +U 6 (#2ed603) +R 5 (#00c512) +U 6 (#6dea43) +R 6 (#00c510) +U 7 (#0f0643) +R 3 (#4292a2) +U 7 (#684961) +R 3 (#03de80) +U 5 (#46e0b1) +R 6 (#3d4ae2) +D 5 (#1d5fc1) +R 7 (#3d4ae0) +U 13 (#470ab1) +R 5 (#03de82) +U 4 (#282c71) +R 6 (#5520f2) +D 5 (#2f0981) +R 2 (#5520f0) +D 12 (#4081a1) +R 6 (#22a272) +U 5 (#284c53) +R 8 (#081202) +D 5 (#0b7831) +R 4 (#3e4552) +D 13 (#0b7833) +R 5 (#38e092) +U 11 (#284c51) +R 2 (#550082) +U 2 (#175033) +R 6 (#036dc2) +D 3 (#068791) +R 9 (#3ff6f2) +D 5 (#4a5783) +R 13 (#4d5f92) +D 6 (#3e3423) +L 12 (#4f62d2) +D 7 (#5e4363) +L 3 (#0f7032) +U 7 (#285261) +L 12 (#4ea392) +D 5 (#4c2dc1) +L 4 (#0d2822) +D 5 (#4c2dc3) +R 9 (#518da2) +D 2 (#41b3b1) +R 7 (#023270) +D 3 (#13d6e1) +R 11 (#0874c0) +D 3 (#2026c1) +R 4 (#68f3a0) +D 7 (#2026c3) +L 8 (#208470) +D 4 (#09bd81) +L 3 (#193a10) +D 7 (#5f3491) +L 4 (#08cad2) +D 10 (#068793) +L 10 (#48acb2) +D 4 (#20e341) +L 7 (#6137a2) +D 8 (#20e343) +L 7 (#0bf292) +D 7 (#5b5f13) +L 8 (#2b2532) +D 3 (#034073) +L 5 (#3c7bb2) +D 11 (#09c643) +L 3 (#0d6e52) +U 3 (#569d23) +L 5 (#63fed0) +U 4 (#162193) +L 4 (#2eb310) +U 3 (#655e23) +L 10 (#42f8e0) +U 4 (#341983) +R 14 (#3dbbc0) +U 5 (#551b13) +L 5 (#2a94f0) +U 6 (#05ae53) +L 2 (#09db90) +U 5 (#2c9e13) +L 8 (#4c6d10) +D 10 (#40ffb3) +L 3 (#306c30) +D 2 (#338911) +L 6 (#4bed00) +D 7 (#338913) +R 7 (#12aae0) +D 4 (#459613) +L 7 (#75d3a2) +D 7 (#37ad61) +L 6 (#6dcf92) +D 4 (#3b7703) +R 9 (#0ee2b2) +D 4 (#514c13) +R 6 (#0ee2b0) +D 3 (#2863b3) +L 8 (#52eee2) +D 8 (#2fc771) +L 7 (#5be782) +D 4 (#2f39a1) +L 6 (#5be780) +U 4 (#5625b1) +L 2 (#5588c2) +U 14 (#37ad63) +L 3 (#0681e2) +U 4 (#085481) +L 3 (#037432) +U 12 (#31ed51) +L 4 (#6791e2) +D 5 (#3546d3) +L 6 (#4419e2) +D 10 (#3546d1) +L 4 (#2c1a72) +D 10 (#5bead1) +L 3 (#3b3252) +D 9 (#3390c1) +L 3 (#1d78c2) +D 5 (#488d71) +R 10 (#244042) +D 2 (#10d1e1) +R 5 (#3fd582) +D 5 (#5025a1) +L 3 (#381b70) +D 11 (#771831) +L 7 (#2abf50) +U 11 (#17be81) +L 5 (#1eb3c0) +D 5 (#3d9c61) +L 5 (#5f4de2) +D 8 (#278361) +L 2 (#2b95b2) +D 9 (#2cb0b1) +L 4 (#4a0182) +D 6 (#2c6bb3) +L 2 (#499642) +D 5 (#4c76d3) +L 8 (#2fe412) +U 9 (#07b893) +L 2 (#236d52) +U 8 (#3956a3) +L 7 (#34f252) +U 3 (#52bbf1) +L 7 (#189470) +U 4 (#54b661) +L 9 (#189472) +U 7 (#0110f1) +L 3 (#409bf2) +U 3 (#46d3e3) +L 5 (#4c0f82) +U 11 (#215e73) +L 4 (#4c0f80) +U 7 (#4050f3) +L 9 (#39b192) +U 7 (#28b043) +L 3 (#011270) +U 7 (#1f6693) +R 7 (#73c390) +U 2 (#2c4113) +R 5 (#0321c0) +U 8 (#2f2903) +L 12 (#5ab560) +U 3 (#4dd983) +L 6 (#38b7d2) +U 12 (#03c8c3) diff --git a/2023/18/src/main.rs b/2023/18/src/main.rs new file mode 100644 index 0000000..0e3d88f --- /dev/null +++ b/2023/18/src/main.rs @@ -0,0 +1,304 @@ +use itertools::Itertools; +use std::collections::LinkedList; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// DATA + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +enum Direction { + Left, + Right, + Up, + Down, +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +enum Turn { + LeftNinety, + RightNinety, + OneEighty, + None, +} + +impl Direction { + const fn all() -> &'static [Self; 4] { + &[Direction::Left, Direction::Right, Direction::Up, Direction::Down] + } + const fn opposite(&self) -> Self { + match self { + Direction::Left => Direction::Right, + Direction::Right => Direction::Left, + Direction::Up => Direction::Down, + Direction::Down => Direction::Up, + } + } + const fn offset(&self) -> (isize, isize) { + match self { + Direction::Left => (-1, 0), + Direction::Right => (1, 0), + Direction::Up => (0, -1), + Direction::Down => (0, 1), + } + } + fn turn_kind(&self, next_dir: Direction) -> Turn { + if *self == next_dir { + Turn::None + } else if self.opposite() == next_dir { + Turn::OneEighty + } else { + match self { + Direction::Left if next_dir == Direction::Up => Turn::RightNinety, + Direction::Left if next_dir == Direction::Down => Turn::LeftNinety, + Direction::Right if next_dir == Direction::Up => Turn::LeftNinety, + Direction::Right if next_dir == Direction::Down => Turn::RightNinety, + Direction::Up if next_dir == Direction::Left => Turn::LeftNinety, + Direction::Up if next_dir == Direction::Right => Turn::RightNinety, + Direction::Down if next_dir == Direction::Right => Turn::LeftNinety, + Direction::Down if next_dir == Direction::Left => Turn::RightNinety, + _ => unreachable!(), + } + } + } +} + +impl From<&str> for Direction { + fn from(s: &str) -> Self { + match s { + "L" => Direction::Left, + "R" => Direction::Right, + "U" => Direction::Up, + "D" => Direction::Down, + s => panic!("{} is not a valid direction", s), + } + } +} + +impl From<&Direction> for char { + fn from(dir: &Direction) -> Self { + match dir { + Direction::Left => '←', + Direction::Right => '→', + Direction::Up => '↑', + Direction::Down => '↓', + } + } +} + +#[derive(Debug, Clone)] +struct DigInstruction { + dir: Direction, + count: usize, + color: String, +} + +impl From<&str> for DigInstruction { + fn from(s: &str) -> Self { + let mut parts = s.split_ascii_whitespace(); + let (dir, count, color) = ( + parts.next().unwrap(), + parts.next().unwrap(), + parts.next().unwrap().chars().skip(2).take(6).collect::(), + ); + Self { + dir: dir.into(), + count: count.parse().unwrap(), + color: color.into(), + } + } +} + +impl DigInstruction { + fn part2_transform(&mut self) { + let (distance_s, direction_s) = self.color.split_at(5); + self.count = usize::from_str_radix(distance_s, 16).unwrap(); + self.dir = match direction_s { + "0" => Direction::Right, + "1" => Direction::Down, + "2" => Direction::Left, + "3" => Direction::Up, + s => panic!("`{}` is not a valid direction code", s), + }; + } +} + +#[derive(Debug)] +struct DigPlan { + instructions: Vec, +} + +impl DigPlan { + fn part2_transform(&mut self) { + for i in &mut self.instructions { + i.part2_transform(); + } + } +} + +#[derive(Debug, Clone)] +struct DigTile { + position: Position, +} + +impl Default for DigTile { + fn default() -> Self { + Self { position: (0, 0) } + } +} + +type Position = (isize, isize); + +#[derive(Debug)] +struct DigHole { + tiles_loop: LinkedList, + area: u64, +} + +// determinant of positions p1 and p2 +fn det(p1: Position, p2: Position) -> i64 { + ((p1.0 * p2.1) - (p1.1 * p2.0)) as i64 +} + +impl DigHole { + fn new() -> Self { + DigHole { + tiles_loop: LinkedList::new(), + area: 0, + } + } + fn pos_offset_n(&self, pos: Position, offset: (isize, isize), n: usize) -> Position { + (pos.0 + offset.0 * n as isize, pos.1 + offset.1 * n as isize) + } + fn run_plan(&mut self, plan: &DigPlan) { + let mut cur_pos = (0, 0); + + self.tiles_loop.push_back(DigTile { position: cur_pos }); + let mut move_offset; + for (idx, i) in plan.instructions.iter().enumerate() { + let prev_instruction = if idx > 0 { + &plan.instructions[idx - 1] + } else { + &plan.instructions[plan.instructions.len() - 1] + }; + let Some(next_instruction) = plan.instructions.get(idx + 1).or(Some(&plan.instructions[0])) else { + panic!() + }; + let cur_turn = prev_instruction.dir.turn_kind(i.dir); + let next_turn = i.dir.turn_kind(next_instruction.dir); + + // Point needs to live on the 'outside' corner of the character. to achieve this we need to offset the move + // by the following. Found this empirically but there's probably some mathematical principle behind it... + move_offset = match (cur_turn, next_turn) { + (Turn::RightNinety, Turn::RightNinety) => 1, + (Turn::RightNinety, Turn::LeftNinety) => 0, + (Turn::LeftNinety, Turn::LeftNinety) => -1, + (Turn::LeftNinety, Turn::RightNinety) => 0, + t => panic!("turn {:?} not allowed here", t), + }; + + cur_pos = self.pos_offset_n(cur_pos, i.dir.offset(), (i.count as isize + move_offset) as usize); + self.tiles_loop.push_back(DigTile { position: cur_pos }); + } + + // Shoelace formula + // https://en.wikipedia.org/wiki/Shoelace_formula + let double_area: i64 = self + .tiles_loop + .iter() + .tuple_windows() + .map(|(a, b)| det(a.position, b.position)) + .sum(); + self.area = (double_area / 2).abs() as u64; + } +} + +impl From> for DigPlan { + fn from(lines: Lines) -> Self { + Self { + instructions: lines.map(|line| DigInstruction::from(line.unwrap().as_str())).collect(), + } + } +} + +// PROBLEM 1 solution +fn problem1(input: Lines) -> u64 { + let plan = DigPlan::from(input); + let mut dig = DigHole::new(); + dig.run_plan(&plan); + dig.area +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut plan = DigPlan::from(input); + let mut dig = DigHole::new(); + plan.part2_transform(); + dig.run_plan(&plan); + dig.area +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"R 6 (#70c710) +D 5 (#0dc571) +L 2 (#5713f0) +D 2 (#d2c081) +R 2 (#59c680) +D 2 (#411b91) +L 5 (#8ceee2) +U 2 (#caa173) +L 1 (#1b58a2) +U 2 (#caa171) +R 2 (#7807d2) +U 3 (#a77fa3) +L 2 (#015232) +U 2 (#7a21e3)"; + + const AREA16_SQUARE: &str = &"R 3 (#000000) +D 3 (#000000) +L 3 (#000000) +U 4 (#000000"; + + #[test] + fn area16_square() { + let c = Cursor::new(AREA16_SQUARE); + assert_eq!(problem1(c.lines()), 16); + } + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 62); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 952408144115); + } +} diff --git a/2023/19/Cargo.lock b/2023/19/Cargo.lock new file mode 100644 index 0000000..798dd61 --- /dev/null +++ b/2023/19/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day19" +version = "0.1.0" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] diff --git a/2023/19/Cargo.toml b/2023/19/Cargo.toml new file mode 100644 index 0000000..4b391e6 --- /dev/null +++ b/2023/19/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day19" +version = "0.1.0" +edition = "2021" + +[dependencies] +num-traits = "0.2.17" diff --git a/2023/19/input b/2023/19/input new file mode 100644 index 0000000..5d6f061 --- /dev/null +++ b/2023/19/input @@ -0,0 +1,775 @@ +kzq{m<596:R,x<1149:R,A} +in{s<2440:cd,tgz} +rs{m<2118:zx,s>3630:A,m<2297:cx,vfc} +hzz{m<2478:A,R} +ss{a<692:R,A} +sv{x<877:A,m<2320:R,A} +ph{x<2510:mf,x>3150:R,m>624:xtc,fr} +rg{s>440:hj,a>434:lg,ljm} +hdr{m<1158:R,m>1494:vfl,a<1353:A,R} +zhp{x>633:A,x<248:R,A} +fd{s<1838:A,m<3087:A,a>3495:R,jsg} +qvh{x<3002:crs,m<2331:A,s<3294:dh,R} +qbr{m>3556:R,m<3200:R,R} +sql{m<3219:A,zf} +zf{a<633:A,s<1348:A,A} +rr{x<3035:A,s<3096:A,a<2451:R,A} +bng{s>3079:R,a<724:jd,R} +lv{a<3333:tqj,hth} +bcp{a>1296:dtp,m>2875:dnq,x>1453:xjl,gp} +tz{m>3020:R,R} +gnz{m<692:R,m>1130:R,m>877:R,R} +xqg{x>3462:A,s<3298:A,s>3449:R,R} +pxd{s>1539:jvr,m<2976:frj,x>1748:mr,fts} +tj{m>3165:R,m<3044:R,a>1557:R,A} +jqm{x<2410:xf,m>3048:zm,zbl} +pv{a>2330:R,a>2259:A,x>3069:A,A} +qnz{m>1947:bz,x<2553:jr,x<3256:prq,bx} +cn{x>2438:nl,s<193:A,R} +kkb{s>3372:A,a>295:A,R} +rgr{a>707:R,a>400:hrd,npv} +klp{s<2137:jzp,x>1502:qnz,xj} +fm{m>3258:db,qd} +scf{a>490:A,m>3260:R,R} +btz{a>3068:dr,a>2668:hfb,m<2408:cs,fgr} +xqr{m>2286:A,a<2390:tf,x>3254:R,fkk} +tp{x<1044:A,a>2510:A,s<1735:A,A} +frj{x>2659:vhd,s>1415:lfl,sll} +kcn{s<1967:R,R} +vrm{a>1620:R,x>1345:A,s>3219:A,A} +vm{x<2777:gnz,m<614:hrg,a>903:shm,R} +bqq{a<994:A,m<2065:vrm,s<3419:bnv,zqt} +dtp{a>2018:cp,tbq} +qk{m>3174:slm,m>2948:jqm,gz} +gzq{m>1755:R,A} +nl{s>174:A,a<3022:A,m>2165:R,A} +mkk{a>2954:A,R} +gp{x>808:bng,nmr} +khf{m<1278:R,a>264:A,x>3812:A,A} +xxf{m>1340:A,s>2282:R,R} +bbk{a>1074:gqc,a>616:mdv,pgv} +nrp{x<2414:R,mjc} +kpt{m<3158:bcp,clt} +snl{m<868:km,m<1182:A,x<715:R,A} +cfm{s<3832:R,A} +pt{m>3354:rkx,A} +bz{a>3073:jz,s>2285:A,a>2653:tt,sgd} +zp{s>3328:R,m<981:R,A} +zkg{a<910:kjc,a<1539:hzz,s<2149:xdc,R} +jxm{x>2135:R,m>1114:R,a<668:A,ghm} +szq{s<450:cn,s>694:sdd,s>567:cm,lk} +vlz{s>584:R,s<287:R,A} +fr{s>1707:A,m<281:A,a<1488:R,R} +cdh{x>1427:R,x>1191:R,A} +brs{s>2803:A,a<821:A,s>2565:R,R} +ghm{s<3298:R,m>738:A,x>868:R,R} +cdn{s<745:R,a<1738:R,R} +vgc{s>3045:A,s>2796:R,A} +mjc{m<1443:R,a<1751:R,a<2183:A,R} +rhs{a<1453:A,m>933:kg,A} +vgh{m>3481:zmk,x>2384:ng,s<3473:pt,xvj} +lfb{x>1099:A,A} +cb{a<2973:qkv,x>2753:knq,R} +vd{a<1578:zbh,x>2560:A,s<1772:R,A} +dh{m<2492:R,m>2532:A,A} +tqj{m>1459:R,m>649:R,m>261:A,A} +sch{m<1650:R,A} +brm{x<2245:rm,vsm} +xqc{x>1906:R,s<2586:R,m>1404:R,A} +vmx{m<2123:qsd,s<2169:R,s<2335:R,A} +nrj{s>2051:ml,a<3354:A,x<772:tjm,czr} +zpq{a<805:R,s<2733:A,x<2371:R,R} +lq{m<2008:nxh,m<2296:vmx,zkg} +lfn{m>2015:R,A} +vhv{s>2798:A,m<2650:R,hsz} +rxq{a>2045:A,a>2002:A,R} +gv{x>3571:R,m>3216:A,R} +kjg{x>2443:R,s>3563:A,A} +qv{a>903:A,a<755:A,m<2822:R,A} +dqb{s<1310:A,s<1331:R,s<1351:R,A} +pll{s<1875:R,s<1928:A,a>3979:A,R} +cct{x>2015:cb,m<1524:vqc,a>3121:sdz,rs} +dq{a>3084:R,a>2989:A,x<1241:A,R} +xzx{a>2077:A,a<2023:A,a>2046:A,R} +nmr{s>3420:R,A} +klr{m<1060:A,m>1274:A,ms} +dr{s>1739:qvk,m<2207:R,a>3629:R,gk} +qvk{s<1772:A,a>3599:R,R} +gbj{a>855:R,m>2778:R,A} +drj{s<2725:R,A} +th{s<219:R,s>361:R,R} +dd{x<1745:zgq,a<2006:R,s<3396:tlq,vn} +cpr{x<2287:R,a>3768:R,A} +ntn{a<1603:R,x<3513:A,x<3736:R,A} +zlg{a<2254:xbz,m>1915:xqr,qhg} +mv{m<1685:A,s<2921:A,s<3099:nrm,A} +tnz{m>1738:R,R} +hql{m<1446:rj,qn} +bxk{x>1683:A,m<3826:R,x>1019:A,R} +pg{x>2110:A,A} +vsm{m<1847:R,x<2870:A,R} +qcs{m>3409:xh,x<948:A,x>1273:R,R} +zxz{a<961:gzj,x>900:bg,m<796:nn,A} +mp{a>2922:R,x<1561:A,A} +nb{m>1334:lq,a<942:kjq,s<2216:vcc,zd} +kjc{x>2597:R,m<2416:R,s<2114:R,R} +nz{a>349:A,x<1677:R,A} +cj{x<2805:kjg,x<3336:A,R} +kq{m<1306:R,s<3407:gmg,A} +jp{x<2530:hd,pmn} +vhj{s<2857:A,s<3029:A,x<3204:A,A} +zcr{s>3421:R,A} +kgq{m<2273:bqq,m>2432:mmf,x>1209:zxp,dzs} +crs{a<302:R,A} +fq{m>2037:vgj,m<1874:kfg,m>1968:jx,dtq} +rjx{x<2876:sf,xlc} +qsg{x>2668:A,s<2509:R,R} +gz{a<1575:tvn,a<1927:ljt,a>2208:vmj,nqk} +fzj{s>2118:A,R} +jtn{s<3102:A,m<2129:R,s>3609:A,R} +htb{x<1752:lf,s<2690:zb,a>3052:ntg,chm} +nqk{m>2784:A,x>2006:rxq,xzx} +ktz{a>1145:lnr,zz} +cl{s>2245:R,A} +pp{x>3144:R,a>1054:A,A} +km{s<3596:R,a<217:R,a<378:A,A} +btd{a>328:R,x>3186:R,s<2776:R,R} +nv{a<3170:A,x<1822:A,R} +zqt{a<1992:R,s>3757:A,R} +zvh{x<3431:A,x<3695:R,a<181:A,khf} +rjc{s>3558:rxs,ngt} +fvr{s<1819:A,a>3820:R,A} +gdh{a<1044:A,m>3889:R,a<1313:A,R} +xjl{m>2748:zfc,x>2350:hkv,qdk} +dzs{a<1516:tbs,m<2379:zcr,x>802:A,lvn} +hnk{m<1382:cqm,x<618:R,m>2969:A,ck} +sl{s<2067:R,rq} +bn{m>2044:R,x>1947:R,s>2494:A,A} +klj{m>595:zs,a>1587:A,A} +hd{m<814:rt,hdr} +pm{x>1330:rjf,s>3318:snl,m<757:lgm,jhh} +xn{m>894:R,s<486:R,A} +zxp{s>3057:mnb,A} +bt{x>2338:R,s<2515:R,A} +qdh{x<1435:dfm,m>3320:sn,m<2950:knb,xcq} +zsf{x>3119:R,s<3965:A,R} +lxs{x<1891:R,R} +fp{a>839:A,a<370:A,x>3697:R,A} +fb{x>1576:A,x>1383:R,R} +ssq{x>1389:R,x<683:A,R} +psl{s>1912:nb,mdx} +gzd{a<286:R,m>2048:R,A} +cm{x<2249:R,R} +ps{m<3057:A,a<192:A,R} +ql{x<1455:R,s<2316:A,dz} +tl{s>2746:R,a<2925:jth,s<2631:R,dq} +fl{m<871:R,R} +dnd{s<3340:R,a<1631:A,R} +hpk{x<2148:nbd,kq} +hrd{m>2977:A,R} +cqm{a<3300:A,s<2326:R,R} +vhx{a>264:sx,czq} +hpb{m<1744:bkc,x<1249:cf,vkm} +ms{x>3633:R,a>279:A,x>3536:A,A} +qkk{a<2687:A,s<2740:R,A} +vfc{a<2771:R,A} +rjf{s<3472:br,x<1970:A,a>216:R,A} +xbz{x<3190:lkc,gvr} +mx{s>1027:R,A} +fxr{a<3952:R,R} +sz{m>3436:R,a>3677:R,a<3578:A,R} +qsd{s>2211:A,s>2062:A,x<1916:R,R} +jxd{a>3568:A,x>2134:R,hr} +xlc{m<3646:ntn,m>3749:vb,a<829:hmk,gc} +zmb{x<854:A,tv} +dxx{a>796:brs,m<1206:gq,R} +jj{m<1020:xhs,a<758:ghs,dxx} +jd{s>2702:A,a>437:A,x<1198:A,A} +vb{a<1183:A,x>3584:R,R} +zg{a<1774:R,rp} +fjl{m<1926:A,A} +sx{m>412:rlx,m<224:hsm,ts} +clt{m>3707:kbk,vgh} +sdd{x>1333:A,s>792:ffv,qc} +ltl{s<2747:A,A} +qd{a<1088:A,a>1543:R,R} +lnr{s>1491:A,tgm} +hr{s<2837:A,R} +tbn{m>1412:A,x<1334:ltl,bbq} +db{x<1961:R,R} +dfm{m>3324:R,A} +mdx{s>1504:cc,sb} +xvj{x<1523:R,R} +zm{x>3061:txs,x<2826:R,R} +bkc{s<3439:R,s<3712:R,R} +sp{a>996:A,R} +ftd{x>2631:R,m>2447:R,x>2241:R,R} +qxl{m>591:R,x>508:A,R} +ngs{x<1410:R,A} +nvl{m>836:R,A} +kbd{m>2655:jxd,shz} +bzz{m>2969:R,A} +hth{x>532:R,x<263:A,A} +dtq{x<1785:fjl,qpt} +qbz{m<1913:kkb,m<2018:R,x<2884:tvj,gzd} +tr{x<1816:zxz,s<3159:gj,a>942:vtz,vm} +pdr{a>3495:R,x<960:A,A} +sfd{m<1983:R,a>3730:A,A} +qm{a<3400:A,R} +vlx{x<1628:R,x>2516:A,R} +ztb{s<1848:A,s<1862:R,A} +tlq{m<3961:A,A} +mtx{x<628:R,s<2839:A,R} +dp{m<631:R,x>3424:R,A} +bch{x>544:R,s<1473:A,A} +qkv{s>3614:A,a<2756:R,A} +tlz{s<1526:R,A} +nr{a<1406:R,s>1965:ls,a<1804:R,kmj} +rn{m<3158:R,x>1428:R,A} +fn{s>2334:R,R} +fv{s>496:R,m>2847:A,R} +kdt{x<1651:qx,mn} +dt{s<1907:R,A} +fxp{s>1444:R,x<1151:R,s<1432:R,A} +skp{s<2684:ff,a>3661:grc,kbd} +sf{a<1342:R,R} +vn{x<2642:A,m>3964:A,x>3188:R,R} +cc{m<1326:ph,vd} +st{m>2033:kv,x>2199:mh,qs} +tt{s<2228:R,m>2870:A,x<2943:R,A} +zxc{x<2549:A,a<790:fl,a>821:hs,rv} +nzg{a>721:R,a<709:R,m<1312:R,R} +dpm{x<2029:dvz,tkb} +xzp{x<1033:zhp,m<3650:vk,s>1404:cdh,R} +kmt{x>1508:A,m>3316:sdt,ngs} +jr{x<2063:zvg,x<2340:A,m>945:cl,R} +slv{m<658:A,m>699:R,x<1549:R,R} +txs{x>3387:R,x>3223:A,A} +jbj{x<1080:R,R} +slm{s>2107:qgv,x>1425:pbp,nr} +xj{s<2246:lv,hnk} +rxs{a>3208:R,s>3711:tz,bvt} +sgd{m>2799:R,m<2302:A,s>2226:A,A} +gng{m>1061:xqc,m>382:A,s<2565:A,rkh} +tq{m>3700:R,m>3578:A,m<3552:R,R} +gqc{m<751:vc,m<1228:trs,qln} +bzg{a<3173:A,x>2040:A,A} +gr{s>1874:A,A} +sm{x>1443:fc,m<3252:pqp,lh} +vt{x<324:sch,m<2405:zdb,s<2751:A,mtx} +rrz{a<2925:A,m<1807:A,A} +dvm{s>3793:R,a<714:R,a>723:A,R} +ns{a>216:fzj,s<2226:A,pl} +tsn{m<1180:R,x<1442:ss,a<693:A,dvm} +lgm{m<295:mt,qxl} +gq{a>772:R,s>2860:A,A} +tv{x<1247:R,m>2373:A,R} +shm{a>923:R,R} +tc{m>1326:pd,x<2176:A,A} +zs{a>956:R,x<1552:A,m<1047:A,A} +kbk{a<1245:zr,m>3892:dd,a>1823:hf,rkz} +gc{s>1625:A,x<3456:R,A} +vv{m<1670:jp,a<1470:tvs,dx} +dv{a<3900:dt,x<2401:R,a>3952:pll,A} +rh{x>1522:md,zmb} +bv{s<813:R,x<1898:A,m<2291:A,A} +dfs{a>2165:A,x<2625:A,m<905:A,R} +sq{m<1537:A,a>3700:R,s>3518:R,R} +zbh{m>1984:R,s>1714:A,s>1589:R,A} +pmn{x>3353:mxr,xn} +fc{m<3293:A,R} +hj{m>2607:nz,bv} +cjt{s<3222:R,a>778:A,R} +md{a<2755:A,a<2974:bqz,m<1812:A,R} +jcn{x<3486:A,A} +tm{x<1626:R,x>2473:A,R} +rp{s<2043:A,A} +dtt{x<2469:A,x>2652:A,R} +rkh{a<3899:R,a<3939:A,a>3972:A,R} +vtz{x<3222:jnj,A} +dz{a>1273:R,x<2592:R,a>1109:R,A} +zbl{x<3369:A,a<1301:A,A} +xcc{x<1386:R,x>2717:A,A} +snz{x>1513:R,A} +pnp{m>2870:R,s<1615:R,A} +nzs{x<2228:svh,a>3290:drm,qck} +dx{m<2704:st,bnq} +ml{m>2093:A,m<723:R,x>705:A,A} +nrm{x<2693:R,x>3329:R,a<1508:A,R} +ls{a>2023:R,x<645:A,x<1062:R,R} +zbk{s<3432:A,R} +pcx{a>133:A,R} +xc{a<2881:A,m>2239:R,R} +bvt{x>2521:R,m>3250:A,A} +vfl{a>1549:A,A} +gb{s>2827:fxr,a<3925:vp,tk} +nt{a<3562:R,s>1880:R,A} +qh{s<2766:R,m>1823:R,m<1032:R,R} +vkb{m>3657:R,a<1604:A,R} +qdk{a>446:vrt,a>237:R,A} +nx{s<3053:A,x>2661:A,s<3652:A,A} +ffv{m>2118:A,x<591:A,R} +gnx{m>3550:A,s>340:R,A} +xdc{s<2011:R,a<1886:A,m<2525:R,R} +rnx{x>1741:A,R} +vgj{a<1194:R,a>1316:A,mjf} +rz{m<245:A,R} +jzp{x<1378:nrj,qm} +dhz{m<2327:dnd,x<3029:ftd,m<2461:R,A} +mmf{x>1349:R,m>2512:A,R} +chm{m<1360:sh,a>2753:dmf,x<2850:zdz,rd} +bb{a<1712:R,s<1716:A,fgd} +lfl{s>1480:R,fxp} +vkm{a<944:fb,cdz} +shz{x>2518:R,s>2824:pdr,x<1098:A,R} +lvn{a<1975:A,A} +ng{m<3372:pjq,R} +ntg{x<2896:bvx,hql} +qck{a>2892:R,x>2827:gx,m<1542:njr,R} +xg{m<274:R,R} +qs{a<1914:cdn,R} +bd{m>3657:jbj,m<3576:R,x>715:hk,R} +mnb{m>2328:A,s<3622:A,a<1275:A,A} +lkc{s>3247:R,A} +jz{a<3649:A,a<3878:R,R} +pgv{x<2543:pm,m<747:vhx,s<3269:kb,hx} +vfk{s>2335:sd,R} +mxr{s<525:pz,fp} +qln{s<3057:tbn,m>1379:nrp,tc} +hsm{a<391:A,R} +mdv{a>842:tr,s<3143:jj,mfx} +tgm{x<1435:R,A} +bj{x>2353:R,A} +frk{s>1770:A,lfb} +tnt{a>228:R,x<307:A,A} +bx{s<2240:R,m>928:gf,x<3724:gm,fn} +vr{s<3307:R,m>3549:R,A} +zd{a<1856:ql,vfk} +jth{s<2601:A,A} +tbq{s>3089:A,s<2692:bzz,A} +zz{x>1544:tlz,bch} +mq{m>1048:R,s<3046:dfs,m<905:nvl,R} +xsb{a>968:A,s>3282:R,R} +ctf{m>2182:dhz,s>3326:cj,m<1769:mv,xxb} +cd{a>2405:qjl,s<1247:vv,m<2651:psl,cz} +sdt{m<3413:A,s<1386:A,R} +nm{m>1061:A,m<861:R,a<1430:R,zp} +kbp{a>257:R,R} +czq{s>3301:A,pcx} +rv{x<3177:R,x<3631:R,m>605:R,R} +lg{x<2664:A,fqp} +rq{s<2141:R,x<1452:A,x<2322:A,A} +rkx{m>3406:R,s>2856:R,a>1356:R,R} +rxz{a>3491:ft,m>1104:R,s<1879:A,A} +tqc{s>2638:A,R} +kb{m<1115:btd,a<356:zvh,nh} +svh{m>2131:A,s>1205:A,sdj} +gzj{s>3413:R,m<563:R,R} +tgz{a>2498:nc,m<1536:bbk,m>2606:kpt,dpm} +nc{s>2973:mml,a>3390:skp,htb} +zr{m>3877:A,a<786:zbk,sp} +hf{m<3830:A,R} +rj{s>2812:A,R} +lvf{s<2217:ghz,m<352:fg,s<2328:R,R} +tvn{x<1573:A,ct} +pqp{a<1606:A,s>1424:R,x>1211:A,A} +fpm{a<3321:A,a>3380:A,R} +lpq{a<2688:A,m>550:A,R} +skb{a>496:R,a>370:A,m>3052:A,R} +vqc{a>2824:A,mdj} +sk{s>3032:R,a>1895:kzq,A} +fqp{x<3283:R,x>3734:R,m>2573:A,A} +kv{x<1403:R,A} +jhh{a<365:zv,s>2937:R,a<532:A,A} +qx{m<216:A,m<345:A,R} +tvj{m>2055:R,x>2595:R,R} +fkk{s<3440:A,R} +nh{s>2904:R,x>3474:A,R} +bnq{x<1726:qcs,x>2609:xjj,pg} +bhv{s<1815:btz,a<3302:rh,m<1426:xs,dfq} +grc{a>3883:gb,a>3753:brm,a<3710:vhv,xr} +pl{x<1731:R,a<138:R,m<1085:R,R} +ngt{x>2573:xqg,a>3461:sz,m>3259:vr,vmf} +jnq{m<2176:R,m>2968:R,x>3137:A,R} +sb{m>1679:lfn,s>1408:snz,klj} +jvr{a>1008:bb,s<1695:np,ln} +kjq{m<593:lvf,m<873:bcs,a>378:djc,ns} +hh{s>2869:R,A} +kj{x>1634:rrz,s<2750:A,m>2365:mp,R} +xf{m<3098:A,m<3128:cr,a<995:rn,vj} +zv{x<860:A,a>221:R,x>1038:A,R} +rm{a>3836:A,A} +tbs{a>927:A,R} +gm{m<317:A,A} +sdz{s>3569:R,m<1986:tnz,fpm} +lf{x<960:vt,x<1466:tl,kj} +qhg{m<1782:vgc,a<2401:A,rr} +nxh{a<1275:R,gzq} +tk{x<2449:A,R} +cp{s<3395:R,s>3741:cfm,tm} +zdb{x>591:A,s<2781:A,R} +mhm{x<3419:R,x<3710:A,m>2686:A,A} +pz{m>885:R,a<1395:A,R} +jxl{a<1824:rz,A} +vmr{a>570:jqd,m>2081:qvh,m>1814:qbz,dkx} +kmj{s<1935:R,A} +prq{m>920:xxf,R} +cf{s>3313:A,x>700:A,m>1872:A,R} +qgv{a>1042:vlx,a>596:rqd,R} +mr{x>3011:gv,x>2379:mjx,fm} +cx{a>2892:A,A} +xz{m>2929:R,s<2814:R,s>2986:A,A} +ck{x<994:A,s>2364:A,m>2064:A,R} +zdd{s>911:nzs,szq} +vj{s>2138:A,m>3156:A,R} +jnj{a<996:A,s<3488:R,R} +kfg{s<786:qtq,A} +tjm{s<2018:R,s<2038:R,R} +dkx{x>2834:R,x<2537:R,nx} +zfc{m<2823:gbj,crk} +ff{m>1893:dc,a>3780:gng,xjz} +vmf{x<1019:A,A} +ghs{a<703:A,a>738:R,nzg} +gs{a<393:R,a<477:R,s<3909:R,zsf} +zvg{s>2319:A,x>1801:A,A} +gk{x<2559:R,A} +xjz{s<2581:R,x>2364:R,a<3606:A,tqc} +sd{x<2625:R,A} +mdj{s>3552:A,x>946:A,R} +nbd{x<1042:sq,m<915:A,tmj} +tmj{m>1880:A,x>1503:R,A} +drm{x>3299:R,s<1171:mx,R} +cs{m>1405:A,x>2157:A,tp} +xtc{x<2884:R,A} +gj{a>929:vhj,a<885:R,A} +mml{m>2426:rjc,a>3449:hpk,cct} +hfb{x<1738:xc,R} +vk{m<3572:A,m>3601:A,s>1380:R,R} +dmf{a<2866:A,R} +jx{m<1993:R,R} +vz{m<408:R,A} +cr{m>3114:R,x<927:A,R} +rqd{a>827:R,m<3616:A,R} +hxm{a<2012:A,a<2147:R,s>1974:R,R} +fgr{m>3145:A,R} +ljm{a>175:th,x>2267:R,s>203:A,R} +sh{x<2556:A,s<2802:qkk,x>3244:R,lpq} +mh{x<3136:R,jcn} +jcv{x>3293:A,A} +xh{m<3719:A,x<825:A,R} +nn{s>3158:R,a>1035:A,m>397:A,A} +np{m<3051:pnp,scf} +xd{m<3074:A,s<2935:A,a>2618:A,R} +njr{m>623:R,x<2518:A,R} +vmj{x>1396:pv,a<2288:bfk,R} +rt{a>991:xg,a>395:R,a>238:A,vz} +lmf{a<941:tq,s<1601:R,R} +zdz{s>2837:R,m<2501:cvx,a<2586:R,A} +gmg{a>3638:A,a>3540:R,A} +zmk{m>3609:vkb,jc} +skj{a>908:R,a>564:R,x<489:tnt,R} +mb{m>3811:ktz,x<1811:gl,rjx} +rl{x<2034:R,m>1003:A,a<3236:A,R} +dvz{m>1954:kgq,hpb} +hmk{x>3265:R,s>1639:A,s>1393:R,R} +dfq{s>1884:hjr,a>3606:bq,m<2391:hvz,fd} +jqd{x<2830:cjt,jtn} +vhd{m>2806:pp,a<962:dsd,A} +knb{a>1140:R,qv} +br{x<1778:R,R} +zkt{s>3133:R,m<3860:R,A} +zgq{a>1715:R,m<3933:R,A} +xjj{a<1879:R,x>3421:qbr,x<3014:R,R} +bg{m<731:A,m<1261:R,a<1026:A,A} +ct{x<2963:A,s<2119:R,x>3312:A,A} +bvx{x>2177:R,m<1910:rl,x>1943:bzg,nv} +hg{x>2696:jnq,x>2077:bt,bn} +hs{x<3271:R,A} +tvs{a<654:rg,m<2741:fq,qdh} +qtq{a<982:R,a<1234:A,R} +gcg{m>3101:R,a<480:R,A} +vp{a>3910:R,s>2776:A,a>3898:A,A} +pd{x>1921:A,A} +xt{a>967:R,m>3516:A,a>776:R,A} +qc{a>3321:R,A} +tkb{a<1063:vmr,a>1959:zlg,ctf} +bcs{s>2230:lxs,s>2030:R,m<743:slv,A} +qn{a<3266:A,a>3348:R,m<2667:R,A} +cvx{x>2282:A,m<2005:R,a<2628:A,R} +hrg{m>356:R,s>3696:R,A} +hvz{s<1848:A,a<3490:A,s<1870:A,xcc} +lh{s>1362:R,x>1231:R,a>1747:R,R} +vc{m<422:jxl,x>2315:zdg,sk} +fts{x<952:skj,a>1143:sm,x<1335:sql,kmt} +jsg{x>2596:R,x>1393:A,m>3518:R,A} +hk{x>1083:R,m>3604:R,A} +mt{s>2812:R,x<595:R,s>2688:R,A} +cdz{x>1644:R,m>1862:R,A} +pxv{a<3725:nt,a<3830:A,A} +njd{m<2225:R,mkk} +mjx{s<1366:dqb,tj} +bfk{a>2248:R,R} +xs{m<518:pxv,a<3589:rxz,a>3783:dv,gr} +djc{s>2174:A,js} +zx{x<1016:A,x<1528:R,R} +mzd{a>2015:R,R} +knq{a>3143:R,x>3377:R,R} +bq{s>1846:A,s>1827:cpr,fvr} +zdg{s>3443:A,a>1681:mzd,s<2909:dp,A} +sll{m<2763:R,s<1342:ssq,R} +ft{m<1121:A,s>1882:A,s>1847:R,R} +dnq{x>1487:bl,s<3256:tpk,m<3063:rgr,gmd} +bqz{a>2865:R,A} +sdj{m>1268:A,s<1037:R,a<3341:A,R} +dc{s>2554:A,m<2684:R,qsg} +bbq{s>2709:A,s>2586:A,R} +js{m<1039:A,A} +hjr{x<1882:R,s>1927:R,jcv} +lk{a>3257:bj,a>2920:A,m>1832:fv,R} +ts{a>401:R,a<339:A,s<3429:A,R} +xr{x<1991:sv,s<2852:qh,s<2893:hh,sfd} +bl{m<2988:A,a>784:xsb,a>295:skb,ps} +kg{x<1065:R,x>1534:A,R} +gf{x<3539:A,a<3041:R,R} +ckj{s<2886:A,s>2893:A,A} +fg{s<2314:A,m<139:R,x>1421:A,R} +mn{x<3000:A,m>315:R,m<177:A,R} +vrt{s>3263:A,a<878:R,x>2044:A,R} +npv{x<687:R,A} +ljt{x<2374:R,A} +pbp{a>1398:hxm,a>588:xt,a<333:kcn,R} +fgd{s>1796:R,x>2219:R,m<3069:A,R} +xhs{a<752:R,zpq} +tf{m<2132:A,m>2187:A,A} +qjl{s<1624:zdd,s>1981:klp,bhv} +gx{s<1192:A,s>1376:R,R} +hsz{m>3435:R,s<2747:A,A} +rkz{a>1486:bxk,m<3808:R,a>1388:A,zkt} +mjf{x>2320:A,x<1139:A,R} +gl{a>1425:bd,s>1660:frk,s>1500:lmf,xzp} +gvr{m<2241:R,R} +bnv{s<3071:R,R} +vcc{m<618:sl,x>2269:zg,rhs} +ghz{x>1927:A,a<394:A,m<231:A,R} +crk{m<2842:R,x>2970:A,x<2213:R,R} +mfx{a>731:zxc,m<547:kdt,s>3502:tsn,jxm} +pjq{m<3273:A,a<945:A,A} +tpk{m>2975:drj,xz} +qpt{x>2909:A,a>991:A,a<810:R,R} +jc{m>3552:R,R} +rd{s<2850:R,x>3613:A,s>2904:xd,ckj} +rlx{x>3373:R,A} +hkv{a>502:mhm,s>3324:R,A} +dsd{s<1357:R,x<3340:R,R} +ln{s>1813:ztb,m>3116:rnx,A} +czr{m>2538:R,A} +xxb{s>2993:R,x>2787:R,dtt} +mf{s<1679:A,m<538:A,R} +zb{s>2579:njd,hg} +sn{m>3668:gdh,x>2740:A,s>624:R,gnx} +trs{a<1933:nm,mq} +xcq{m>3184:vlz,x<2483:R,R} +hx{s>3738:gs,x<3448:kbp,klr} +cz{s>1882:qk,m<3508:pxd,mb} +gmd{x>924:A,x<437:A,gcg} + +{x=363,m=218,a=594,s=411} +{x=973,m=37,a=2533,s=132} +{x=2768,m=1537,a=645,s=580} +{x=2079,m=168,a=502,s=656} +{x=38,m=145,a=3452,s=1162} +{x=70,m=1238,a=99,s=560} +{x=1261,m=1540,a=283,s=28} +{x=1194,m=780,a=1538,s=2681} +{x=1236,m=1847,a=1403,s=1086} +{x=2227,m=163,a=716,s=410} +{x=9,m=369,a=572,s=1418} +{x=201,m=1472,a=943,s=3541} +{x=2730,m=1777,a=127,s=3031} +{x=329,m=139,a=1310,s=2983} +{x=54,m=981,a=1378,s=783} +{x=658,m=410,a=482,s=490} +{x=1555,m=1246,a=365,s=2769} +{x=1075,m=95,a=806,s=750} +{x=408,m=125,a=2296,s=1599} +{x=715,m=1257,a=1287,s=1638} +{x=63,m=1295,a=169,s=1802} +{x=2068,m=4,a=768,s=365} +{x=1390,m=3366,a=1730,s=1631} +{x=1900,m=622,a=761,s=1108} +{x=1857,m=678,a=136,s=965} +{x=3295,m=47,a=303,s=63} +{x=2853,m=700,a=315,s=647} +{x=379,m=304,a=914,s=59} +{x=2591,m=385,a=1410,s=1473} +{x=140,m=1237,a=769,s=2670} +{x=747,m=395,a=1988,s=633} +{x=30,m=381,a=1966,s=2597} +{x=1969,m=1191,a=501,s=328} +{x=127,m=1200,a=47,s=2408} +{x=1409,m=2136,a=205,s=3122} +{x=150,m=88,a=1082,s=3059} +{x=320,m=2917,a=1371,s=22} +{x=2237,m=1186,a=466,s=2252} +{x=2460,m=7,a=1758,s=874} +{x=421,m=126,a=1338,s=1479} +{x=150,m=675,a=2555,s=267} +{x=1007,m=1178,a=2927,s=2435} +{x=902,m=190,a=77,s=1279} +{x=1393,m=716,a=686,s=569} +{x=339,m=1199,a=275,s=338} +{x=116,m=1997,a=739,s=142} +{x=1332,m=69,a=74,s=1953} +{x=2324,m=2089,a=101,s=1595} +{x=2966,m=696,a=305,s=1221} +{x=810,m=1860,a=141,s=49} +{x=995,m=382,a=1020,s=756} +{x=899,m=133,a=746,s=952} +{x=325,m=1880,a=554,s=1749} +{x=506,m=1478,a=897,s=597} +{x=659,m=424,a=20,s=1447} +{x=4,m=310,a=1121,s=286} +{x=3357,m=396,a=1204,s=2886} +{x=1209,m=614,a=2397,s=408} +{x=505,m=339,a=72,s=709} +{x=3478,m=1412,a=1777,s=1} +{x=2271,m=136,a=83,s=477} +{x=3008,m=280,a=2167,s=1522} +{x=543,m=772,a=433,s=3560} +{x=227,m=2037,a=1444,s=272} +{x=869,m=80,a=2585,s=1212} +{x=893,m=1207,a=124,s=126} +{x=2178,m=2030,a=3000,s=1438} +{x=450,m=1798,a=1307,s=2147} +{x=469,m=1554,a=1080,s=393} +{x=49,m=389,a=813,s=39} +{x=3041,m=607,a=379,s=1435} +{x=62,m=121,a=47,s=2394} +{x=1589,m=2474,a=1599,s=3} +{x=439,m=1493,a=284,s=3} +{x=619,m=1377,a=1107,s=3452} +{x=1802,m=1132,a=851,s=111} +{x=2154,m=464,a=2611,s=2147} +{x=843,m=423,a=355,s=838} +{x=3,m=423,a=139,s=857} +{x=42,m=843,a=2219,s=3695} +{x=1068,m=575,a=88,s=222} +{x=3097,m=238,a=913,s=2452} +{x=109,m=206,a=291,s=728} +{x=3635,m=1815,a=26,s=19} +{x=49,m=115,a=2998,s=26} +{x=2093,m=391,a=267,s=2896} +{x=282,m=2364,a=62,s=62} +{x=361,m=719,a=1679,s=137} +{x=631,m=1131,a=203,s=2281} +{x=794,m=1004,a=868,s=636} +{x=1702,m=415,a=35,s=236} +{x=63,m=1690,a=682,s=2009} +{x=1815,m=3459,a=144,s=374} +{x=91,m=590,a=1651,s=1681} +{x=1561,m=2303,a=307,s=1463} +{x=842,m=927,a=868,s=455} +{x=51,m=23,a=427,s=247} +{x=2431,m=187,a=267,s=184} +{x=1596,m=1904,a=668,s=1553} +{x=922,m=1435,a=336,s=1301} +{x=1038,m=1632,a=220,s=117} +{x=1935,m=1433,a=2988,s=316} +{x=21,m=1158,a=1988,s=1242} +{x=840,m=1272,a=418,s=1198} +{x=1227,m=1811,a=158,s=10} +{x=1836,m=56,a=19,s=1573} +{x=461,m=621,a=532,s=92} +{x=2082,m=1292,a=1032,s=322} +{x=2094,m=249,a=865,s=2030} +{x=1092,m=265,a=1745,s=1720} +{x=2361,m=1087,a=209,s=1934} +{x=92,m=111,a=3188,s=2172} +{x=248,m=2163,a=1351,s=1194} +{x=1675,m=2194,a=324,s=1764} +{x=2148,m=3124,a=854,s=1823} +{x=520,m=617,a=649,s=49} +{x=2497,m=12,a=270,s=161} +{x=248,m=2,a=1026,s=1196} +{x=367,m=860,a=1141,s=61} +{x=411,m=90,a=3401,s=2156} +{x=284,m=265,a=421,s=866} +{x=2152,m=2245,a=1206,s=1056} +{x=1578,m=74,a=93,s=1494} +{x=225,m=50,a=2223,s=1888} +{x=2448,m=372,a=587,s=820} +{x=1222,m=215,a=31,s=1753} +{x=2906,m=1170,a=1120,s=291} +{x=1253,m=329,a=478,s=2489} +{x=563,m=150,a=103,s=1051} +{x=212,m=980,a=1325,s=882} +{x=580,m=754,a=555,s=1470} +{x=522,m=397,a=1018,s=763} +{x=2327,m=1100,a=975,s=511} +{x=324,m=1719,a=48,s=569} +{x=1872,m=3353,a=54,s=1330} +{x=2082,m=1209,a=2390,s=747} +{x=1510,m=2290,a=591,s=45} +{x=60,m=1482,a=924,s=598} +{x=775,m=675,a=1635,s=1560} +{x=316,m=170,a=168,s=1335} +{x=1821,m=79,a=2073,s=92} +{x=612,m=1814,a=1651,s=3496} +{x=116,m=2643,a=1073,s=301} +{x=313,m=92,a=2892,s=109} +{x=1109,m=279,a=701,s=763} +{x=86,m=214,a=1910,s=99} +{x=1049,m=320,a=530,s=216} +{x=192,m=366,a=887,s=1516} +{x=353,m=107,a=257,s=225} +{x=117,m=72,a=810,s=147} +{x=341,m=17,a=190,s=2748} +{x=1397,m=572,a=2275,s=1494} +{x=386,m=1691,a=346,s=985} +{x=603,m=584,a=1464,s=291} +{x=49,m=46,a=2277,s=1200} +{x=3116,m=1,a=500,s=166} +{x=310,m=1054,a=1271,s=1423} +{x=1690,m=721,a=1555,s=232} +{x=1713,m=2002,a=1132,s=611} +{x=229,m=1721,a=1590,s=42} +{x=16,m=1444,a=2296,s=144} +{x=371,m=339,a=1073,s=1585} +{x=1025,m=10,a=491,s=196} +{x=58,m=1161,a=492,s=1401} +{x=305,m=1366,a=919,s=236} +{x=1427,m=654,a=2052,s=2399} +{x=1312,m=723,a=408,s=173} +{x=589,m=703,a=29,s=126} +{x=2609,m=1461,a=266,s=129} +{x=299,m=253,a=857,s=2220} +{x=259,m=600,a=494,s=546} +{x=2978,m=988,a=1660,s=2661} +{x=193,m=478,a=213,s=1631} +{x=3378,m=2275,a=534,s=2317} +{x=654,m=2641,a=284,s=1768} +{x=50,m=76,a=672,s=120} +{x=85,m=630,a=1446,s=1321} +{x=306,m=986,a=808,s=1724} +{x=782,m=1554,a=741,s=2220} +{x=1115,m=48,a=3283,s=958} +{x=214,m=2498,a=996,s=981} +{x=1437,m=53,a=2052,s=914} +{x=3256,m=441,a=47,s=108} +{x=689,m=1954,a=60,s=133} +{x=1,m=2270,a=903,s=13} +{x=2373,m=956,a=2739,s=860} +{x=1270,m=357,a=2115,s=1868} +{x=114,m=587,a=349,s=62} +{x=186,m=491,a=464,s=46} +{x=2001,m=99,a=649,s=1854} +{x=408,m=386,a=681,s=774} +{x=697,m=775,a=2740,s=1582} +{x=525,m=2075,a=11,s=822} +{x=621,m=1069,a=233,s=255} +{x=979,m=2750,a=241,s=199} +{x=2006,m=665,a=2211,s=1776} +{x=483,m=213,a=2042,s=67} +{x=1181,m=1199,a=1513,s=919} +{x=1378,m=274,a=438,s=546} +{x=59,m=224,a=3034,s=254} diff --git a/2023/19/src/main.rs b/2023/19/src/main.rs new file mode 100644 index 0000000..dd416cd --- /dev/null +++ b/2023/19/src/main.rs @@ -0,0 +1,392 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::ops::{Range, IndexMut, Index}; +use std::time::Instant; +use num_traits::Num; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// DATA + +const INPUT_RANGE: Range = 1..4001; + +#[derive(Debug)] +struct RulePredicate { + op: PredicateOperator, + var: char, +} + +#[derive(Debug, Clone)] +enum RuleAction { + Terminate(bool), + Jump(String), +} + +impl From<&str> for RuleAction { + fn from(s: &str) -> Self { + match s { + "A" => Self::Terminate(true), + "R" => Self::Terminate(false), + s => Self::Jump(s.into()), + } + } +} + +#[derive(Debug)] +struct Rule { + pred: RulePredicate, + action: RuleAction, +} + +#[derive(Debug)] +enum PredicateOperator { + Always, + Lt(u64), + Gt(u64), +} + +#[derive(Debug, Clone)] +struct XmasRanges { + ranges: [Vec>; 4], +} + +impl XmasRanges { + fn none() -> XmasRanges { + Self { + ranges: [vec![0..0], vec![0..0], vec![0..0], vec![0..0]], + } + } + fn all() -> XmasRanges { + Self { + ranges: [ + vec![INPUT_RANGE], + vec![INPUT_RANGE], + vec![INPUT_RANGE], + vec![INPUT_RANGE], + ], + } + } + fn idx(c: char) -> usize { + match c { + 'x' => 0, + 'm' => 1, + 'a' => 2, + 's' => 3, + c => panic!("`{}` is not a valid xmas char", c), + } + } + fn insert(&mut self, c: char, v: Vec>) { + self.ranges[Self::idx(c)] = v; + } + fn count_states(&self) -> u64 { + self.ranges + .iter() + .map(|ranges| ranges.iter().map(|range| range.end - range.start).sum::()) + .product() + } +} + +impl Index for XmasRanges { + type Output = Vec>; + fn index(&self, index: char) -> &Self::Output { + &self.ranges[Self::idx(index)] + } +} + +impl IndexMut for XmasRanges { + fn index_mut(&mut self, index: char) -> &mut Self::Output { + &mut self.ranges[Self::idx(index)] + } +} + +fn range_overlap(r1: &Range, r2: &Range) -> Option> { + let new_start = std::cmp::max(r1.start, r2.start); + let new_end = std::cmp::min(r1.end - T::one(), r2.end - T::one()); + + if new_start <= std::cmp::min(r1.end - T::one(), r2.end - T::one()) && new_end >= std::cmp::max(r1.start, r2.start) + { + Some(new_start..new_end + T::one()) + } else { + None + } +} + +fn range_exclude(keep: &Range, exclude: &Range) -> Vec> { + let mut residual = Vec::new(); + if let Some(overlap) = range_overlap(keep, exclude) { + if keep.start < overlap.start { + residual.push(keep.start..overlap.start); + } + if keep.end > overlap.end { + residual.push(overlap.end..keep.end); + } + } else { + residual.push(keep.clone()); + } + residual +} + +impl From<&str> for PredicateOperator { + fn from(s: &str) -> Self { + let (op_s, val_s) = s.split_at(1); + match op_s { + "<" => PredicateOperator::Lt(val_s.parse().unwrap()), + ">" => PredicateOperator::Gt(val_s.parse().unwrap()), + s => panic!("unknown operator {}", s), + } + } +} + +impl From<&str> for RulePredicate { + fn from(s: &str) -> Self { + let (var_s, pred_s) = s.split_at(1); + Self { + op: pred_s.into(), + var: var_s.chars().next().unwrap(), + } + } +} + +impl RulePredicate { + fn check(&self, part: &Part) -> bool { + match self.op { + PredicateOperator::Always => true, + PredicateOperator::Gt(val) => part.0[&self.var] > val, + PredicateOperator::Lt(val) => part.0[&self.var] < val, + } + } + fn matching_range(&self) -> Range { + match self.op { + PredicateOperator::Always => INPUT_RANGE, + PredicateOperator::Gt(val) => val + 1..INPUT_RANGE.end, + PredicateOperator::Lt(val) => INPUT_RANGE.start..val, + } + } +} + +impl From<&str> for Rule { + fn from(s: &str) -> Self { + if let Some((predicate_s, action_s)) = s.split_once(':') { + Self { + pred: predicate_s.into(), + action: action_s.into(), + } + } else { + Self { + pred: RulePredicate { + op: PredicateOperator::Always, + var: '\0', + }, + action: s.into(), + } + } + } +} + +impl Rule { + fn predicate_result(&self, ranges: XmasRanges) -> (XmasRanges, XmasRanges) { + if let PredicateOperator::Always = self.pred.op { + (ranges, XmasRanges::none()) + } else { + let (mut matching, mut unmatching) = (ranges.clone(), ranges.clone()); + let relevant_ranges = &ranges[self.pred.var]; + matching.insert( + self.pred.var, + relevant_ranges + .iter() + .filter_map(|range| range_overlap(range, &self.pred.matching_range())) + .collect(), + ); + unmatching.insert( + self.pred.var, + relevant_ranges + .iter() + .flat_map(|range| range_exclude(range, &self.pred.matching_range())) + .collect(), + ); + (matching, unmatching) + } + } + fn possible_ranges(&self, wfs: &Workflows, ranges: XmasRanges) -> (u64, XmasRanges) { + let (matching, unmatching) = self.predicate_result(ranges); + match &self.action { + RuleAction::Terminate(true) => (matching.count_states(), unmatching), + RuleAction::Terminate(false) => (0, unmatching), + RuleAction::Jump(wf) => (wfs.0[wf].possible_ranges(wfs, matching), unmatching), + } + } +} + +#[derive(Debug)] +struct Workflow { + name: String, + rules: Vec, +} + +impl From<&str> for Workflow { + fn from(s: &str) -> Self { + let (name_s, rest_s) = s.split_once('{').unwrap(); + let rules = rest_s.split_once('}').unwrap().0.split(',').map(|r| r.into()).collect(); + Self { + name: name_s.into(), + rules, + } + } +} +impl Workflow { + fn execute(&self, part: &Part) -> RuleAction { + for r in &self.rules { + if r.pred.check(part) { + return r.action.clone(); + } + } + panic!("unhandled part {:?}", part); + } + fn possible_ranges(&self, wfs: &Workflows, mut ranges: XmasRanges) -> u64 { + let mut accum = 0u64; + for r in &self.rules { + let count; + (count, ranges) = r.possible_ranges(wfs, ranges); + accum += count + } + accum + } +} + +impl Workflows { + fn execute(&self, part: &Part) -> bool { + let mut action = RuleAction::Jump("in".into()); + loop { + match &action { + RuleAction::Terminate(b) => return *b, + RuleAction::Jump(j) => { + let next_workflow = &self.0[j]; + action = next_workflow.execute(part) + } + } + } + } + + fn count_possible_states(&self) -> u64 { + self.0["in".into()].possible_ranges(self, XmasRanges::all()) + } +} + +#[derive(Debug)] +struct Workflows(HashMap); + +#[derive(Debug)] +struct Part(HashMap); + +impl From<&str> for Part { + fn from(s: &str) -> Self { + let (_, vars_s) = s.split_once('{').unwrap(); + let vars = vars_s.split_once('}').unwrap().0.split(','); + let mut part = HashMap::new(); + for var in vars { + let (name, val) = var.split_once('=').unwrap(); + part.insert(name.chars().next().unwrap(), val.parse().unwrap()); + } + Self(part) + } +} + +type Parts = Vec; + +// PROBLEM 1 solution + +fn problem1(mut input: Lines) -> u64 { + let mut wfs = Workflows(HashMap::new()); + let mut parts: Parts = Vec::new(); + while let Some(Ok(line)) = input.next() { + if line != "" { + let wf: Workflow = line.as_str().into(); + wfs.0.insert(wf.name.clone(), wf); + } else { + break; + } + } + while let Some(Ok(line)) = input.next() { + parts.push(line.as_str().into()); + } + + parts + .iter() + .filter(|part| wfs.execute(part)) + .map(|part| part.0.values().sum::()) + .sum::() +} + +// PROBLEM 2 solution +fn problem2(mut input: Lines) -> u64 { + let mut wfs = Workflows(HashMap::new()); + let mut parts: Parts = Vec::new(); + while let Some(Ok(line)) = input.next() { + if line != "" { + let wf: Workflow = line.as_str().into(); + wfs.0.insert(wf.name.clone(), wf); + } else { + break; + } + } + while let Some(Ok(line)) = input.next() { + parts.push(line.as_str().into()); + } + + wfs.count_possible_states() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + const EXAMPLE: &str = &"px{a<2006:qkq,m>2090:A,rfg} +pv{a>1716:R,A} +lnx{m>1548:A,A} +rfg{s<537:gd,x>2440:R,A} +qs{s>3448:A,lnx} +qkq{x<1416:A,crn} +crn{x>2662:A,R} +in{s<1351:px,qqz} +qqz{s>2770:qs,m<1801:hdj,R} +gd{a>3333:R,R} +hdj{m>838:A,pv} + +{x=787,m=2655,a=1222,s=2876} +{x=1679,m=44,a=2067,s=496} +{x=2036,m=264,a=79,s=2244} +{x=2461,m=1339,a=466,s=291} +{x=2127,m=1623,a=2188,s=1013}"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 19114); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 167409079868000); + } +} diff --git a/2023/2/Cargo.lock b/2023/2/Cargo.lock new file mode 100644 index 0000000..2e7221e --- /dev/null +++ b/2023/2/Cargo.lock @@ -0,0 +1,119 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "day2" +version = "0.1.0" +dependencies = [ + "lazy-regex", + "regex", +] + +[[package]] +name = "lazy-regex" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/2/Cargo.toml b/2023/2/Cargo.toml new file mode 100644 index 0000000..bd45c7c --- /dev/null +++ b/2023/2/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy-regex = "3.1.0" +regex = "1.10.2" diff --git a/2023/2/input b/2023/2/input new file mode 100644 index 0000000..aed79ba --- /dev/null +++ b/2023/2/input @@ -0,0 +1,100 @@ +Game 1: 1 blue; 4 green, 5 blue; 11 red, 3 blue, 11 green; 1 red, 10 green, 4 blue; 17 red, 12 green, 7 blue; 3 blue, 19 green, 15 red +Game 2: 17 red, 10 green; 3 blue, 17 red, 7 green; 10 green, 1 blue, 10 red; 7 green, 15 red, 1 blue; 7 green, 8 blue, 16 red; 18 red, 5 green, 3 blue +Game 3: 10 blue, 3 green, 8 red; 15 green, 14 blue, 1 red; 8 blue, 11 red, 2 green; 5 red, 9 green, 6 blue +Game 4: 1 red, 3 blue; 3 blue, 3 green, 1 red; 11 blue, 2 green; 2 green, 14 blue; 1 green, 7 blue; 11 blue, 5 green +Game 5: 9 green, 5 red, 10 blue; 9 red, 4 blue, 12 green; 9 green, 6 blue, 14 red; 16 green, 8 red, 6 blue; 11 blue, 13 red, 1 green +Game 6: 1 blue, 2 green, 16 red; 1 green, 19 red; 1 blue; 3 blue, 2 red, 1 green; 18 red, 2 blue, 1 green +Game 7: 2 blue, 9 red, 5 green; 11 blue, 6 green, 4 red; 7 red, 3 green, 5 blue; 5 green, 11 blue, 7 red; 17 blue, 4 red, 3 green; 20 blue, 1 green, 2 red +Game 8: 1 green, 6 red, 4 blue; 8 green, 4 blue, 2 red; 2 blue, 5 green +Game 9: 1 green, 5 blue; 4 blue; 2 red, 1 blue +Game 10: 1 green, 12 blue; 6 red, 4 green; 5 green, 14 blue, 9 red; 6 red, 13 blue, 2 green; 6 red, 17 blue, 1 green +Game 11: 19 green, 1 red; 15 red, 7 green; 1 blue, 8 red, 14 green; 1 blue, 11 green, 1 red +Game 12: 4 red, 3 green, 12 blue; 4 green, 13 red; 2 green, 15 blue, 5 red; 5 red, 10 blue, 3 green; 3 green, 17 blue, 17 red; 1 red, 4 green, 15 blue +Game 13: 5 red, 7 blue; 6 red, 1 green, 11 blue; 17 blue, 11 green, 4 red; 6 red, 9 green, 5 blue; 4 green, 14 blue +Game 14: 6 red, 15 blue, 3 green; 4 green, 4 blue, 3 red; 3 blue, 1 green, 5 red +Game 15: 4 green, 3 blue, 6 red; 3 blue, 2 red, 4 green; 15 red, 3 green, 4 blue; 11 red, 2 blue, 1 green +Game 16: 4 green, 1 blue, 12 red; 10 green, 6 blue, 10 red; 1 blue, 2 green, 15 red; 1 green, 3 red, 4 blue +Game 17: 10 green, 11 blue; 13 green, 10 blue, 3 red; 8 red, 3 green, 10 blue +Game 18: 3 red; 4 red, 1 blue; 3 green, 3 red; 10 green, 1 blue; 4 red, 6 green, 1 blue; 3 green +Game 19: 4 red, 6 green; 11 red, 4 blue, 2 green; 4 green, 8 red; 9 red, 7 green, 2 blue; 13 red, 4 blue; 9 red +Game 20: 2 blue, 1 green, 8 red; 3 green, 11 blue, 1 red; 5 blue, 4 red, 6 green; 6 green, 7 red, 5 blue; 7 red, 2 green, 1 blue +Game 21: 7 blue, 3 green; 2 green, 2 red, 7 blue; 6 blue, 3 red, 1 green; 9 blue, 1 green, 3 red +Game 22: 7 red, 5 blue, 9 green; 6 red, 1 blue, 5 green; 18 red, 7 green, 5 blue; 13 red, 11 green, 1 blue; 13 red, 1 blue, 11 green +Game 23: 10 green, 1 blue, 5 red; 2 red, 4 green; 9 green, 2 red; 10 green, 1 blue, 5 red +Game 24: 2 red, 6 green, 16 blue; 3 blue, 12 red, 3 green; 6 blue, 4 red, 12 green; 12 green, 14 blue, 3 red +Game 25: 5 red, 2 blue, 1 green; 4 blue, 14 red, 2 green; 16 blue, 4 red; 5 red; 12 blue, 16 red; 2 red, 6 blue, 1 green +Game 26: 10 blue, 6 green, 5 red; 2 red, 2 green; 8 blue, 5 red; 7 blue, 11 green; 8 green, 9 blue +Game 27: 1 red, 2 green; 6 green, 10 blue; 1 red, 6 green, 11 blue; 4 blue, 1 green, 1 red; 1 red, 7 blue; 5 green, 2 blue, 1 red +Game 28: 6 blue, 5 red, 3 green; 5 blue, 1 green; 1 green, 8 red, 1 blue; 2 blue, 4 green; 4 red, 5 blue +Game 29: 2 red, 4 green, 6 blue; 6 blue, 2 green, 1 red; 10 green, 13 blue, 2 red; 11 green, 11 blue, 2 red; 5 red, 10 green +Game 30: 4 green, 4 blue, 5 red; 6 red, 18 green, 7 blue; 1 red, 11 green, 2 blue; 11 red, 2 blue, 1 green +Game 31: 1 red, 7 green, 2 blue; 5 red, 6 green, 9 blue; 3 green, 1 red, 9 blue; 3 red; 8 blue, 6 green, 1 red; 14 blue, 4 green, 7 red +Game 32: 11 red, 2 blue, 2 green; 18 blue, 1 green; 6 blue, 1 green, 15 red; 4 red, 2 green; 13 red, 6 green, 5 blue; 7 blue, 5 green +Game 33: 12 blue, 2 red, 2 green; 5 blue, 3 green; 2 green, 6 red, 4 blue; 12 blue, 13 red; 10 blue, 12 red, 1 green; 4 blue, 4 red +Game 34: 2 blue, 1 red, 6 green; 2 blue, 1 green; 1 red, 2 green, 2 blue; 17 red, 1 blue, 2 green; 4 green, 2 red +Game 35: 2 blue, 12 red, 2 green; 2 green, 8 red, 11 blue; 17 red, 16 blue; 8 blue, 8 red; 1 green, 6 red, 9 blue +Game 36: 6 red, 14 green, 7 blue; 1 green, 1 red, 6 blue; 9 red, 10 blue, 9 green; 11 blue, 14 green, 8 red; 10 red, 6 green, 3 blue +Game 37: 6 green; 2 red, 5 green; 4 blue, 4 red, 5 green; 1 red, 7 green, 4 blue +Game 38: 1 green, 8 red, 10 blue; 3 green, 5 red, 12 blue; 13 blue, 5 red, 1 green; 3 green, 1 red; 3 red, 15 blue, 6 green; 3 green, 6 red, 4 blue +Game 39: 15 green, 3 blue; 3 green; 3 blue, 3 red, 8 green; 4 blue, 10 green, 4 red; 2 green, 5 blue, 4 red; 3 green, 3 blue, 1 red +Game 40: 2 red, 3 green, 15 blue; 5 blue, 13 green, 2 red; 11 blue, 1 red; 5 blue, 20 green, 5 red +Game 41: 1 red, 2 green, 1 blue; 10 red; 11 green, 1 blue, 5 red; 10 red, 13 green +Game 42: 10 red, 8 green, 2 blue; 5 green, 4 red; 6 red; 5 red, 9 green, 2 blue; 2 blue, 2 red, 3 green; 1 blue, 7 red, 2 green +Game 43: 8 red, 6 blue, 12 green; 11 green, 2 red, 2 blue; 4 blue, 3 red, 8 green; 14 red, 3 blue; 9 green, 1 blue, 5 red +Game 44: 4 red, 5 blue, 4 green; 9 blue, 1 green; 10 green, 2 blue, 4 red; 5 red, 15 green, 12 blue +Game 45: 1 red, 2 green; 5 blue; 3 blue, 1 red; 1 blue; 4 green, 1 red +Game 46: 3 green, 8 red, 5 blue; 1 blue, 10 red, 5 green; 2 green, 5 red, 3 blue; 5 green, 4 red, 13 blue +Game 47: 2 green, 1 blue; 2 red, 2 green, 6 blue; 1 green, 1 red, 1 blue; 2 green, 4 blue, 4 red; 2 green, 6 blue, 3 red +Game 48: 4 blue, 1 green, 2 red; 6 blue, 1 red; 4 blue +Game 49: 16 blue, 18 green, 13 red; 7 red, 13 blue; 14 green, 12 red; 20 green, 14 red, 12 blue +Game 50: 8 blue, 2 red, 1 green; 4 red, 2 green, 5 blue; 6 blue, 2 green; 8 blue, 3 green, 5 red; 1 green, 4 blue +Game 51: 2 blue, 8 red; 2 green, 3 red, 2 blue; 12 green; 3 blue, 10 green, 1 red; 13 green, 3 blue, 5 red +Game 52: 12 blue, 5 red, 16 green; 4 blue, 13 green; 2 red, 5 blue, 5 green; 4 green, 8 blue, 3 red +Game 53: 7 blue, 5 green; 8 green, 8 blue; 4 red, 1 green; 8 blue, 5 red, 11 green +Game 54: 4 blue, 3 green, 1 red; 10 green, 4 red, 7 blue; 1 red, 6 blue, 2 green +Game 55: 2 red, 10 blue; 13 red, 10 blue; 4 red, 5 blue, 4 green; 3 green, 8 blue; 5 blue, 7 red, 2 green +Game 56: 6 green, 7 blue, 9 red; 3 blue, 4 green; 15 red, 3 blue, 3 green; 5 red, 8 green, 7 blue +Game 57: 9 red, 13 blue, 2 green; 3 red, 2 green, 3 blue; 1 blue, 5 red, 3 green; 9 blue, 2 green, 2 red; 3 red, 12 blue, 3 green; 2 green, 7 red +Game 58: 4 blue, 7 red; 2 blue, 5 green, 1 red; 17 blue, 3 red; 5 green, 1 blue, 4 red; 4 green, 14 blue, 3 red +Game 59: 5 blue, 4 red; 7 red; 4 blue, 4 red, 1 green; 1 green, 1 blue, 5 red +Game 60: 4 red, 2 green; 1 green, 2 blue; 2 red, 2 green; 4 red, 2 green; 3 red +Game 61: 6 red, 14 green; 12 green, 3 blue; 3 blue, 16 green, 11 red +Game 62: 12 green, 2 red, 1 blue; 14 red, 2 blue, 4 green; 7 red, 5 green, 1 blue; 4 green, 14 red, 1 blue; 2 blue, 7 green, 3 red; 14 red, 9 green, 2 blue +Game 63: 1 red, 8 blue, 12 green; 3 green, 10 blue, 4 red; 2 red, 1 blue, 2 green; 2 red, 8 green; 4 red, 3 blue, 9 green; 13 green, 4 blue +Game 64: 4 blue, 9 green, 4 red; 10 green, 6 blue, 7 red; 10 green, 8 red, 1 blue; 13 blue, 8 green, 8 red; 4 green, 1 red, 8 blue +Game 65: 16 blue, 5 red, 13 green; 5 red, 9 green, 10 blue; 2 green, 14 red; 6 red, 5 green, 5 blue; 19 blue, 4 green, 14 red; 7 red, 1 blue, 4 green +Game 66: 7 blue, 11 green, 5 red; 7 green, 8 red, 10 blue; 3 red, 1 green +Game 67: 9 green, 2 blue, 13 red; 11 red, 10 blue; 14 red, 1 green, 1 blue; 1 red, 13 green, 6 blue; 7 blue, 3 green, 5 red; 3 green, 2 blue +Game 68: 6 green, 2 blue; 1 blue, 3 red, 5 green; 1 blue, 10 green +Game 69: 4 red, 6 blue, 1 green; 6 blue, 2 red, 4 green; 9 green, 7 blue, 2 red +Game 70: 11 blue, 1 green, 4 red; 7 blue, 1 green; 6 red, 9 blue; 1 green, 3 red, 7 blue; 1 green, 9 blue, 2 red +Game 71: 2 green, 6 red; 1 blue, 2 green, 15 red; 6 red, 1 blue; 2 green, 5 red +Game 72: 5 green, 6 red; 8 red, 1 blue, 12 green; 1 blue, 9 red, 15 green; 11 green, 1 blue, 6 red; 14 green, 5 red +Game 73: 9 green, 13 red, 1 blue; 14 green, 5 blue, 13 red; 13 green, 10 red, 16 blue; 3 blue, 7 red, 1 green; 7 red, 6 green, 11 blue +Game 74: 1 blue, 9 red, 15 green; 3 blue, 7 green; 9 green, 1 blue, 7 red +Game 75: 5 blue, 12 red, 2 green; 2 blue, 2 green; 4 green, 2 red, 7 blue; 4 green, 7 blue, 5 red; 1 green, 7 blue, 3 red +Game 76: 1 blue; 8 red, 13 blue; 4 green, 7 blue, 7 red; 3 red, 12 blue, 2 green; 2 green, 2 blue, 1 red +Game 77: 4 blue; 4 blue; 1 blue, 1 red; 1 red, 5 blue; 10 blue, 1 green +Game 78: 3 blue, 10 green, 6 red; 12 red, 7 blue, 8 green; 2 green, 18 red, 5 blue; 2 red, 15 blue, 14 green; 4 green, 6 blue, 13 red +Game 79: 7 red, 1 green; 1 blue, 6 red, 2 green; 1 blue, 12 red +Game 80: 4 red, 6 blue, 2 green; 5 blue, 2 red; 6 blue, 7 red, 2 green +Game 81: 2 green, 9 red; 2 green, 2 blue, 7 red; 12 red, 7 green; 8 green, 3 red, 3 blue +Game 82: 4 green, 5 blue; 2 red, 16 blue; 2 red, 2 green, 18 blue +Game 83: 14 red, 2 green; 3 blue, 16 red, 2 green; 4 green, 13 red, 1 blue +Game 84: 10 green, 6 blue, 2 red; 5 red, 6 blue; 7 green, 6 red, 9 blue +Game 85: 1 red; 12 red, 1 blue, 2 green; 6 red, 1 green; 12 red, 2 green +Game 86: 14 red, 1 green, 3 blue; 3 blue; 4 green, 8 red, 2 blue; 10 red, 2 green +Game 87: 4 red, 9 green, 8 blue; 3 green, 6 blue, 7 red; 4 blue, 1 red; 2 red, 7 blue, 11 green; 8 green, 2 blue, 5 red; 6 blue, 10 green, 8 red +Game 88: 1 green, 1 red; 2 green, 1 blue; 3 green, 1 red, 1 blue; 4 green; 1 blue, 3 green, 1 red +Game 89: 11 green, 7 blue, 8 red; 7 blue, 3 green, 2 red; 7 green, 6 red, 4 blue; 1 blue, 2 green, 10 red; 3 red, 2 blue, 1 green +Game 90: 10 green, 12 red, 2 blue; 7 red, 7 blue, 8 green; 2 blue, 11 red, 7 green; 6 green, 5 red, 2 blue; 7 red, 10 green +Game 91: 12 red, 8 green; 8 red, 6 green, 3 blue; 12 red, 4 blue, 2 green +Game 92: 10 blue, 3 green; 4 red, 13 blue, 8 green; 7 green, 8 blue, 7 red +Game 93: 7 red, 4 green, 1 blue; 15 green, 4 red; 2 blue, 15 red +Game 94: 10 red, 10 green, 11 blue; 3 red, 1 green, 7 blue; 9 red, 4 green, 9 blue; 7 red, 9 green, 13 blue; 9 blue, 2 green, 10 red +Game 95: 4 blue, 12 green; 7 green, 1 red, 5 blue; 2 blue, 8 green, 8 red +Game 96: 12 green, 2 blue; 11 green, 3 blue; 3 red, 2 green, 5 blue; 12 green, 2 blue, 4 red; 2 blue, 1 green, 1 red; 3 red, 11 green, 3 blue +Game 97: 6 red, 3 blue, 1 green; 1 blue, 2 red, 14 green; 4 blue, 14 green +Game 98: 13 green, 1 red, 5 blue; 2 red, 5 green, 7 blue; 19 green, 5 blue; 4 blue, 13 green; 5 green, 8 blue +Game 99: 11 red, 8 green; 16 red, 10 green; 9 red, 6 green; 3 blue, 2 red, 4 green +Game 100: 4 red, 2 blue, 4 green; 2 green, 1 red, 1 blue; 3 green, 4 blue, 5 red; 18 red, 2 blue; 9 red, 5 green, 4 blue diff --git a/2023/2/src/main.rs b/2023/2/src/main.rs new file mode 100644 index 0000000..f4f56b8 --- /dev/null +++ b/2023/2/src/main.rs @@ -0,0 +1,215 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +use lazy_regex::{lazy_regex, Lazy}; +use regex::Regex; + +// --- Day 2: Cube Conundrum --- + +// You're launched high into the atmosphere! The apex of your trajectory just barely +// reaches the surface of a large island floating in the sky. You gently land in a +// fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over +// to greet you. + +// The Elf explains that you've arrived at Snow Island and apologizes for the lack of +// snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have +// some time. They don't get many visitors up here; would you like to play a game in the +// meantime? + +// As you walk, the Elf shows you a small bag and some cubes which are either red, +// green, or blue. Each time you play this game, he will hide a secret number of cubes +// of each color in the bag, and your goal is to figure out information about the number +// of cubes. + +// To get information, once a bag has been loaded with cubes, the Elf will reach into +// the bag, grab a handful of random cubes, show them to you, and then put them back in +// the bag. He'll do this a few times per game. + +// You play several games and record the information from each game (your puzzle input). +// Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a +// semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 +// red, 5 green, 4 blue). + +// For example, the record of a few games might look like this: + +// Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue, 2 green; 3 +// green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, +// 13 green; 5 green, 1 red Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 +// blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green + +// In game 1, three sets of cubes are revealed from the bag (and then put back again). +// The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green +// cubes, and 6 blue cubes; the third set is only 2 green cubes. + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSING + +struct CubeCount { + blue: u64, + red: u64, + green: u64, +} + +impl Default for CubeCount { + fn default() -> Self { + Self { + blue: 0, + red: 0, + green: 0, + } + } +} + +impl From<&str> for CubeCount { + fn from(s: &str) -> Self { + let mut result = Self::default(); + let counts = s.split(", "); + for count in counts { + let (n, color) = count.split_once(' ').unwrap(); + match color { + "blue" => result.blue = n.parse().unwrap(), + "red" => result.red = n.parse().unwrap(), + "green" => result.green = n.parse().unwrap(), + _ => panic!("bad colour {}", color), + } + } + + result + } +} + +pub static GAME_ID_REGEX: Lazy = lazy_regex!(r"^Game ([0-9]+)"); + +struct GameResult { + id: u64, + results: Vec, +} + +impl From<&str> for GameResult { + fn from(s: &str) -> Self { + let mut results = Vec::new(); + let (s_id, rest) = s.split_once(": ").unwrap(); + let id: u64 = GAME_ID_REGEX + .captures(s_id) + .unwrap() + .get(1) + .unwrap() + .as_str() + .parse() + .unwrap(); + + let s_results = rest.split("; "); + + for s_result in s_results { + results.push(CubeCount::from(s_result)); + } + + GameResult { id, results } + } +} + +// PROBLEM 1 solution + +// The Elf would first like to know which games would have been possible if the bag +// contained only 12 red cubes, 13 green cubes, and 14 blue cubes? + +// In the example above, games 1, 2, and 5 would have been possible if the bag had been +// loaded with that configuration. However, game 3 would have been impossible because at +// one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have +// been impossible because the Elf showed you 15 blue cubes at once. If you add up the +// IDs of the games that would have been possible, you get 8. + +// Determine which games would have been possible if the bag had been loaded with only +// 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those +// games? + +// possible criteria: 12 red cubes, 13 green cubes, and 14 blue cubes + +const PROBLEM1_MINIMUM: CubeCount = CubeCount { + red: 12, + green: 13, + blue: 14, +}; + +fn problem1_result_possible(game: &GameResult) -> bool { + !game.results.iter().any(|result| { + result.red > PROBLEM1_MINIMUM.red + || result.green > PROBLEM1_MINIMUM.green + || result.blue > PROBLEM1_MINIMUM.blue + }) +} + +fn problem1(input: InputIter) -> u64 { + let mut n = 0u64; + for line in input { + let game_result = GameResult::from(line.unwrap().as_str()); + if problem1_result_possible(&game_result) { + n += game_result.id; + } + } + + n +} + +// PROBLEM 2 solution + +// --- Part Two --- + +// The Elf says they've stopped producing snow because they aren't getting any water! He +// isn't sure why the water stopped; however, he can show you how to get to the water +// source to check it out for yourself. It's just up ahead! + +// As you continue your walk, the Elf poses a second question: in each game you played, +// what is the fewest number of cubes of each color that could have been in the bag to +// make the game possible? + +// Again consider the example games from earlier: + +// Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue, 2 green; 3 +// green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, +// 13 green; 5 green, 1 red Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 +// blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green + +// In game 1, the game could have been played with as few as 4 red, 2 green, and 6 +// blue cubes. If any color had even one fewer cube, the game would have been +// impossible. Game 2 could have been played with a minimum of 1 red, 3 green, and 4 +// blue cubes. Game 3 must have been played with at least 20 red, 13 green, and 6 +// blue cubes. Game 4 required at least 14 red, 3 green, and 15 blue cubes. Game 5 +// needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag. + +// The power of a set of cubes is equal to the numbers of red, green, and blue cubes +// multiplied together. The power of the minimum set of cubes in game 1 is 48. In games +// 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these five powers produces +// the sum 2286. + +// For each game, find the minimum set of cubes that must have been present. What is the +// sum of the power of these sets? + + +// Minimum power = maximum seen of each colour across all games, r * g * b +fn problem2_game_minimum_power(game: &GameResult) -> u64 { + // Could be done in one pass, but this is cleaner using Iter methods + let max_r = game.results.iter().map(|c| c.red).max().unwrap(); + let max_g = game.results.iter().map(|c| c.green).max().unwrap(); + let max_b = game.results.iter().map(|c| c.blue).max().unwrap(); + + max_r * max_g * max_b +} + +fn problem2(input: InputIter) -> u64 { + let games = input.map(|l| GameResult::from(l.unwrap().as_str())); + games.fold(0u64, |accum, val| accum + problem2_game_minimum_power(&val)) +} diff --git a/2023/20/Cargo.lock b/2023/20/Cargo.lock new file mode 100644 index 0000000..698d501 --- /dev/null +++ b/2023/20/Cargo.lock @@ -0,0 +1,201 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day20" +version = "0.1.0" +dependencies = [ + "lazy-regex", + "num", +] + +[[package]] +name = "lazy-regex" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/20/Cargo.toml b/2023/20/Cargo.toml new file mode 100644 index 0000000..b05b468 --- /dev/null +++ b/2023/20/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day20" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy-regex = "3.1.0" +num = "0.4.1" diff --git a/2023/20/input b/2023/20/input new file mode 100644 index 0000000..71d3d84 --- /dev/null +++ b/2023/20/input @@ -0,0 +1,58 @@ +%hm -> cr +%qc -> nd +&dh -> rm +%ph -> zz +%ps -> kc, dt +%qb -> dt +%jl -> vt, tb +%fh -> dm, gr +broadcaster -> np, mg, vd, xr +%zz -> sq +&rm -> rx +%nd -> br +%nx -> vr, vt +%qf -> dt, dv +%np -> xm, ph +%dm -> nf, gr +%sq -> kj +%bv -> fp, xm +%br -> kt +%mg -> dz, gr +&dt -> vd, dv, dh, hm, ks, hd, kq +%ks -> qf +&qd -> rm +%xr -> vt, rn +%vr -> tg, vt +%lc -> xm +%tq -> gr, fh +%cr -> kq, dt +%vd -> dt, ks +%tb -> nx +%dz -> gr, fd +&gr -> dp, mg, fd, qn +%nf -> gr +%dv -> hm +%qj -> lc, xm +%kc -> dt, gf +%gf -> dt, qb +%vh -> xm, sv +%sr -> vt +%fp -> qg, xm +%kj -> vh +%pc -> tq, gr +%kq -> hd +%xd -> xg, gr +%tg -> sr, vt +&bb -> rm +%rn -> vt, qc +%hd -> ps +%qg -> xm, qj +&dp -> rm +%qn -> pc +%kt -> jl +%sv -> bv +&vt -> bb, nd, qc, xr, br, tb, kt +%fd -> mx +&xm -> zz, sv, sq, ph, kj, np, qd +%xg -> gr, qn +%mx -> gr, xd diff --git a/2023/20/src/main.rs b/2023/20/src/main.rs new file mode 100644 index 0000000..790327a --- /dev/null +++ b/2023/20/src/main.rs @@ -0,0 +1,362 @@ +use std::collections::{BinaryHeap, HashMap, VecDeque}; +use std::fmt::Display; +use std::fs::File; +use std::hash::{Hash, Hasher}; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +use lazy_regex::lazy_regex; +use num::integer::lcm; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +const MODULE_PATTERN: lazy_regex::Lazy = lazy_regex!("^([%&]?)([a-z]+) -> ([a-z, ]+)$"); + +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] +enum PulseType { + Low, + High, +} + +impl From for PulseType { + fn from(value: bool) -> Self { + match value { + true => PulseType::High, + false => PulseType::Low, + } + } +} + +impl From for bool { + fn from(value: PulseType) -> Self { + match value { + PulseType::High => true, + PulseType::Low => false, + } + } +} + +impl Display for PulseType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PulseType::Low => f.write_str("low"), + PulseType::High => f.write_str("high"), + } + } +} + +#[derive(Debug, Clone, Copy)] +enum ModuleKind { + FlipFlop, + Conjunction, + Broadcast, + Button, +} + +#[derive(Debug, Clone)] +struct Module { + name: String, + kind: Option, + inputs: Vec, + outputs: Vec, + on: bool, + last_input_states: Vec, +} + +impl Hash for Module { + fn hash(&self, state: &mut H) { + // the only states that change are 'on' and 'last_input_states', the rest are constant once created + self.on.hash(state); + self.last_input_states.hash(state); + } +} + +impl Module { + fn new(name: String) -> Self { + Self { + name, + kind: None, + inputs: Vec::new(), + outputs: Vec::new(), + on: false, + last_input_states: Vec::new(), + } + } + fn source_idx(&self, name: &str) -> usize { + self.inputs.iter().position(|in_name| in_name == name).unwrap() + } + fn with_kind(name: String, kind: ModuleKind) -> Self { + let mut m = Self::new(name); + m.kind = Some(kind); + m + } + fn and_destinations(mut self, destinations: &str) -> Self { + for dest in destinations.split(", ") { + self.outputs.push(dest.into()); + } + self + } + fn and_source(mut self, source: String) -> Self { + self.add_source(source); + self + } + fn add_source(&mut self, source: String) { + self.inputs.push(source); + self.last_input_states.push(PulseType::Low); + } + fn reset(&mut self) { + // relevant state is on and last_input_states + self.on = false; + for input in &mut self.last_input_states { + *input = PulseType::Low; + } + } + fn state_space(&self) -> u128 { + match self.kind { + Some(ModuleKind::Broadcast) | Some(ModuleKind::Button) => 1, + Some(ModuleKind::FlipFlop) => 2, + Some(ModuleKind::Conjunction) => 1 << self.last_input_states.len(), // 2^n + None => 1 + } + } +} + +impl From<&str> for Module { + fn from(s: &str) -> Self { + let Some(re_result) = MODULE_PATTERN.captures(s) else { + panic!("unparseable module: {}", s); + }; + let (_, [kind, name, destinations]) = re_result.extract(); + if name == "broadcaster" { + return Module::with_kind(name.into(), ModuleKind::Broadcast).and_destinations(destinations); + }; + match kind { + "%" => Module::with_kind(name.into(), ModuleKind::FlipFlop).and_destinations(destinations), + "&" => Module::with_kind(name.into(), ModuleKind::Conjunction).and_destinations(destinations), + _ => panic!("invalid module kind {}", kind), + } + } +} + +#[derive(Debug)] +struct Job { + signal: PulseType, + targets: Vec, + from: String, + priority: usize, +} + +impl PartialEq for Job { + fn eq(&self, other: &Self) -> bool { + self.priority == other.priority + } +} +impl Eq for Job {} + +impl PartialOrd for Job { + fn partial_cmp(&self, other: &Self) -> Option { + other.priority.partial_cmp(&self.priority) + } +} + +impl Ord for Job { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.partial_cmp(other).unwrap() + } +} + +#[derive(Debug)] +struct Machine { + modules: HashMap, + work_queue: BinaryHeap, + count_low: u64, + count_high: u64, +} + +impl From> for Machine { + fn from(mut lines: Lines) -> Self { + let mut modules = HashMap::from([( + "button".into(), + Module::with_kind("button".into(), ModuleKind::Button).and_destinations("broadcaster"), + )]); + while let Some(Ok(line)) = lines.next() { + let mut module = Module::from(line.as_str()); + for output in &module.outputs { + if let Some(output_mod) = modules.get_mut(output) { + // already exists, push to input list + output_mod.add_source(module.name.to_owned()); + } else { + // forward declaration of 'unknown' modules + modules.insert( + output.clone(), + Module::new(output.clone()).and_source(module.name.clone()), + ); + } + } + if let Some(existing_mod) = modules.get(&module.name) { + module.inputs = existing_mod.inputs.clone(); // might have been pre-prepared, the rest we take ours + module.last_input_states = existing_mod.last_input_states.clone(); + } + modules.insert(module.name.clone(), module); + } + + Machine { + modules, + work_queue: BinaryHeap::new(), + count_low: 0, + count_high: 0, + } + } +} + +impl Machine { + fn press_button(&mut self) { + self.send_pulse(PulseType::Low, "broadcaster", "button", 1); + } + fn send_pulse(&mut self, signal: PulseType, target: &str, from: &str, priority: usize) { + // count pulse when it is received + match signal { + PulseType::Low => self.count_low += 1, + PulseType::High => self.count_high += 1, + }; + match self.modules[target].kind { + Some(ModuleKind::Button) | Some(ModuleKind::Broadcast) => { + self.send_all_outputs(PulseType::Low, target, priority) + } + Some(ModuleKind::FlipFlop) => match signal { + PulseType::High => (), + PulseType::Low => { + let new_state = !self.modules.get_mut(target).unwrap().on; + self.modules.get_mut(target).unwrap().on = new_state; + self.send_all_outputs(new_state.into(), target, priority) + } + }, + Some(ModuleKind::Conjunction) => { + let target_m = self.modules.get_mut(target).unwrap(); + let source_idx = target_m.source_idx(from); + target_m.last_input_states[source_idx] = signal; + if target_m.last_input_states.iter().all(|state| *state == PulseType::High) { + self.send_all_outputs(PulseType::Low, target, priority) + } else { + self.send_all_outputs(PulseType::High, target, priority) + } + } + None => (), + } + } + fn send_all_outputs(&mut self, signal: PulseType, from: &str, priority: usize) { + self.work_queue.push(Job { + signal, + targets: self.modules[from].outputs.iter().map(|op| op.to_owned()).collect(), + from: from.to_owned(), + priority: priority + 1, + }) + } + fn run(&mut self) { + while let Some(job) = self.work_queue.pop() { + for target in job.targets { + self.send_pulse(job.signal, &target, &job.from, job.priority); + } + } + } + // the number of cycles it took until the target sends a low pulse, or None if the target node sent no low pulses but the job finished + fn time_to_state(&mut self, goal:&str, state: PulseType) -> Option { + while let Some(job) = self.work_queue.pop() { + if job.from == goal && job.signal == state { + return Some(self.count_high + self.count_low); + } + for target in job.targets { + self.send_pulse(job.signal, &target, &job.from, job.priority); + } + } + None + } + fn reset(&mut self) { + for (_name, m) in &mut self.modules { + m.reset(); + } + self.count_low = 0; + self.count_high = 0; + } + fn state_space(&self) -> u128 { + self.modules.values().map(|m| m.state_space()).product() + } +} + +// PROBLEM 1 solution +const PROBLEM1_ITERATIONS: usize = 1000; +fn problem1(input: Lines) -> u64 { + let mut machine = Machine::from(input); + for _ in 0..PROBLEM1_ITERATIONS { + machine.press_button(); + machine.run(); + } + machine.count_low * machine.count_high +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u128 { + let mut machine = Machine::from(input); + println!("STATES: {}", machine.state_space()); + // Find the rx module and look at its parent(s) + let rx = &machine.modules["rx"]; + // my input has a single conjunction as parent, todo implement other possibilities + let con = &machine.modules[&rx.inputs[0]]; + + // for each input, find how long it takes for it to be High, so the conjunction sends a low to rx + let mut cycles: Vec<_> = Vec::new(); + for input in con.inputs.clone() { + print!("searching distance to {}...", input); + machine.reset(); + let mut button_count = 0; + loop { + machine.press_button(); + button_count += 1; + if let Some(distance) = machine.time_to_state(&input, PulseType::High) { + println!("got {} pulses, {} button presses", distance, button_count); + cycles.push(button_count as u128); + break; + } + } + } + cycles.iter().fold(1, |accum, cycle| lcm(accum, *cycle)) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"broadcaster -> a +%a -> inv, con +&inv -> b +%b -> con +&con -> output"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 11687500); + } +} diff --git a/2023/21/Cargo.lock b/2023/21/Cargo.lock new file mode 100644 index 0000000..2e61ea1 --- /dev/null +++ b/2023/21/Cargo.lock @@ -0,0 +1,465 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bytemuck" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fca89a0e215bab21874660c67903c5f143333cab1da83d041c7ded6053774751" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2fe95351b870527a5d09bf563ed3c97c0cffb87cf1c78a591bf48bb218d9aa" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d96137f14f244c37f989d9fff8f95e6c18b918e71f36638f8c49112e4c78f" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "day21" +version = "0.1.0" +dependencies = [ + "indicatif", + "polyfit-rs", + "primes", + "rayon", + "test-case", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] +name = "indicatif" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "matrixmultiply" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +dependencies = [ + "autocfg", + "rawpointer", +] + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "nalgebra" +version = "0.31.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20bd243ab3dbb395b39ee730402d2e5405e448c75133ec49cc977762c4cba3d1" +dependencies = [ + "approx", + "matrixmultiply", + "nalgebra-macros", + "num-complex", + "num-rational", + "num-traits", + "simba", + "typenum", +] + +[[package]] +name = "nalgebra-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + +[[package]] +name = "polyfit-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab98d9704c7300e37472a6790a447eaf501d664a1889737faa53c26790d2b697" +dependencies = [ + "nalgebra", +] + +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + +[[package]] +name = "primes" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a61082d8bceecd71a3870e9162002bb75f7ba9c7aa8b76227e887782fef9c8" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "safe_arch" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f398075ce1e6a179b46f51bd88d0598b92b00d3551f1a2d4ac49e771b56ac354" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "simba" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f3fd720c48c53cace224ae62bef1bbff363a70c68c4802a78b5cc6159618176" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", + "wide", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "test-case" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" +dependencies = [ + "test-case-macros", +] + +[[package]] +name = "test-case-core" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn 2.0.42", +] + +[[package]] +name = "test-case-macros" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.42", + "test-case-core", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] +name = "wide" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c68938b57b33da363195412cfc5fc37c9ed49aa9cfe2156fde64b8d2c9498242" +dependencies = [ + "bytemuck", + "safe_arch", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/2023/21/Cargo.toml b/2023/21/Cargo.toml new file mode 100644 index 0000000..2bc0ee8 --- /dev/null +++ b/2023/21/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "day21" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +indicatif = "0.17.7" +polyfit-rs = "0.2.1" +primes = "0.3.0" +rayon = "1.8.0" +test-case = "3.3.1" diff --git a/2023/21/input b/2023/21/input new file mode 100644 index 0000000..b7f57f9 --- /dev/null +++ b/2023/21/input @@ -0,0 +1,131 @@ +................................................................................................................................... +..............#...#........#...............#.#.......#....##...........#.#..#......#.....#.........#..##........................... +.#..##..##..#......#.#.#...##......###...##......#.#..#...................#.....#..........#..#................#...#..........#.... +.....................#..#.......#.....##.........#.......................................#........#....#.#..........#.#......#.#... +......##...#.....#.......#...#..###..........#..........#......................#........#.............#.....#........#....#........ +.................##.................#...#.........#................................#........#...........#............#........#.... +..............#.#....#........##.....##..........#.............##.............#...#......#..##..........#.........#.....#...#..#... +..#....#.......#...##..#......#.........#.#..#....#.#..............#........#...#...#...............#.#.....#..#..##......#...#.... +...#......##...##..#...#..........#...#..........#..............#...#..........#..#..#..#........#.#.#.#.#...........###........... +.#.#.#...#.#...##............................................#......................#...#......#......##.#......................... +.##.#.........#...#..................##...........#............#..................###...#...........#.................#....#....... +.....................#...#.#................#................#.#........................#...#...##....##....#.#...#.........#...#.. +...#.......#...#.........................#.#..................#........##..........................#......##....##............#.... +......................#.....................#...............#.......#.#...................#...............#...#........#.##......#. +............................#..........................#........#....#.##..#.......#.##..#...#.#..#...............#.#..#....#...... +.......................................................#.#..#.#....#......................#............#....#..#.......#.........#. +.....##........#...............#.......#...............#.#..........##..#...................#................#..#...#.#....#...###. +....#.....................##................#.......#.......#...#.#...#...............#................#...............##.......... +....#........#..#.......#.#.#........................#.#...........#.......#..#.........#....#.......#........#.#.#................ +......###.....#..#...........#.....#..#..#........#......#.#........###.....##............#..#...........#......................... +.....#...........#...................#..#..........#..#...###.#.........#.##...............#..##..........#.........#.............. +.#.........#....#............#.##..........................##.....#........#....#.............#.......#...#..............##.#...... +.#..#...#.......#...#.......##.......#.#.............................#...#...#.#...........#...#.................#.........#.#.#... +...#....................#.....##..............#.................#.................#............#......#....#.......#.........#..... +.......#...#.............#.##...#.#...................#.....#..#........#................................#.............#..#...##... +...#.###..............#....#........................#......#...#.........#......#.#....................#........#...#...#.......#.. +............#.#...#...........................#....###.###...#........#..#........#...................##.....###......#........#... +.....#...#................#.##..##..................#....#.........#............##...................##................#.....#..... +.#....#..............##...#..................#......#..........##.#....#......##.#...............#....##..............#....#....... +....#.#.##...........##.....#...........#.........#...#........#..#.......#......#................#.#...#....#...#................. +.......#...#...#...#..#.......#..........#..................#..#........#..#.#.....#.#.....................##...................... +.....##.........#.......##...#.............#.##.#.#...#.##.#...#......#...#....#.......#..#.........#...........#.##.###.#.....#... +...................#.#.......................#.....#.#......#.....##...#.#....#..#..........#...........#..##....#...#.......#.#... +.....#..#..#.........#...#.#.........#...#.#...##...#......##........#...............#.......#................#.....#...#.#........ +........#.....###.....................#.........#.........#.........#....##............#..##.............#.........#..#.......#.... +.#.........#....##..##.................#...#................................#...#.....#.........#.......#............#....#......#. +..#.........#.......#..................#..#.#...#........#.#.......#...##.#........#..........#.#..........#..#.....#.........#.... +..#...............#...................##.#.......#...#..........#....................##.........#.................#..#......##..#.. +.#........#.......#............##...........##........#.......................#................#....................#...###......#. +........###..........#..............#.............#..#.#.#............##...............#........#...#........#...................#. +...#............................#...##.##........#......#..#...................#.#.....#...#.##...............#........#...#....... +..#...##....................#.....##......#.......#..........#......#.........#...#..............#..............#.................. +.....#..#....................#....###.....#..#.............#...#....#................#.....#.....................#........#........ +.....#.#...#.....#..............##............#....##.#....#.#........##...........#....#.........................#....#......##... +....#..#.....##...........#.#.#.#............#....#.....#.........#........................##.......#.#..............##...#........ +...............#..............#................................#....#........#.......#.......##......#.#............#......#....#.. +.#......#....##........#.............#..#.#.............#...#..#.........#.........#................#..................#........... +.........#..............#....#......#.....##......#............#....#.........#....#...................#....#.........#......#..#.. +..............................#....#.#........#.#.....#..#.................#....#...#..........#............##.......#.#.#....#.... +.#..................#....#..#.......#.##..............#...#.......#................#............................................... +.#..##.........................#.....###......#....#......#.....#............#.#......##......##......##....#..........##.....#.... +..........................##.#.....#..........................................#.#..#....#..#..................#...........#........ +..##.....#.......#....#.........#..........#....................#.........#........###........................#..........#......... +.#.#...............#..........#.......#.....#....##.#..#.##...............#...#...#.#......#.......#.......#.#.............#....##. +..................#...............##.........##..#........#..#..........#.....................#............................#....... +.....#................#...##.......##.........#.....#.#....#.#........#...#..#....#..............#..#....#....#....#............... +..##.#........#...............#.#..#...................##...#......#.#...#..#.....##.................##...#.#...................#.. +...#...............#......#.....#.......#......#.........#...#......#...#..#....#..#......#.......#.......#...#.##.#...........##.. +..............##..........##..........#..##...............#...........##..#..#..#.#.......#...#.............#...................... +..........#.#........................#...#.#..#.#...##..#.#.#.#.......#....#.#.#..............#...#...#........###..##...........#. +......................#...#.#..##.....#....#.....###.#..#.....#..........#........#...........##...#..#...........##..#............ +..................................####.#..............#.................#........#.#............##......#.#..........#............. +..................#..........#.............................#...........#..#..#......#................#.......##...##..#............ +....................#...#....#.#.#.##......#................#.................#...........#....#..........#....#...##.###.......... +.................#.......#...#............###.###..#.......#......#.......#..##......#.#.....#.....................#............... +.................................................................S................................................................. +.......##...........#.........###.......#....###.#.#....#.#....#....#........#.......#...#...#.#.........#...................#..... +.......#...............#............#...#..#.......##...#...#.#............#.............#......##..........#..............#....... +.......##.........#.....................##...#...#.................#...#.......#..####......#.....#..............#..##............. +........#..#.............#.#..##..............#...#.............#.....#......#...#.#.......#...............##...##................. +.........#..#...#..#.#..#..#...................##......#.....#..#...##..............#......#..........#.##...#.#.##.#.............. +..............#..........#....................................#.........#.......#..#............##................#................ +...........#...#.....#.........#....#..###....#...#.......#....#...............##.....#.......#.#.....#.#....#.#..#................ +..#...................#........###..............#.#................#...###...##.............#.###......#...#...#.....#..........#.. +.#.#............#.....#....###......................................#..........................#..................#................ +................#..#...#................#.................##.................##..........#.......#...#.............#............... +.....##...........#....#.......#......#.#.....#.....#................#.....#.........#..#.................#........#........#...... +.#.#.............#..#.#..............#.....#..#.#.....#..#......#.........#.....#.#......#..#.......#.#......##............#..#.... +...................#............#....#...........#........#...................#.....##..............#........###...............#... +......###..........###.#.......#.................................................#..#.........#.#...#.........#.#..........#.#..... +..#.................#..#..#...#............#...#...#..##.#....##..#..................##.....#......#.........................#...#. +......................#.#.....#.#...#.......##...........#...#....#.............#.......#.............#.#.#.#...................... +......#..#.#..........#....#.........#......#................#.........##.#....#.......#........#.#....................#......#.... +..#....#....#...............#..........#...#.#......#...................#.........#..#.#......#..#.........................##....#. +..............#..........##......#.##...#...#.#................#...#..#.......#.#.....#........#...###..##.........#..##.........#. +........##...........................##.......#.#..................#........#.......#.#........#.........................##....#... +..#.#...........#.......................#.#.....#....#.....#...............####...#..............#...#....................#.#...... +.......#...#....#.#.......#####...##.....#.#.......#....#.#........#................................#.##..........##....#..#....... +.........#.....#................##.#..................#.....................#...#................#.###.#.......###....#............ +............#.#....#........#....#..................#...##.............#............#.......#.....#..#............##............... +....#.....#..........#..............#...#...#........##.....#.#....#....#........##.............................#.#.......#........ +..#..#.#.......#.................#................................#......##.#..#..............#.#................#...#.#........... +.....#.....#........................#..#................#......#.......#.........###.#.#.........#.........#.....#.......#......... +.......#.......................................#.......#...............#.#..###...#.#.##.......#...........#.#.....#.##........#... +......##.........#.................................#.........#........##.#................#.#...#.............#...#..#........#.... +......#.....#.#.......#...........#..#...........#.........#..#.#.......#.........##.....#....#.#...............#...#...#....#...#. +..#...#.#....#....#..............................#..#...#.......#.#.#..##..................#...............#......#..###..#.#...... +.....#...#..#......#.....##...............#..........#..#.#####.......#...........#...................#.....#...#.........#..#.#... +.................#.......#..................#........#.......##...##.#....#.#.............#.............#....#....#................ +...........####..#.....#....#.............#.......#...#..#.....#.............#...#....#.............#.#....#.#.............#....... +...#..#..#...#...#.#.....#.......................#....#..###.###.....#.........#....................#....#..#............#.....#... +.#.##....##...#......#.#.....#......................#.##..................#........#..#................#.#.......#.............#... +....##........#.......#..#..#............#..#..#..#...#......###..#..#..#...##...............................#.#........#......#.#. +..#..#..........#..........#..#.#.............#.....#...........#.##.......#............................#...........#...#.......... +..#.#..#...#....#.....#.....#...#................#.....#.....#....#..#...#..#....#....#.....................#....#................. +.......#........#...#.............#.................#...#.#.....#.#....#..........#......................#..#............#......... +....#........#........#..............#.........................#...................#..........#.........#..#.........#............. +................#........#.........#............................#.#.............#..##.......#......#..#.................#......#.#. +..#.....#.........#...#..#.#.........................#...#...#...........#.....#...........#........#.............#.....#........#. +....#...#....#......#....#...#..#.................#...##.....#.#............................##...#...#..##.#.....................#. +...#..##...............#............................#...........#......#.#.....#............#........#.##................#......... +................#..#.............#..#..............#.#....#...#.#...#...#.#.##.............................#..#...........#........ +.......#.......#....###............#.#.#.....................#.#..#....#.................##.....#.#..........##..#......#.......... +.......#..........##.#.......#.##...#...............##.....#..........#................#............#...#........#........#........ +...###.#.............................#.....#.#............##..#...#.....###..........#...............................#.....#....#.. +....###.#...#.......#.##........#......#..##.#..........#.#...#.#....#.#..............##....#..#...#...........#...#.#..#.......... +.....#....#.#..##.....#..........#..#...##..............#.......#.......#.............#..#.....#.......#.....#...##...###.......#.. +........##...#...##..#.#.....#..........................#.#.#..#..#...................#....#....#..#....#..........#....##......... +...........##.....#...#........##..#........#....#..........#..#...............................................#......#.#.......#.. +...#..##.........##....#...........##...#.......##.........#........#...........#........#.#.##...........#....#.....###...#....... +...#...#........................#..#...#..#........................................#......#..#...#.#....#...........#.............. +...#..#.......#..#......##...................#...............#.....#.#.............#.....#.........#.#..##.....#..........#..#..... +....###....#..#.....#.#........#.##.......##...#...............#...............###.###.#...#............##.....#.................#. +....#...#.........#.##............#.....#........#............................................##.#......#..##.........#....###..#.. +............#...##..#..........#........#.......#.#..........................#..#.#.......#......#.......................##.#..#... +....#......#....#....#..#.....#........#......#....#..#.#..............................#..#....#............#....#................. +..#.............#...#......#...#.....................#.##...............................##...#.......#........................#.##. +....#....##........#..............#....#.......#.....................................#........#........#...##..........#........... +....#.#.##..#..#....................#.................#.##.............#....#....#.............###...#...#..##....#.....##......... +.............#.......#...............#........#.#.....#..#.#................#.....#..##.....#....#...............#....#......#...#. +................................................................................................................................... diff --git a/2023/21/src/main.rs b/2023/21/src/main.rs new file mode 100644 index 0000000..a02160f --- /dev/null +++ b/2023/21/src/main.rs @@ -0,0 +1,230 @@ +use core::panic; +use indicatif::{ProgressBar, ProgressStyle}; +use rayon::prelude::*; +use std::collections::HashSet; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +type Position = (isize, isize); +type WrappedPosition = (usize, usize); +type Offset = (isize, isize); + +#[derive(Debug, Clone)] +struct MapTile { + c: char, +} + +impl MapTile { + fn new(c: char) -> Self { + Self { c } + } +} + +struct GardenMap { + map: Vec>, + start: Position, +} + +impl From> for GardenMap { + fn from(lines: Lines) -> Self { + let map = lines + .map(|line| line.unwrap().chars().map(|c| MapTile::new(c)).collect()) + .collect(); + let mut new = Self { map, start: (0, 0) }; + new.find_start(); + new + } +} + +const ADJACENCIES: [Offset; 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)]; + +impl GardenMap { + fn wrap_pos(&self, pos: &Position) -> WrappedPosition { + let (width, height) = (self.width() as isize, self.height() as isize); + ( + if pos.0 < 0 { + (pos.0 + (-pos.0 / width + 1) * width) as usize % self.width() + } else { + pos.0 as usize % self.width() + }, + if pos.1 < 0 { + (pos.1 + (-pos.1 / height + 1) * height) as usize % self.height() + } else { + pos.1 as usize % self.height() + }, + ) + } + fn width(&self) -> usize { + self.map[0].len() + } + fn height(&self) -> usize { + self.map.len() + } + fn at(&self, pos: &Position) -> &MapTile { + let pos = self.wrap_pos(pos); + &self.map[pos.1][pos.0] + } + // return the valid 'moves' from pos + fn adjacent_to(&self, pos: &Position) -> Vec { + ADJACENCIES + .iter() + .filter_map(|ofs| self.offset_pos(pos, ofs)) + .filter(|pos| self.at(pos).c != '#') + .collect() + } + fn offset_pos(&self, pos: &Position, ofs: &Offset) -> Option { + let new_pos = (pos.0 as isize + ofs.0, pos.1 as isize + ofs.1); + return Some((new_pos.0, new_pos.1)); + } + fn find_start(&mut self) { + for (y, row) in self.map.iter().enumerate() { + for (x, tile) in row.iter().enumerate() { + if tile.c == 'S' { + self.start = (x as isize, y as isize); + return; + } + } + } + panic!("didn't find the start square!"); + } + fn reachable_after(&self, from: &Position, n: usize) -> u64 { + let bar = ProgressBar::new(n as u64).with_style( + ProgressStyle::with_template( + "[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}", + ) + .unwrap(), + ); + let mut visited_after: Vec> = Vec::new(); + visited_after.push(HashSet::from([*from])); + for i in 1..n+1 { + visited_after.push( + visited_after[i - 1] + .iter() + .flat_map(|last| self.adjacent_to(last)) + .collect(), + ); + bar.inc(1); + // if primes::is_prime(i as u64) { + // println!("count after {} steps: {}", i, visited_after[i].len()); + // } + } + + visited_after[n].len() as u64 + } + fn reachable_count_after(&self, from: &Position, n: usize) -> u64 { + let dim = self.width() as f64; + let target_mod = (n % self.width()) as f64; + let x_values:Vec = vec![target_mod, target_mod + dim, target_mod + 2.*dim]; + let y_values:Vec = x_values.iter().map(|n| self.reachable_after(from, *n as usize) as f64).collect(); + + let coeffs = polyfit_rs::polyfit_rs::polyfit( + &x_values, + &y_values, + 2, + ).unwrap(); + println!("values: x: {:?} y: {:?}", x_values, y_values); + println!("coefficients: {:?}", coeffs); + + let f_n= n as f64; + let result = coeffs[0] + coeffs[1] * f_n + coeffs[2] * f_n.powf(2.0); + + result.round() as u64 + } + fn draw_with_bounds(&self, from: &Position, to: &Position) { + for row in from.1..to.1 + 1 { + for col in from.0..to.0 + 1 { + print!("{}", self.at(&(col, row)).c); + } + println!(); + } + } +} + +// fn print_visited(map: &GardenMap, visited: &Vec>) { +// for (y, row) in visited.iter().enumerate() { +// for (x, cell) in row.iter().enumerate() { +// print!("{}", if *cell { 'O' } else { map.at(&(x, y)).c }); +// } +// println!(); +// } +// } + +// PROBLEM 1 solution + +fn problem1_impl(input: Lines, n: usize) -> u64 { + let map = GardenMap::from(input); + // println!("map: {:?} start: {:?}", map.map, &map.start); + // map.draw_with_bounds( + // &(-(map.width() as isize), -(map.height() as isize)), + // &(map.width() as isize * 2 + 1, map.height() as isize * 2 + 1), + // ); + map.reachable_count_after(&map.start, n) +} + +fn problem1(input: Lines) -> u64 { + problem1_impl(input, 64) +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + problem1_impl(input, 26501365) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + use test_case::test_case; + + const EXAMPLE: &str = &"........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +..........."; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1_impl(c.lines(), 6), 16); + } + + #[test_case(6, 16)] + #[test_case(10, 50)] + #[test_case(50, 1594)] + #[test_case(100, 6536)] + #[test_case(500, 167004)] + fn problem2_example(n: usize, expect: u64) { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1_impl(c.lines(), n), expect); + } +} diff --git a/2023/22/Cargo.lock b/2023/22/Cargo.lock new file mode 100644 index 0000000..be7ab4d --- /dev/null +++ b/2023/22/Cargo.lock @@ -0,0 +1,128 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day22" +version = "0.1.0" +dependencies = [ + "itertools", + "ndarray", + "petgraph", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "matrixmultiply" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +dependencies = [ + "autocfg", + "rawpointer", +] + +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" diff --git a/2023/22/Cargo.toml b/2023/22/Cargo.toml new file mode 100644 index 0000000..75be33d --- /dev/null +++ b/2023/22/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day22" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" +ndarray = "0.15.6" +petgraph = "0.6.4" diff --git a/2023/22/src/main.rs b/2023/22/src/main.rs new file mode 100644 index 0000000..cde4a58 --- /dev/null +++ b/2023/22/src/main.rs @@ -0,0 +1,385 @@ +use itertools::Itertools; +use ndarray::prelude::*; +use petgraph::prelude::*; +use petgraph::visit::{IntoNodeReferences, Walker}; +use std::collections::BinaryHeap; +use std::fmt::{Display, Write}; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::str::FromStr; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +#[derive(Hash, PartialEq, Eq, Clone, Debug)] +struct Coord { + x: usize, + y: usize, + z: usize, +} + +impl FromStr for Coord { + type Err = Box; + fn from_str(value: &str) -> Result { + let (x, y, z) = value.split(',').next_tuple().unwrap(); + Ok(Self { + x: x.parse()?, + y: y.parse()?, + z: z.parse()?, + }) + } +} + +#[derive(Clone, Hash, PartialEq, Eq)] +struct BrickBlock { + c1: Coord, + c2: Coord, +} + +impl std::fmt::Debug for BrickBlock { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!( + f, + "[{},{},{} - {},{},{}]", + self.c1.x, self.c1.y, self.c1.z, self.c2.x, self.c2.y, self.c2.z + ) + } +} + +impl BrickBlock { + fn bottom_z_plane(&self) -> usize { + std::cmp::min(self.c1.z, self.c2.z) + } + fn top_z_plane(&self) -> usize { + std::cmp::max(self.c1.z, self.c2.z) + } + fn bottom_x_plane(&self) -> usize { + std::cmp::min(self.c1.x, self.c2.x) + } + fn top_x_plane(&self) -> usize { + std::cmp::max(self.c1.x, self.c2.x) + } + fn bottom_y_plane(&self) -> usize { + std::cmp::min(self.c1.y, self.c2.y) + } + fn top_y_plane(&self) -> usize { + std::cmp::max(self.c1.y, self.c2.y) + } +} + +impl From<&str> for BrickBlock { + fn from(value: &str) -> Self { + let (c1, c2) = value.split_once('~').unwrap(); + Self { + c1: c1.parse().unwrap(), + c2: c2.parse().unwrap(), + } + } +} + +impl PartialOrd for BrickBlock { + fn partial_cmp(&self, other: &Self) -> Option { + Some(other.bottom_z_plane().cmp(&self.bottom_z_plane())) + } +} + +// Note this is a reversed ordering +impl Ord for BrickBlock { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.bottom_z_plane().cmp(&other.bottom_z_plane()) + } +} + +type BlockMap = Array3; +type MapTile = Option; + +struct BlockPile { + blocks: Vec, + block_map: Array3, + bounds: (usize, usize, usize), + graph: Graph, +} + +impl BlockPile { + fn remove_block(&mut self, block: &BrickBlock) { + // loop over the (inclusive) bounding coordinates and remove them all from the map + self.block_map + .slice_mut(s![ + block.bottom_x_plane()..block.top_x_plane() + 1, + block.bottom_y_plane()..block.top_y_plane() + 1, + block.bottom_z_plane()..block.top_z_plane() + 1, + ]) + .fill(None); + self.blocks.remove(self.blocks.iter().position(|b| b == block).unwrap()); + } + fn add_block(&mut self, block: &BrickBlock) { + // loop over the (inclusive) bounding coordinates and remove them all from the map + self.block_map + .slice_mut(s![ + block.bottom_x_plane()..block.top_x_plane() + 1, + block.bottom_y_plane()..block.top_y_plane() + 1, + block.bottom_z_plane()..block.top_z_plane() + 1, + ]) + .fill(Some(block.to_owned())); + self.blocks.push(block.clone()); + } + fn blocks_directly_above(&self, block: &BrickBlock) -> Vec { + // find the top plane of the block + // get the array range of all squares at z_plane + 1 within the bounds of x & y + // i think there is a built in way in ndarray to do this... + let directly_above = self.block_map.slice(s![ + block.bottom_x_plane()..block.top_x_plane() + 1, + block.bottom_y_plane()..block.top_y_plane() + 1, + block.top_z_plane() + 1..std::cmp::min(block.top_z_plane() + 2, self.bounds.2) + ]); + + directly_above.iter().filter_map(|v| v.clone()).unique().collect() + } + fn supported_by(&self, block: &BrickBlock) -> usize { + let z_plane = std::cmp::min(block.c1.z, block.c2.z); + // get the slice of tiles below us + let directly_below = self.block_map.slice(s![ + block.bottom_x_plane()..block.top_x_plane() + 1, + block.bottom_y_plane()..block.top_y_plane() + 1, + z_plane - 1..z_plane // the layer below + ]); + directly_below.iter().filter_map(|v| v.clone()).unique().count() + } + fn blocks_above_will_move_if_we_are_gone(&mut self, block: &BrickBlock) -> bool { + self.blocks_directly_above(&block) + .iter() + .map(|b| self.supported_by(b)) + .any(|b| b == 1) // block we support will move if we are their only support + } + fn blocks_supported_by_at_all(&self, block: &BrickBlock) -> Vec { + self.blocks_directly_above(&block).iter().map(|b| b.clone()).collect() + } + /// Find the plane of the first block directly below us + fn supporting_plane(&self, block: &BrickBlock) -> Option { + // find the bottom plane of ourselves + let z_plane = std::cmp::min(block.c1.z, block.c2.z); + // get the slice of tiles below us + let directly_below = self.block_map.slice(s![ + block.bottom_x_plane()..block.top_x_plane() + 1, + block.bottom_y_plane()..block.top_y_plane() + 1, + 1..z_plane // don't include our own plane + ]); + // find the highest top z value of those + let block_below = directly_below + .indexed_iter() + .filter_map(|(idx, v)| if let Some(val) = v { Some((idx, val)) } else { None }) + .max_by_key(|(_idx, v)| v.top_z_plane()); + + if let Some(block) = block_below { + Some(block.1.top_z_plane()) + } else { + None + } + } + + fn drop_blocks(&mut self) { + // VecDeque doesn't sort and Vec isn't convenient for pushback and popfront, so eh... use a heap. + let mut blocks_to_move = BinaryHeap::from(self.blocks.clone()); + while let Some(mut block) = blocks_to_move.pop() { + let z_move = match self.supporting_plane(&block) { + Some(z) if z + 1 != block.bottom_z_plane() => block.bottom_z_plane() - (z + 1), + None if block.bottom_z_plane() != 1 => block.bottom_z_plane() - 1, + _ => { + continue; + } // we are in position already with nothing below us + }; + self.remove_block(&block); + block.c1.z -= z_move; + block.c2.z -= z_move; + self.add_block(&block); + blocks_to_move.push(block); + } + } + fn build_graph(&mut self) { + self.blocks.sort_by_key(|b| b.bottom_z_plane()); + for b in 0..self.blocks.len() { + self.graph.add_node(self.blocks[b].clone()); + } + for b in 0..self.blocks.len() { + let block = &self.blocks[b]; + let depends_on_us = self.blocks_supported_by_at_all(block); + for dependent in depends_on_us { + self.graph.add_edge( + b.into(), + self.blocks.iter().position(|b| b == &dependent).unwrap().into(), + (), + ); + } + } + } +} + +impl From> for BlockPile { + fn from(lines: Lines) -> Self { + let blocks = lines.map(|line| BrickBlock::from(line.unwrap().as_str())).collect_vec(); + let mut bounds = (0, 0, 0); + for block in &blocks { + bounds.0 = std::cmp::max(block.top_x_plane() + 1, bounds.0); + bounds.1 = std::cmp::max(block.top_y_plane() + 1, bounds.1); + bounds.2 = std::cmp::max(block.top_z_plane() + 1, bounds.2); + } + let mut new = BlockPile { + blocks: Vec::new(), + bounds, + block_map: BlockMap::from_elem(bounds, None), + graph: Graph::default(), + }; + for block in blocks { + new.add_block(&block); + } + + new + } +} + +impl Display for BlockPile { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for z in (0..self.bounds.2).rev() { + let z_plane = self.block_map.slice(s![.., .., z..z+1]); + if z_plane.iter().all(|b| b.is_none()) { continue }; + for i in 0..self.bounds.0 { + // XZ view + let y = z_plane.slice(s![i..i + 1, .., ..]); + f.write_char( + match y.iter().enumerate().find_map(|(idx, val)| val.is_some().then(|| idx)) { + None => ' ', + Some(0) | Some(1) => '█', + Some(2) | Some(3) => '▓', + Some(4) | Some(5) => '▒', + Some(6) | Some(7) => '░', + _ => '░', + }, + )?; + } + write!(f, " {} ", z)?; + for y in 0..self.bounds.1 { + let x = z_plane.slice(s![.., y..y + 1, ..]); + f.write_char( + match x.iter().enumerate().find_map(|(idx, val)| val.is_some().then(|| idx)) { + None => ' ', + Some(0) | Some(1) => '█', + Some(2) | Some(3) => '▓', + Some(4) | Some(5) => '▒', + Some(6) | Some(7) => '░', + _ => '░', + }, + )?; + } + writeln!(f, " {}", z)?; + } + Ok(()) + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut pile = BlockPile::from(input); + println!("{}", pile); + println!("dropping blocks!"); + pile.drop_blocks(); + println!("{}", pile); + + let blocks = pile.blocks.clone(); + let removable: Vec<_> = blocks + .iter() + .filter(|b| !pile.blocks_above_will_move_if_we_are_gone(*b)) + .collect(); + + removable.len() as u64 +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut pile = BlockPile::from(input); + pile.drop_blocks(); + pile.build_graph(); + + println!("{}", pile); + + let mut accum = 0; + let fixed_nodes = pile + .graph + .node_references() + .filter(|(_idx, b)| b.bottom_z_plane() == 1) + .map(|(idx, _b)| idx) + .collect_vec(); + for node in pile.graph.node_indices() { + // remove links to node's neighbors + let dependents = pile.graph.neighbors(node).collect_vec(); + let edges = pile.graph.edges(node).map(|v| v.id()).collect_vec(); + for edge in edges { + pile.graph.remove_edge(edge); + } + + // find how many nodes are reachable from z = 1 - these won't move + let safe_blocks = fixed_nodes + .iter() + .flat_map(|origin| { + Bfs::new(&pile.graph, *origin) + .iter(&pile.graph) + .map(|n| pile.graph[n].clone()) + }) + .unique() + .count(); + // we are looking for the nodes that *will* disintegrate + accum += pile.graph.node_count() - safe_blocks; + // put the graph back how it was + for neigh in dependents { + pile.graph.add_edge(node, neigh, ()); + } + } + accum as u64 +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"1,0,1~1,2,1 +0,0,2~2,0,2 +0,2,3~2,2,3 +0,0,4~0,2,4 +2,0,5~2,2,5 +0,1,6~2,1,6 +1,1,8~1,1,9"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 5); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 7); + } +} diff --git a/2023/23/Cargo.lock b/2023/23/Cargo.lock new file mode 100644 index 0000000..e856710 --- /dev/null +++ b/2023/23/Cargo.lock @@ -0,0 +1,128 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day23" +version = "0.1.0" +dependencies = [ + "itertools", + "ndarray", + "petgraph", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "matrixmultiply" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +dependencies = [ + "autocfg", + "rawpointer", +] + +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" diff --git a/2023/23/Cargo.toml b/2023/23/Cargo.toml new file mode 100644 index 0000000..177fd7a --- /dev/null +++ b/2023/23/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day23" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" +ndarray = "0.15.6" +petgraph = "0.6.4" diff --git a/2023/23/src/main.rs b/2023/23/src/main.rs new file mode 100644 index 0000000..84afd54 --- /dev/null +++ b/2023/23/src/main.rs @@ -0,0 +1,326 @@ +use itertools::Itertools; +use ndarray::prelude::*; +use petgraph::algo::all_simple_paths; +use petgraph::prelude::*; +use std::collections::{HashMap, HashSet}; +use std::fmt::{Debug, Display, Write}; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +#[derive(Clone)] +struct Node { + c: char, + pos: Position, +} + +impl Display for Node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({},{})", self.pos.0, self.pos.1) + } +} + +impl Debug for Node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({},{})", self.pos.0, self.pos.1) + } +} + +type Position = (usize, usize); + +#[derive(Clone)] +struct ForestMap { + map: Array2, + indexes: HashMap, + graph: StableDiGraph, + start: Position, + end: Position, +} + +const ADJACENCIES: [(isize, isize); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)]; + +fn offset_pos(map: &Array2, pos: Position, ofs: (isize, isize)) -> Option { + let new_pos = (pos.0 as isize + ofs.0, pos.1 as isize + ofs.1); + if new_pos.0 >= 0 + && new_pos.0 < map.len_of(Axis(0)) as isize + && new_pos.1 >= 0 + && new_pos.1 < map.len_of(Axis(1)) as isize + { + Some((new_pos.0 as usize, new_pos.1 as usize)) + } else { + None + } +} + +fn adjacent_to(map: &Array2, pos: Position) -> Vec { + ADJACENCIES + .iter() + .filter_map(|ofs| offset_pos(map, pos, *ofs)) + .collect() +} + +impl From> for ForestMap { + fn from(lines: Lines) -> Self { + let rows = lines.map(|line| line.unwrap().chars().collect_vec()).collect_vec(); + let map = Array::from_shape_vec([rows[0].len(), rows.len()], rows.into_iter().flatten().collect_vec()) + .unwrap() + .reversed_axes(); + let start = (map.slice(s![.., 0]).iter().position(|c| *c == '.').unwrap(), 0); + let end = ( + map.slice(s![.., map.len_of(Axis(1)) - 1]) + .iter() + .position(|c| *c == '.') + .unwrap(), + map.len_of(Axis(1)) - 1, + ); + + let mut graph = StableGraph::default(); + let mut indexes = HashMap::new(); + for (pos, c) in map.indexed_iter() { + if *c != '#' { + indexes.insert(pos, graph.add_node(Node { c: *c, pos })); + } + } + + Self { + map, + start, + end, + graph, + indexes, + } + } +} + +impl ForestMap { + fn build_graph(&mut self) { + for (pos, c) in self.map.indexed_iter() { + match c { + '#' => continue, + '.' => { + adjacent_to(&self.map, pos).iter().for_each(|adj| { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[adj], 1); + } + }); + } + '^' => { + if let Some(adj) = offset_pos(&self.map, pos, (0, -1)) { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[&adj], 1); + } + } + } + '>' => { + if let Some(adj) = offset_pos(&self.map, pos, (1, 0)) { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[&adj], 1); + } + } + } + 'v' => { + if let Some(adj) = offset_pos(&self.map, pos, (0, 1)) { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[&adj], 1); + } + } + } + '<' => { + if let Some(adj) = offset_pos(&self.map, pos, (-1, 0)) { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[&adj], 1); + } + } + } + c => panic!("invalid map character {}", c), + } + } + } + + fn build_graph2(&mut self) { + for (pos, c) in self.map.indexed_iter() { + match c { + '#' => continue, + '.' | '^' | '>' | 'v' | '<' => { + adjacent_to(&self.map, pos).iter().for_each(|adj| { + if self.indexes.contains_key(&adj) { + self.graph.add_edge(self.indexes[&pos], self.indexes[adj], 1); + } + }); + } + c => panic!("invalid map character {}", c), + } + } + } + + // Cull nodes that don't change the topology of the graph and combine their cost + fn simplify_graph(&mut self) { + let mut idxs: Vec<_> = self + .graph + .neighbors(self.indexes[&self.start]) + .map(|idx| (self.indexes[&self.start], idx)) + .collect(); + let mut visited = HashSet::from([self.indexes[&self.start]]); + while let Some((last_idx, cur_idx)) = idxs.pop() { + if !visited.insert(cur_idx) { + continue; + } + let our_neighbors = self.graph.neighbors(cur_idx).collect_vec(); + + // if we have exactly 2 neighbours, then one is where we came from, and we can shortcut this node with a + // pair of new edges A <-> C and break the existing 4 edges between them + if our_neighbors.len() == 2 { + let next_idx = our_neighbors.iter().find(|n| **n != last_idx).unwrap(); + + // remove the 4 existing edges + // careful of order of operations, as removing edges invalidates edge indexes + let forward_cost = self + .graph + .remove_edge(self.graph.find_edge(cur_idx, *next_idx).unwrap()) + .unwrap(); + let last_forward_cost = self + .graph + .remove_edge(self.graph.find_edge(last_idx, cur_idx).unwrap()) + .unwrap(); + let backward_cost = self + .graph + .remove_edge(self.graph.find_edge(cur_idx, last_idx).unwrap()) + .unwrap(); + let next_backward_cost = self + .graph + .remove_edge(self.graph.find_edge(*next_idx, cur_idx).unwrap()) + .unwrap(); + let new_forward_cost = forward_cost + last_forward_cost; + let new_backward_cost = backward_cost + next_backward_cost; + + // add edge from last to next + self.graph.add_edge(last_idx, *next_idx, new_forward_cost); + self.graph.add_edge(*next_idx, last_idx, new_backward_cost); + + self.graph.remove_node(cur_idx); + // push the next node + idxs.push((last_idx, *next_idx)); + } else { + // don't do anything about nodes with > 2 edges, just push them onto the stack, if there are some + idxs.append( + &mut self + .graph + .neighbors(cur_idx) + .into_iter() + .map(|next_idx| (cur_idx, next_idx)) + .collect(), + ); + } + } + } +} + +impl Debug for ForestMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for y in 0..self.map.len_of(Axis(1)) { + for c in self.map.index_axis(Axis(1), y) { + f.write_char(*c)?; + } + writeln!(f)?; + } + Ok(()) + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut map = ForestMap::from(input); + map.build_graph(); + // println!("{:?}", map); + let paths = all_simple_paths::, _>(&map.graph, map.indexes[&map.start], map.indexes[&map.end], 0, None) + .collect_vec(); + let longest = paths.iter().max_by_key(|path| path.len()).unwrap(); + + longest.len() as u64 - 1 +} + +fn calc_path_length(map: &ForestMap, path: &Vec) -> u64 { + path.iter().tuple_windows().fold(0, |accum, (prev, next)| { + accum + map.graph[map.graph.find_edge(*prev, *next).unwrap()] + }) +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut map = ForestMap::from(input); + map.build_graph2(); + map.simplify_graph(); + + let paths = all_simple_paths::, _>(&map.graph, map.indexes[&map.start], map.indexes[&map.end], 0, None) + .collect_vec(); + let longest = paths.iter().max_by_key(|path| calc_path_length(&map, &path)).unwrap(); + longest.iter().tuple_windows().fold(0, |accum, (prev, next)| { + accum + map.graph[map.graph.find_edge(*prev, *next).unwrap()] + }) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"#.##################### +#.......#########...### +#######.#########.#.### +###.....#.>.>.###.#.### +###v#####.#v#.###.#.### +###.>...#.#.#.....#...# +###v###.#.#.#########.# +###...#.#.#.......#...# +#####.#.#.#######.#.### +#.....#.#.#.......#...# +#.#####.#.#.#########v# +#.#...#...#...###...>.# +#.#.#v#######v###.###v# +#...#.>.#...>.>.#.###.# +#####v#.#.###v#.#.###.# +#.....#...#...#.#.#...# +#.#########.###.#.#.### +#...###...#...#...#.### +###.###.#.###v#####v### +#...#...#.#.>.>.#.>.### +#.###.###.#.###.#.#v### +#.....###...###...#...# +#####################.#"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 94); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 154); + } +} diff --git a/2023/24/Cargo.lock b/2023/24/Cargo.lock new file mode 100644 index 0000000..2e519f1 --- /dev/null +++ b/2023/24/Cargo.lock @@ -0,0 +1,134 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "day24" +version = "0.1.0" +dependencies = [ + "itertools", + "lazy-regex", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "lazy-regex" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "syn" +version = "2.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/24/Cargo.toml b/2023/24/Cargo.toml new file mode 100644 index 0000000..1b063bd --- /dev/null +++ b/2023/24/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day24" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" +lazy-regex = "3.1.0" diff --git a/2023/24/cheat.py b/2023/24/cheat.py new file mode 100644 index 0000000..b2d3d4a --- /dev/null +++ b/2023/24/cheat.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +from z3 import * + +rx, ry, rz = Ints('rx ry rz') +rvx, rvy, rvz = Ints('rvx rvy rvz') +t0, t1, t2 = Ints('t0 t1 t2') +answer = Int('answer') + +solve( + rx + t0 * rvx == 225415405941969 + t0 * 23, + ry + t0 * rvy == 400648127977931 + t0 * -204, + rz + t0 * rvz == 79201130433258 + t0 * 617, + + rx + t1 * rvx == 353783687623292 + t1 * -80, + ry + t1 * rvy == 138575899489956 + t1 * 156, + rz + t1 * rvz == 318416438572569 + t1 * 21, + + rx + t2 * rvx == 215751176267772 + t2 * -120, + ry + t2 * rvy == 376619563956940 + t2 * 126, + rz + t2 * rvz == 230133299986253 + t2 * -352, + + answer == rx + ry + rz, +) diff --git a/2023/24/src/main.rs b/2023/24/src/main.rs new file mode 100644 index 0000000..2c6ed13 --- /dev/null +++ b/2023/24/src/main.rs @@ -0,0 +1,205 @@ +use itertools::Itertools; +use std::fmt::Debug; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::ops::Index; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// Parse + +#[derive(Clone, Copy)] +struct Vec3 { + x: f64, + y: f64, + z: f64, +} +impl Debug for Vec3 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}, {}, {}", self.x, self.y, self.z) + } +} + +#[derive(Clone, Copy)] +struct Vec2 { + x: f64, + y: f64, +} +impl Debug for Vec2 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}, {}", self.x, self.y) + } +} + + +#[derive(Clone)] + +struct Hailstone { + pos: Vec3, + vel: Vec3, +} + +impl Hailstone { + fn xy_vel_angle(&self) -> f64 { + (self.vel.y / self.vel.x).atan() + } + fn xy_vel_abs(&self) -> f64 { + (self.vel.x.powi(2) + self.vel.y.powi(2)).sqrt() + } + fn xy_slope(&self) -> f64 { + self.vel.y / self.vel.x + } + fn y_crossing(&self) -> f64 { + self.pos.y - self.xy_slope() * self.pos.x + } + fn xy_poi(&self, other: &Hailstone) -> Option { + let our_slope = self.xy_slope(); + let other_slope = other.xy_slope(); + if our_slope == other_slope || our_slope * other_slope == -1. { + None + } else { + let our_yint = self.y_crossing(); + let other_yint = other.y_crossing(); + let ratio = (other_yint - our_yint) / (our_slope - other_slope); + Some(Vec2 { + x: ratio, + y: our_slope * ratio + our_yint, + }) + } + } + fn xy_point_future(&self, point: &Vec2) -> bool { + // a point will be in the past if the difference between its 'new' position and its 'start' position has a + // different sign than the velocity for any component + let diffs = [point.x - self.pos.x, point.y - self.pos.y]; + // for (diff, vel) in diffs.iter().zip([self.vel.x, self.vel.y].iter()) { + // println!(" diff: {:?} vel: {:?} mul: {:?}", diff, vel, diff * vel > 0.); + // } + diffs.iter().zip([self.vel.x, self.vel.y].iter()).any(|(diff, vel)| diff * vel > 0.) + } +} + +impl From<&str> for Hailstone { + fn from(value: &str) -> Self { + let (pos, vel) = value.split_once("@").unwrap(); + let [px, py, pz] = pos.split(",").map(|s| s.trim().parse::().unwrap()).collect_vec()[..] else { + panic!() + }; + let [vx, vy, vz] = vel.split(",").map(|s| s.trim().parse::().unwrap()).collect_vec()[..] else { + panic!() + }; + + Self { + pos: Vec3 { x: px, y: py, z: pz }, + vel: Vec3 { x: vx, y: vy, z: vz }, + } + } +} + +impl Debug for Hailstone { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?} @ {:?}", self.pos, self.vel) + } +} + +#[derive(Debug)] +struct Hailstones { + stones: Vec, +} + +impl Hailstones { + fn count_xy_pois(&self, bounds: (f64, f64)) -> u64 { + assert!(bounds.0 < bounds.1); + let mut stones = self.stones.clone(); + + let mut count = 0; + while let Some(stone) = &stones.pop() { + for other in &stones { + let Some(poi) = stone.xy_poi(other) else { continue }; + // println!("intersection: {:?} / {:?} @ {:?}", stone, other, poi); + if poi.x >= bounds.0 && poi.x <= bounds.1 && poi.y >= bounds.0 && poi.y <= bounds.1 && stone.xy_point_future(&poi) && other.xy_point_future(&poi) { + count += 1; + } + } + } + + count + } +} + +impl Index for Hailstones { + type Output = Hailstone; + fn index(&self, index: usize) -> &Self::Output { + &self.stones[index] + } +} + +impl From> for Hailstones { + fn from(value: Lines) -> Self { + let mut stones = Vec::new(); + for line in value { + stones.push(Hailstone::from(line.unwrap().as_str())); + } + Hailstones { stones } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + problem1_impl(input, (200000000000000., 400000000000000.)) +} + +fn problem1_impl(input: Lines, bounds: (f64, f64)) -> u64 { + let stones = Hailstones::from(input); + + stones.count_xy_pois(bounds) +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + 0 +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"19, 13, 30 @ -2, 1, -2 +18, 19, 22 @ -1, -1, -2 +20, 25, 34 @ -2, -2, -4 +12, 31, 28 @ -1, -2, -1 +20, 19, 15 @ 1, -5, -3"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1_impl(c.lines(), (7., 27.)), 2); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 0); + } +} diff --git a/2023/25/Cargo.lock b/2023/25/Cargo.lock new file mode 100644 index 0000000..45c6130 --- /dev/null +++ b/2023/25/Cargo.lock @@ -0,0 +1,266 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fca89a0e215bab21874660c67903c5f143333cab1da83d041c7ded6053774751" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e3681d554572a651dda4186cd47240627c3d0114d45a95f6ad27f2f22e7548d" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3a430a770ebd84726f584a90ee7f020d28db52c6d02138900f22341f866d39c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "day25" +version = "0.1.0" +dependencies = [ + "indicatif", + "itertools", + "petgraph", + "rayon", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "indicatif" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/2023/25/Cargo.toml b/2023/25/Cargo.toml new file mode 100644 index 0000000..efca931 --- /dev/null +++ b/2023/25/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day25" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +indicatif = "0.17.7" +itertools = "0.12.0" +petgraph = "0.6.4" +rayon = "1.8.0" diff --git a/2023/25/src/main.rs b/2023/25/src/main.rs new file mode 100644 index 0000000..a429390 --- /dev/null +++ b/2023/25/src/main.rs @@ -0,0 +1,171 @@ +use indicatif::{ProgressBar, ProgressIterator, ProgressStyle}; +use itertools::Itertools; +use petgraph::algo::connected_components; +use petgraph::dot::Dot; +use petgraph::prelude::*; +use petgraph::visit::{depth_first_search, DfsEvent, IntoEdgeReferences, IntoNodeIdentifiers, IntoNodeReferences}; +use rayon::prelude::*; +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSE + +struct Circuit { + graph: UnGraph, + nodes: HashMap, +} + +#[derive(Debug, Clone)] +struct Node { + targets: Vec, + index: Option, +} + +impl Node { + fn with_index(mut self, index: NodeIndex) -> Self { + self.index = Some(index); + self + } +} + +impl From> for Circuit { + fn from(lines: Lines) -> Self { + let mut graph = UnGraph::default(); + let mut nodes = HashMap::new(); + for line in lines.map(|v| v.unwrap()) { + let (source, targets_s) = line.split_once(": ").unwrap(); + let targets = targets_s.split_whitespace().map(|s| s.to_string()).collect_vec(); + nodes.insert( + source.to_string(), + Node { + targets, + index: Some(graph.add_node(source.to_string())), + }, + ); + } + + for (name, node) in nodes.iter_mut() { + for target in &node.targets { + let target_idx = match graph.node_references().find(|n| n.1 == target).map(|n| n.0) { + Some(target) => target, + None => graph.add_node(target.to_string()), + }; + graph.add_edge(node.index.unwrap(), target_idx, format!("{}-{}", name, target)); + } + } + + Self { graph, nodes } + } +} + +impl Circuit { + // returns the set of nodes on each side + fn find_critical_edges(&self, removals: usize) -> (Vec, Vec) { + let bar = ProgressBar::new(self.graph.edge_references().combinations(removals).try_len().unwrap() as u64) + .with_style( + ProgressStyle::with_template( + "[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}", + ) + .unwrap(), + ); + // for set in self.graph.edge_references().combinations(removals).progress_with(bar) { + + let set = [("zcp", "zjm"), ("rsg", "nsk"), ("jks", "rfg")] + .iter() + .flat_map(|(src, dst)| { + self.graph.edge_references().filter(|e| self.graph[e.source()] == *src && self.graph[e.target()] == *dst || self.graph[e.target()] == *src && self.graph[e.source()] == *dst) + }).collect_vec(); + let mut local_graph: StableUnGraph<_, _> = self.graph.clone().into(); + for wire in &set { + local_graph.remove_edge(wire.id()); + } + let local_graph = UnGraph::from(local_graph); + if connected_components(&local_graph) == 2 { + // each edge will have one leg in each component, so return the set of connected nodes + let mut left = Vec::new(); + depth_first_search(&local_graph, Some(set[0].source()), |event| { + if let DfsEvent::Discover(n, t) = event { + left.push(local_graph[n].to_string()); + } + }); + let mut right = Vec::new(); + depth_first_search(&local_graph, Some(set[0].target()), |event| { + if let DfsEvent::Discover(n, t) = event { + right.push(local_graph[n].to_string()); + } + }); + return (left, right); + } + // } + panic!("no cutline found"); + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let circuit = Circuit::from(input); + println!("{:?}", Dot::new(&circuit.graph)); + let (left, right) = circuit.find_critical_edges(3); + (left.len() * right.len()) as u64 +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + 0 +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"jqt: rhn xhk nvd +rsh: frs pzl lsr +xhk: hfx +cmg: qnr nvd lhk bvb +rhn: xhk bvb hfx +bvb: xhk hfx +pzl: lsr hfx nvd +qnr: nvd +ntq: jqt hfx bvb xhk +nvd: lhk +lsr: lhk +rzs: qnr cmg lsr rsh +frs: qnr lhk lsr"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 54); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 0); + } +} diff --git a/2023/3/Cargo.lock b/2023/3/Cargo.lock new file mode 100644 index 0000000..b62acea --- /dev/null +++ b/2023/3/Cargo.lock @@ -0,0 +1,92 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day3" +version = "0.1.0" +dependencies = [ + "num", +] + +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] diff --git a/2023/3/Cargo.toml b/2023/3/Cargo.toml new file mode 100644 index 0000000..572ad51 --- /dev/null +++ b/2023/3/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num = "0.4.1" diff --git a/2023/3/input b/2023/3/input new file mode 100644 index 0000000..3030b76 --- /dev/null +++ b/2023/3/input @@ -0,0 +1,140 @@ +........897.......839...........651.399.............236...............................343...986...........308............................... +........*................*404......*............134.....953..508=.....................*....*..........325*..........744......392............ +...350..847..403...-..541.....622.................*....................356.......%..95...793....................830...........*.....163..... +.....&.......@...105.............$..745............42...785.............*..443.412............922..............*...........663........*..... +.253....../..........696.............*..................*..............817.$.....................*...........62........................373.. +...........244...419....*189....=........*...871.74...797...353.737................227..626.....429.726%...9......*956...803/.......*....... +.793...........=.....-...........119..683.18.*...............@......+.....182..372....$.&...................*..756...............945.610.... +....*.........496.....395.....................929.746................76..*.....*..............673-...*.....47........591..271............... +.....964..................241...........*............+.....+....590.......129.6....$.....@...........680....................*............... +..........523*448...........&....136.759.356............789.......*.................367.330..397.............483....354......354...869...... +...488................920........*...........................652...802....$.......*............*..............-.....................*....... +..../.........986....*......*....550.995......376...............+......641.....567.315.........887..66...863....552.936..........277........ +.................*.800...354.464.......*.533......................20.......704..........918........*.....*...........*....214........&434... +........%.....589...................596..*.......373...-..........*....785.@......941......*....521.......908...897..610.................... +........384..........827*101....698......978....*......947.614.571....*..........@.....+...1.......................=......#73...734......... +122%..........*...................*..........993............./.....260.........+.....466...............%........................*........... +...........938.992........246......305...449......335...739................588..681.........920.....164.........................431......... +..558....$........................................@.....*.....................=............-............847.....879..51................624.. +.....*.104....*818.......505.785.10...................161....210....................723..........55......*..424..&.....*.........723..*..... +...44......542.....975..................437...............51....*686.....*....../..*.........990*......708.....@....715...&....#.......997.. +...............801..@.............-....*.....702....&....*............238.236.706.183..+................................842.545............. +...........202*...................906..204...*...990......212....380%..................208...................66............................. +......./........../....878............................266.......................278...............920...................144.........423..... +....494..*.....976........@.....&.......*287...553-..*.............731...................649..247*........703......@....%....817....*....... +.........856.......592...........708.404............707.......274...*........-............#.....................%.567.........*......431.761 +.............663.........974...............%...446.............*............751....793..........811....225.&...48.....860..887.............. +................&...........*.......673..536......-.........447.....@...............@...$..................816..........*................... +....822................@....251....$............................413..306.164.............540.....313..476..........@...586./65....840....... +.....*......@.950.....266............$.........1...+...............*..........373..................*.............885.................*...... +......157.529....*.........341.....480.............60......765....122...633....+............$182....963.....................587..976.824.... +................172............685.....480....#........831*................*......747......................262..................-........... +....*......+438.....@......332./.......=...739..815...............356.....119.273.&.....317*674.......=...*....561..........192..........89. +....867..-........179..............319........../....74@..952*374...*.........*....................125....578...*...........*......652...... +..........549.............873.........*...=........*...............695.*.....126.........821.....................697.$......806.52....*54... +....792...........-...646*....770.....163.637....941........884.........744..............................#............800................... +.......*.......181............@.............../........92..#...................518...654.......421*995.310....450...............771......... +...+................./48...........968.........269................904.......=...../.....................................266.....*........... +.211....636.................630.17*.....%581........./843.....%..*.......878...............829.....119.33........211.........247............ +.......*......................-...................%..........559..873.............*138.439*....687*..........130....*681.................... +....522.................792........896.........471...480*321...................125..............................*...................291..... +........616.......818.....$..858.............................659*826.....109.....................................10.564=.331...156...+...... +........*.......@...*..29....*...@......546.............907.............*.....965.......217.808.....802..212...............*................ +....607.976..641..852.@...330...158....../.......630........247.......503........$.335+....*.........*..*....618...423&.335...803........... +......=..............................502........#........-..*.....923......664....................421...395.-..................#............ +........*.................625....646..@...99............901.59......*.....*..........517.$747.*....................315..............79...922 +.....289.789..............%......*.........*......................762....388..189.....*........277.........*222...*.................$....... +.................................738....406.....462...........................*.....915..............505/.......61.....+.......202*......646 +..749..............163............................*.....963..........907.......979....................................223..716.....265...... +.......&....741............362.......117.......837..96../.................#..............786.256.....942..=970...983........*............... +......133......*486.725.......*....../...............%................930.310...................*733.-.............$.....198.........189.... +....................*........256........68.......267...873........418*.....................573.................................219..*....... +......+.....439..525..187........495..............*...=.......923..........327..988..300........447*59..702../..840......40.....*........... +......731....*........*..........*..............547.......453.....6.$.........*.......*................-....447....@.......*.737............ +.907#.......375........195.....707..17.................9..........%..119...147..594.22.......81.........................485......446........ +.....................................*....#............*.....62...................*...........*....569...935.868....-...............*528.... +...#..........209........229.......787.470..............403........892.........816.....923..811...$.........*......104.907.849....*......... +..154.346.....*...361.........................10/...237................-................*.............600..............*.......998.606...... +.........%...216..*.......*.........*368.869.........#................6..98...........53.............*.....850@...102...751................. +....550............727...383.....818.....*...913.............879...........*.....420.....150.........708.................................... +705*.......@688.......................289.....*.........................431........*...+....*50.....................698...668*144........... +..................501..70.520....$32......562..458....170......279$............504......883............/...44........*...................... +..450..107@........*..................586*...........*.........................*....510............788.274............589...-26......&...... +...................342.........&.551+.................302...............286.....931....&....-649..$.............575................185...199 +............602..............470.........243*......@............818.690*.......................................*....863..793*............... +......934.....*......200.936.....46..........137.72.........377*......................443*372........498....320..#...........553......598... +........-...451./952..*...*....*.......................804..............305......700............675...&.........611......................... +......................393.......708....957........*697......808.....138*.........%......186....*....%...162*...........624........244.%..... +.....220*.........872......*...........*.........7.........................................=..6....391......562...880.....*......#....120... +...+.....608.........*...74.568........193.........379.........$682......933.927&.....&............................./..984.................. +93..553........*768.739...........421.........880.....*....737.............*.........69..................................................... +.........334...............................40*........910.................................+465..682.............141*.......248..80.......... +.........=..................323...172.............506..............503........568....785+.......%.......940*734.......#573....*...*.....130. +...163@........%17..42..108*.......*...$......700..*..81...../...............+....%.............................#285.........499.523....*... +.............-........*..........305.706.....*....942.....843........*.............378.861.......573.859.173............522............544.. +..........974.......517.756..762.............480.....................289................*...........*......*.228%..........*......628....... +....567........&274......+..../..........................................926.............391............214.............270...230..-........ +...........=...............52...../......155*150.......180&..*......482..&...884................-125...........................@............ +..........332..................318............................123......%......*...............=........................310.................. +....................856.154........@............161....192..................=..387.............384..*827................*..&400......588.... +.......283.............*....@.......12.....309...*...$...........643.......885..........862................210.......449.................... +.......*......937.........98.....&........*....560....884.......$.................866#.../.............622..*..901.............462.......... +.....279............273......896..17.661..880...............138...632......66...%.............93......*....349...*........./......=......... +..............430....*....+.....*.....*............350...............*..........226.......621*......312........=..266....574..111........... +..339........../.....770.451....122........990.....................811...780........*448.................325.249...............+...$...441.. +......................................$......%...........793..............@..787.766....................................64..+......252.*.... +.........556........../.....256/...267...336.....897..........524*417.........................950...930$.452........164......713.......592.. +....759....*...........672.................-.868....*....613......................401.....742..*..............560........=.........939...... +.....$...780.......195......578...............*...%...+....*.....=.....&.............*......#...645...636.797..........652...........*...... +......................*......................77...97.988.821...882...977.......127.609.914...............*.................................. +..........+...195&.....930.....872.......................................79..............&.....14...................-.......150..274........ +..........473................9*.............951&................./...827*.............46......-...13....751.........640.......*.....*504.... +898................-.............56*....378..............291..310..........58............937.....$.......*.../.............840..714......... +............-321.128.157............549..-....130....478*.........*.....................................123...164..............*....631..... +..516..989@..........*.........*670............................204..577.......&817....@514........790...............816.........148.*....... +.....*................233...........*838.....*951.....-954..........$.......&.................110.........99...........*..@187.......671.467 +224...205.....552................823......343................490*...........377..................*.......$...........779.......408.......... +............./..../.....................................166*..........120...........452.........148...=.......................*............. +.562..............768.........#.267.29...806*......476......214.........*....*......*...............867.211....248.$870....@...829...*...... +...*..826@...................1.....*.........251....+.....#...........211.893.403....702.......44.......*........+..........75......977..... +..368.........861$......867....=.......................834.....................................=.........507................................ +.......721*............*.......912.#497....................87-.....231...894.......968..........................659$.......@..........564... +...........690.......432....@..................332................*.........*756..*................302..214*.........803..855............... +..................26.......943...........633...*.....299.150....639.................796.....440.............428......+...........538.647.... +........31........*............................260.....-................=..261.............*............................644.303..%.....-.... +227..........#.161....*....&.....305...............345........328....639...*........$.......802....612.185................%../.............. +.......683.953.....224....58.....*.................*....799...............944.964....245.............*.%................@......28.19.242.97. +....93*........../.............908.........$249..480.......*......978..............................58........=209......373.802*.........*... +467...........201..........932.....997......................548................/.....-904.....796.....3+............................915..... +...................#......@.......*......748*965.................922......451..169..............$.945....372#..979........%..436...*........ +........&.....880&..789........14.805..............571....222/.....*.70......*........./958..........%.........&.../....61.....*...73....... +.......280.................................=.861......-.........144...........950................%.......907......910.......396............. +.....+.........592*149.820*511...........298....&.......*264..........-..527..............-.....679.......*...............@......50*270..... +...901......=....................@483.............$..936......625%...619....%..........748...............785...860......26..181.........747. +.........85..640...#........=.=.......49.......216.............................268*.............................#...........#.....53....*... +...........+.......744...933...766....*.................%...............*714.......909................................999...........*...311. +.............783....................947.................352...765..............897.....+430..869.................121.%............236....... +...45....*......*..752......617..................332...........*...236...........*..........*....@.528..........&......530.................. +..../.134.713.871..*.............*...........#.....%.930...........*....323....377........678.544..*........542.........$.......470.629*786. +..................259...&.....959.225......177.......*.....912*..........$.........................306..996.*....*.........*681...*......... +..........250*768......598.........................850.........598..............3*.........904*648........=.372...197...410......152...=.... +............................................507@.......960..............362.164...131...................................................115. +.....746.......117..678=........952...303*.......+......................./..*...........$636../...597......37................351...=........ +.......*.281...@...................*......337.258..814...687.....#922.=....772..611.153.......573....*.......*..............*.......518..... +....144.....*.......396...........240................+......*.........45.......+.......*...........479.&293..126.....784-...783............. +...........843...@....*..918*.............................986.....542...................117.....................................578.707..... +..984.............251........258....597$.650.........................*..................................928.409.684...547.......*....%...... +....&..489....931...........................$....356........................489..............302...............*............./...259.../996. +......./........*.................................*......385........63*911...*.........%........*578...633.175.........@804..421............ +..98...........188..........226....+...............909....*...................535.....798................$....*.796......................... +....*.....%...................+....531...563...........426......617*202.........................929-.......439....%..332.....+.23&.347...... +...364..149..............425............*.....662.481.......................788..$844..................506..........@.....693......&........ +..........................*....408...772....+.......-.......232*......*......*.............#.+294.....*.....706................435.......... +......566*972........631=.425.*...........14............984.....667..7.236....195..9.....222........757......*.....865..604&..*....344.&.... +.................439..........710..............477*714..............................*544........368............395*...........435...*..204.. +..........229......+..596.179........../................165.542*47........147..............653..*...................466...........119....... +.....690...@............@.......467..626........&......*.................*....=42.....724....+.816....262*.....237.@....549.383............. +......*.........750..............*..............202....967.914......279..................*........./......147.@...............*........480.. +...444.......84...#....+..........527......*351..............*......*................225..888.......806.............7......#...518.....*.... +.............*........628...............749......*976.200.....852..354...23....=........*...............................138............833.. +..........964.................................211...............................578.....986.............879......59......................... diff --git a/2023/3/src/main.rs b/2023/3/src/main.rs new file mode 100644 index 0000000..d832b1e --- /dev/null +++ b/2023/3/src/main.rs @@ -0,0 +1,310 @@ +use num::traits; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::ops::Range; + +// --- Day 3: Gear Ratios --- + +// You and the Elf eventually reach a gondola lift station; he says the gondola lift +// will take you up to the water source, but this is as far as he can bring you. You go +// inside. + +// It doesn't take long to find the gondolas, but there seems to be a problem: they're +// not moving. + +// "Aaah!" + +// You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. +// "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll +// still be a while before I can fix it." You offer to help. + +// The engineer explains that an engine part seems to be missing from the engine, but +// nobody can figure out which one. If you can add up all the part numbers in the engine +// schematic, it should be easy to work out which part is missing. + +// The engine schematic (your puzzle input) consists of a visual representation of the +// engine. There are lots of numbers and symbols you don't really understand, but +// apparently any number adjacent to a symbol, even diagonally, is a "part number" and +// should be included in your sum. (Periods (.) do not count as a symbol.) + +// Here is an example engine schematic: + +// 467..114.. ...*...... ..35..633. ......#... 617*...... .....+.58. ..592..... +// ......755. ...$.*.... .664.598.. + +// In this schematic, two numbers are not part numbers because they are not adjacent to +// a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a +// symbol and so is a part number; their sum is 4361. + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSING + +// We will store the schematic as a 2D Vector of char + +#[derive(Debug)] +struct Pos(usize, usize); + +// Outer Vec holds rows, so addressing is column then row +#[derive(Debug)] +struct SchematicRow(Vec); +#[derive(Debug)] +struct Schematic(Vec); + +impl From<&str> for SchematicRow { + fn from(s: &str) -> Self { + SchematicRow(s.chars().collect()) + } +} + +impl From for Schematic { + fn from(input: InputIter) -> Self { + Self(input.map(|line| line.unwrap().as_str().into()).collect()) + } +} + +const ADJACENCY_OFFSETS: &[(i64, i64)] = &[ + (-1, -1), + (0, -1), + (1, -1), + (-1, 0), + (1, 0), + (-1, 1), + (0, 1), + (1, 1), +]; + +// We assume the schematic is composed of equal length lines +impl Schematic { + fn in_bounds>(&self, x: T, y: T) -> bool { + let x_bound: T = self.0[0] + .0 + .len() + .try_into() + .unwrap_or_else(|_| panic!("bad bounds")); + let y_bound: T = self + .0 + .len() + .try_into() + .unwrap_or_else(|_| panic!("bad bounds")); + x >= T::zero() && x < x_bound && y >= T::zero() && y < y_bound + } + fn at(&self, pos: &Pos) -> char { + self.0[pos.1].0[pos.0] + } + + fn adjacent_to_symbol(&self, pos: &Pos) -> bool { + ADJACENCY_OFFSETS.iter().any(|(x_ofs, y_ofs)| { + let adj_x = (pos.0 as i64) + x_ofs; + let adj_y = (pos.1 as i64) + y_ofs; + + if !self.in_bounds(adj_x, adj_y) { + false + } else { + let adj_c = self.at(&Pos(adj_x as usize, adj_y as usize)); + adj_c != '.' && adj_c.is_ascii_punctuation() + } + }) + } + + fn is_gear(&self, pos: &Pos) -> bool { + if self.at(pos) != '*' { + return false; + } + + ADJACENCY_OFFSETS + .iter() + .filter(|(x_ofs, y_ofs)| { + let adj_x = (pos.0 as i64) + x_ofs; + let adj_y = (pos.1 as i64) + y_ofs; + + if adj_x < 0 + || adj_x >= self.0[pos.1].0.len() as i64 + || adj_y < 0 + || adj_y >= self.0.len() as i64 + { + false + } else { + let adj_c = self.at(&Pos(adj_x as usize, adj_y as usize)); + adj_c.is_digit(10) + } + }) + .count() + >= 2 + } + + fn gear_ratio(&self, pos: &Pos) -> Option { + if self.at(pos) != '*' { + return None; + } + + let mut exclusions: Vec<(Range, i64)> = Vec::new(); + let mut nums = (0u64, 0u64); + + for (x_ofs, y_ofs) in ADJACENCY_OFFSETS { + let adj_x = (pos.0 as i64) + x_ofs; + let adj_y = (pos.1 as i64) + y_ofs; + if !self.in_bounds(adj_x, adj_y) + || exclusions + .iter() + .any(|(ex_x_range, ex_y)| *ex_y == adj_y && ex_x_range.contains(&(adj_x as usize))) + { + continue; + } + let adj_c = self.at(&Pos(adj_x as usize, adj_y as usize)); + if adj_c.is_digit(10) { + let mut num_start_x = adj_x as usize; + loop { + if num_start_x > 0 + && self + .at(&Pos((num_start_x - 1) as usize, adj_y as usize)) + .is_digit(10) + { + num_start_x -= 1; + } else { + break; + } + } + let mut num_end_x = adj_x as usize; + loop { + if self.in_bounds(num_end_x + 1, adj_y as usize) + && self + .at(&Pos((num_end_x + 1) as usize, adj_y as usize)) + .is_digit(10) + { + num_end_x += 1; + } else { + break; + } + } + // we have the bounds for the number, extract a slice + let num_slice = &self.0[adj_y as usize].0[num_start_x..num_end_x + 1]; + let mut num = 0u64; + for c in num_slice { + num = num * 10 + c.to_digit(10).unwrap() as u64; + } + if nums.0 == 0 { + nums.0 = num + } else { + nums.1 = num; + return Some(nums.0 * nums.1); + } + exclusions.push((num_start_x..num_end_x + 1, adj_y)); + } + } + + None + } +} + +// PROBLEM 1 solution + +// Of course, the actual engine schematic is much larger. What is the sum of all of the +// part numbers in the engine schematic? + +fn problem1_all_valid_part_numbers(schematic: &Schematic) -> Vec { + let mut results = Vec::new(); + + let mut cur_pos = Pos(0usize, 0usize); + + while cur_pos.1 < schematic.0.len() { + while cur_pos.0 < schematic.0[0].0.len() { + let c = schematic.at(&cur_pos); + if c.is_digit(10) { + let mut valid: bool = schematic.adjacent_to_symbol(&cur_pos); + let mut num = c.to_digit(10).unwrap() as u64; + loop { + if cur_pos.0 + 1 >= schematic.0[0].0.len() { + break; + } + cur_pos.0 += 1; + if schematic.at(&cur_pos).is_digit(10) { + num = num * 10 + schematic.at(&cur_pos).to_digit(10).unwrap() as u64; + valid = valid || schematic.adjacent_to_symbol(&cur_pos); + } else { + break; + } + } + if valid { + results.push(num); + } + } + cur_pos.0 += 1; + } + cur_pos.1 += 1; + cur_pos.0 = 0; + } + + results +} + +fn problem1(input: InputIter) -> u64 { + let schematic: Schematic = input.into(); + problem1_all_valid_part_numbers(&schematic).iter().sum() +} + +// PROBLEM 2 solution + +// --- Part Two --- + +// The engineer finds the missing part and installs it in the engine! As the engine +// springs to life, you jump in the closest gondola, finally ready to ascend to the +// water source. + +// You don't seem to be going very fast, though. Maybe something is still wrong? +// Fortunately, the gondola has a phone labeled "help", so you pick it up and the +// engineer answers. + +// Before you can explain the situation, she suggests that you look out the window. +// There stands the engineer, holding a phone in one hand and waving with the other. +// You're going so slowly that you haven't even left the station. You exit the gondola. + +// The missing part wasn't the only issue - one of the gears in the engine is wrong. A +// gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is +// the result of multiplying those two numbers together. + +// This time, you need to find the gear ratio of every gear and add them all up so that +// the engineer can figure out which gear needs to be replaced. + +// Consider the same engine schematic again: + +// 467..114.. ...*...... ..35..633. ......#... 617*...... .....+.58. ..592..... +// ......755. ...$.*.... .664.598.. + +// In this schematic, there are two gears. The first is in the top left; it has part +// numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower +// right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is +// only adjacent to one part number.) Adding up all of the gear ratios produces 467835. + +// What is the sum of all of the gear ratios in your engine schematic? + +fn problem2_all_gear_ratios(schematic: &Schematic) -> Vec { + let mut gears = Vec::new(); + for y in 0..schematic.0.len() { + for x in 0..schematic.0[0].0.len() { + match schematic.gear_ratio(&Pos(x, y)) { + Some(ratio) => gears.push(ratio), + _ => () + } + } + } + + gears +} + +fn problem2(input: InputIter) -> u64 { + let schematic: Schematic = input.into(); + problem2_all_gear_ratios(&schematic).iter().sum() +} diff --git a/2023/4/Cargo.lock b/2023/4/Cargo.lock new file mode 100644 index 0000000..526614a --- /dev/null +++ b/2023/4/Cargo.lock @@ -0,0 +1,118 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "day4" +version = "0.1.0" +dependencies = [ + "lazy-regex", +] + +[[package]] +name = "lazy-regex" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/4/Cargo.toml b/2023/4/Cargo.toml new file mode 100644 index 0000000..a02af65 --- /dev/null +++ b/2023/4/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day4" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +lazy-regex = "3.1.0" diff --git a/2023/4/input b/2023/4/input new file mode 100644 index 0000000..7b9b368 --- /dev/null +++ b/2023/4/input @@ -0,0 +1,186 @@ +Card 1: 10 5 11 65 27 43 44 29 24 69 | 65 66 18 14 17 97 95 34 38 23 10 25 22 15 87 9 28 43 4 71 89 20 72 5 6 +Card 2: 25 43 15 31 45 19 36 73 34 85 | 92 11 85 68 74 20 19 71 1 36 43 32 77 33 14 31 73 15 45 83 34 25 6 88 57 +Card 3: 4 46 42 23 18 98 59 75 19 57 | 22 3 75 80 42 23 59 39 98 38 18 21 67 57 20 25 71 26 64 4 83 79 91 65 90 +Card 4: 92 13 56 27 19 44 70 93 32 66 | 38 4 19 75 87 93 32 1 23 14 51 22 42 44 33 63 13 56 70 66 18 92 47 53 27 +Card 5: 25 18 30 45 23 80 91 13 47 61 | 62 45 71 30 39 19 61 68 23 80 91 96 25 58 13 88 67 29 60 2 74 55 12 83 46 +Card 6: 20 36 26 18 61 35 94 58 79 19 | 85 68 56 7 53 58 19 20 79 59 26 61 18 15 94 33 29 71 35 89 17 36 3 67 72 +Card 7: 29 21 71 91 74 6 12 53 50 55 | 29 51 12 74 82 18 21 55 53 33 65 91 95 69 2 62 57 50 3 79 71 61 47 6 14 +Card 8: 24 72 46 88 65 12 48 97 76 73 | 17 97 87 18 83 11 49 44 88 79 64 55 19 25 63 74 73 14 82 15 72 43 76 48 46 +Card 9: 86 38 55 52 33 61 41 75 87 26 | 38 97 88 91 33 36 54 64 40 52 68 71 43 55 72 75 4 61 58 49 56 86 30 15 17 +Card 10: 15 37 72 5 66 81 33 6 80 76 | 52 34 21 68 72 82 91 81 7 19 86 67 94 87 16 63 43 9 14 23 44 60 18 4 74 +Card 11: 24 21 17 27 34 80 18 79 56 32 | 18 45 21 63 82 69 48 54 89 5 83 80 92 93 49 8 55 12 97 35 4 3 42 94 56 +Card 12: 90 61 45 54 64 93 69 85 58 88 | 79 89 74 45 27 61 9 56 69 88 13 80 66 4 16 62 34 51 71 54 48 64 58 63 50 +Card 13: 80 39 35 27 56 94 25 77 84 26 | 24 19 72 54 96 63 56 15 25 16 59 35 20 57 52 65 95 66 45 31 77 60 42 93 37 +Card 14: 33 83 94 41 7 79 18 40 24 97 | 49 27 45 21 20 8 93 26 12 42 94 33 96 57 98 91 19 52 78 2 18 80 65 72 44 +Card 15: 16 83 92 40 13 35 94 23 47 17 | 55 61 75 43 18 5 22 65 67 7 49 63 71 13 45 78 53 21 57 34 98 99 64 32 56 +Card 16: 14 76 72 8 42 61 60 82 24 21 | 56 10 38 11 37 45 29 22 51 98 86 49 26 13 16 52 50 14 70 30 87 73 66 77 88 +Card 17: 63 32 83 13 81 89 33 49 5 34 | 99 85 12 6 27 93 78 44 14 76 1 51 36 9 4 62 91 84 8 37 45 66 75 42 40 +Card 18: 43 97 37 61 69 94 60 67 91 99 | 87 25 65 98 26 62 42 11 61 32 34 76 14 20 44 92 22 5 12 46 21 33 3 15 59 +Card 19: 32 96 10 49 26 86 67 85 50 75 | 58 98 84 43 23 33 64 74 37 80 63 40 51 95 13 55 31 25 87 97 81 52 53 24 6 +Card 20: 7 70 41 40 16 15 20 72 47 93 | 93 41 7 79 62 86 69 70 75 67 51 72 81 15 22 48 47 80 71 38 40 97 20 82 16 +Card 21: 90 76 52 48 36 37 89 45 20 17 | 23 48 86 7 99 50 20 58 93 43 16 91 5 76 52 45 17 68 89 22 61 36 90 37 83 +Card 22: 85 71 20 58 32 83 53 94 61 80 | 88 59 66 80 49 53 19 97 5 20 62 8 94 54 67 73 93 12 10 61 32 41 71 72 85 +Card 23: 60 61 30 65 11 28 89 54 22 78 | 88 71 97 46 60 45 54 26 22 42 59 57 80 9 69 61 47 3 85 7 32 78 11 66 33 +Card 24: 35 16 5 85 87 58 66 39 88 12 | 51 32 59 41 73 21 58 79 72 10 74 28 95 69 16 92 88 18 34 46 77 17 65 76 99 +Card 25: 67 41 62 96 40 22 81 12 98 9 | 92 66 87 35 50 38 19 14 74 93 1 76 59 6 84 99 58 61 34 13 63 30 53 90 24 +Card 26: 10 78 28 1 72 94 60 71 37 89 | 71 9 28 11 4 72 43 90 74 37 18 14 40 16 21 97 59 13 73 89 63 60 5 10 70 +Card 27: 65 7 45 59 32 51 22 16 2 64 | 65 37 72 7 26 86 49 3 63 67 16 52 51 21 89 14 70 78 36 27 92 39 8 64 42 +Card 28: 89 17 93 57 78 33 52 97 53 4 | 23 20 66 55 42 28 52 61 35 41 60 51 84 83 49 67 22 5 48 9 2 7 87 38 93 +Card 29: 64 12 30 16 87 59 76 34 85 28 | 47 21 32 37 94 25 6 26 49 59 81 45 87 70 23 5 91 8 34 58 72 89 52 93 63 +Card 30: 11 69 86 19 59 28 41 68 81 66 | 92 33 1 43 88 75 42 29 38 55 24 89 4 95 47 70 30 65 83 72 25 82 14 85 21 +Card 31: 48 43 14 54 80 70 93 64 42 77 | 91 19 62 79 30 23 29 11 37 13 41 54 97 52 7 90 33 17 96 28 20 9 47 31 38 +Card 32: 78 91 2 56 93 3 88 50 20 12 | 2 20 18 84 12 82 43 76 39 53 32 79 57 17 63 28 30 9 1 55 96 70 29 25 87 +Card 33: 99 93 14 28 36 68 11 97 58 9 | 94 4 67 66 40 87 96 83 78 23 71 41 92 24 77 1 15 42 12 57 3 89 46 72 59 +Card 34: 20 88 61 67 31 21 90 30 26 56 | 6 71 18 34 66 10 41 37 58 32 13 76 3 96 86 47 50 91 17 15 39 68 73 27 12 +Card 35: 5 58 96 48 47 70 19 34 63 84 | 21 59 88 32 37 33 20 49 55 62 7 66 1 2 40 77 22 82 39 13 45 61 56 44 17 +Card 36: 36 66 44 13 33 2 60 46 4 31 | 73 99 13 72 44 80 60 77 65 19 46 4 59 97 61 98 31 92 83 2 68 36 66 33 22 +Card 37: 95 71 87 60 17 19 97 30 48 55 | 30 53 35 19 94 27 95 44 47 13 69 87 12 56 4 26 85 17 63 7 70 60 71 41 3 +Card 38: 61 16 20 92 98 19 79 62 45 91 | 79 44 60 23 25 83 92 19 16 38 66 48 75 20 2 98 24 87 45 62 61 32 91 69 13 +Card 39: 49 2 18 74 99 30 54 16 93 68 | 8 74 75 72 68 67 89 86 31 30 97 47 24 71 18 99 54 22 16 70 3 7 49 50 25 +Card 40: 12 45 7 15 65 84 77 36 28 1 | 51 35 63 43 59 81 88 58 56 71 94 46 9 86 28 13 30 99 20 17 95 23 32 54 47 +Card 41: 8 99 26 60 56 69 48 47 31 16 | 83 9 66 73 95 67 21 86 94 2 99 51 98 80 57 36 82 4 23 19 12 68 88 44 59 +Card 42: 99 48 94 50 91 55 2 12 67 36 | 47 45 62 66 31 84 26 20 52 41 40 55 21 35 37 15 69 53 72 65 79 60 25 48 57 +Card 43: 41 75 61 39 67 88 48 42 51 79 | 85 99 95 79 64 92 34 8 46 15 56 28 12 6 81 48 41 49 20 97 91 67 51 55 74 +Card 44: 10 99 33 96 21 6 49 62 82 38 | 68 41 88 24 6 28 98 66 96 49 30 33 94 85 1 75 21 61 60 99 89 81 31 54 20 +Card 45: 46 61 60 2 29 45 7 55 83 16 | 25 34 84 91 97 61 55 17 16 79 67 53 59 27 69 58 29 46 24 93 3 32 60 49 33 +Card 46: 93 25 42 68 22 85 58 61 63 50 | 8 46 4 15 96 83 94 13 19 65 24 80 64 38 89 44 52 78 98 36 47 51 97 2 57 +Card 47: 11 59 80 10 72 76 43 86 56 7 | 59 67 35 5 47 81 99 21 38 90 73 55 64 24 34 42 58 51 56 11 68 60 23 92 43 +Card 48: 97 88 28 44 24 10 17 27 83 12 | 67 92 74 50 4 35 77 47 6 40 11 20 76 16 42 9 3 79 68 82 43 41 60 94 89 +Card 49: 35 98 90 92 42 12 13 83 53 95 | 36 34 51 96 24 94 53 7 31 6 71 48 16 75 77 83 68 46 14 66 65 22 9 74 93 +Card 50: 1 65 66 2 76 36 78 56 99 90 | 12 13 3 38 96 20 10 14 57 81 7 35 88 55 68 48 31 52 89 9 70 15 79 80 16 +Card 51: 70 41 53 40 55 82 69 7 5 71 | 56 4 54 46 65 17 91 94 72 88 90 31 85 14 97 95 44 26 92 8 21 73 38 77 37 +Card 52: 62 16 85 42 56 1 82 92 43 37 | 74 28 19 42 82 21 85 37 75 50 17 16 1 76 56 86 91 31 3 92 6 62 43 64 83 +Card 53: 69 91 12 9 63 50 58 57 67 13 | 40 42 70 62 6 69 9 82 78 19 89 56 57 83 47 25 98 46 30 53 72 28 10 58 44 +Card 54: 73 44 64 21 16 50 63 41 36 51 | 6 4 66 39 21 14 67 50 15 1 35 40 64 44 24 73 28 47 80 13 2 43 91 46 16 +Card 55: 47 55 14 43 1 54 81 65 19 33 | 19 10 97 44 84 61 5 25 96 81 32 73 75 83 65 17 3 41 14 92 9 80 27 63 8 +Card 56: 43 26 93 75 33 51 89 60 1 81 | 9 10 16 60 84 81 93 26 4 96 17 91 79 18 43 67 47 92 1 33 21 63 51 75 89 +Card 57: 76 70 7 31 8 39 63 99 30 13 | 31 63 76 17 68 39 30 50 27 72 84 79 70 57 28 13 29 97 67 8 75 87 3 7 99 +Card 58: 28 29 86 65 7 36 38 3 20 43 | 70 20 65 64 43 98 18 19 78 87 7 55 30 35 28 37 56 29 39 38 3 40 36 86 75 +Card 59: 55 10 23 31 17 37 22 92 14 44 | 67 11 60 92 42 68 44 78 3 43 76 16 97 55 8 35 49 54 18 40 46 52 95 84 69 +Card 60: 34 13 63 27 44 11 80 9 43 28 | 48 44 95 59 41 13 30 40 73 33 10 19 74 14 78 12 37 28 35 63 87 39 53 11 93 +Card 61: 42 75 7 20 73 47 16 28 5 92 | 12 65 7 5 38 74 58 28 62 85 10 47 18 75 86 52 73 77 1 43 36 93 80 13 20 +Card 62: 47 35 92 12 61 25 83 96 6 87 | 5 21 56 66 24 13 19 54 68 92 87 12 93 35 88 11 49 79 69 31 42 96 47 59 84 +Card 63: 43 97 21 67 11 40 81 53 23 42 | 65 52 24 80 71 6 3 88 72 17 66 77 41 57 44 91 1 36 56 27 39 43 8 67 34 +Card 64: 96 73 20 69 19 93 98 99 41 24 | 20 19 52 63 60 34 8 40 14 6 89 1 94 62 98 96 22 71 65 35 28 18 25 59 42 +Card 65: 5 40 14 19 36 93 59 9 12 75 | 60 15 90 21 71 66 86 59 62 31 82 76 38 14 9 98 53 32 6 8 19 4 11 33 29 +Card 66: 97 35 40 5 62 15 79 72 38 32 | 69 8 94 75 35 9 64 96 42 76 67 91 31 92 52 71 77 45 18 12 61 25 88 20 50 +Card 67: 55 89 12 98 71 16 36 87 91 25 | 32 39 83 13 10 81 51 29 43 7 70 60 40 78 44 17 23 94 57 22 20 11 86 34 99 +Card 68: 53 37 26 55 34 39 62 35 68 86 | 44 85 58 41 42 31 52 35 66 49 77 65 27 60 7 2 45 21 26 40 20 18 69 4 99 +Card 69: 45 7 89 51 21 97 38 33 84 28 | 72 39 65 64 24 1 62 56 54 12 5 60 80 49 88 26 42 19 71 46 87 70 9 8 82 +Card 70: 50 92 10 16 56 40 14 6 61 30 | 11 68 37 3 74 32 58 66 18 20 69 33 22 19 97 7 53 39 64 91 57 55 51 87 83 +Card 71: 16 38 60 51 15 56 26 46 76 64 | 26 65 51 88 63 56 64 76 61 40 20 16 55 81 29 58 7 38 46 62 37 23 4 15 60 +Card 72: 48 3 37 4 61 7 63 69 18 38 | 7 63 22 48 8 91 2 77 40 44 69 86 51 74 37 53 38 96 49 3 4 61 18 15 43 +Card 73: 22 59 55 78 44 99 96 29 76 46 | 59 23 44 31 29 63 18 92 46 22 4 56 85 43 82 94 95 48 14 64 78 76 55 99 96 +Card 74: 49 46 66 52 68 64 62 93 81 96 | 83 31 15 98 68 17 74 79 81 20 25 66 16 53 43 46 30 21 24 70 63 1 82 71 72 +Card 75: 25 56 3 78 52 87 6 5 54 99 | 1 4 66 17 87 5 52 79 88 25 36 99 86 3 65 61 70 71 78 54 56 39 69 68 6 +Card 76: 62 66 78 11 51 71 42 84 39 33 | 26 29 45 7 32 58 83 72 19 82 67 91 76 59 39 93 41 5 12 47 79 49 21 11 20 +Card 77: 98 80 42 61 82 18 34 66 2 1 | 84 33 16 9 91 86 22 42 1 34 46 99 41 82 85 61 49 26 18 88 98 66 80 72 2 +Card 78: 74 69 73 62 50 86 14 34 15 57 | 73 40 62 15 52 9 68 82 46 86 89 22 83 74 50 58 38 8 34 16 14 69 57 64 60 +Card 79: 99 36 21 35 10 43 32 69 74 6 | 10 14 75 66 48 15 17 43 74 2 16 50 56 6 69 61 87 36 99 32 90 21 35 60 28 +Card 80: 63 3 55 47 54 20 95 72 36 57 | 80 57 63 66 47 44 49 38 3 54 15 52 11 75 16 72 50 55 36 95 33 81 26 45 20 +Card 81: 46 61 5 40 70 1 65 18 74 78 | 70 66 57 55 9 33 83 71 78 37 74 18 92 38 65 84 40 1 97 61 91 59 5 46 24 +Card 82: 85 25 81 91 76 71 8 12 24 21 | 37 76 74 34 85 91 44 41 99 8 25 28 98 39 24 12 71 75 95 47 21 69 46 81 40 +Card 83: 98 57 78 67 90 55 53 74 59 85 | 20 52 73 61 41 64 85 4 68 70 87 14 32 99 55 48 84 74 94 80 50 42 25 44 35 +Card 84: 22 56 11 9 47 49 1 91 78 55 | 52 28 97 67 44 37 4 61 36 53 96 30 2 19 9 43 29 15 49 41 68 72 7 38 94 +Card 85: 84 66 28 12 30 82 69 57 72 76 | 37 48 69 96 47 78 98 59 43 27 29 35 15 81 32 64 66 57 94 95 44 12 2 89 70 +Card 86: 47 5 23 50 56 45 25 54 60 42 | 34 71 3 94 42 46 43 5 39 83 95 27 40 18 13 14 47 59 65 12 38 11 15 82 28 +Card 87: 93 58 92 44 94 78 80 26 39 50 | 75 47 94 20 9 85 79 26 56 44 14 67 95 5 12 6 31 30 39 40 8 58 80 34 59 +Card 88: 61 20 91 64 69 87 50 2 8 45 | 61 14 28 67 11 91 82 51 18 52 66 24 62 59 92 69 2 49 3 46 42 35 4 27 20 +Card 89: 32 38 6 48 14 60 82 1 9 5 | 97 29 67 21 7 46 12 19 65 14 34 35 44 82 64 5 40 98 57 13 45 55 10 22 79 +Card 90: 89 54 9 27 86 29 17 26 20 47 | 46 51 40 60 2 41 59 89 79 47 73 52 15 29 81 84 96 22 44 13 10 72 87 28 86 +Card 91: 98 15 46 26 72 22 71 82 90 39 | 63 62 29 7 90 79 12 49 54 96 21 70 6 13 2 9 86 53 99 3 33 72 66 28 22 +Card 92: 65 33 98 12 70 18 47 71 69 59 | 58 66 85 27 26 43 87 51 39 83 73 33 17 54 90 5 99 29 8 49 34 71 45 19 78 +Card 93: 62 50 63 30 80 74 32 70 31 12 | 28 3 9 71 34 40 12 77 55 69 2 44 76 58 83 11 23 17 10 13 33 81 93 94 85 +Card 94: 21 23 14 63 32 5 70 79 81 44 | 4 1 18 35 64 58 80 42 47 92 25 78 82 11 9 83 65 86 17 90 69 56 98 85 84 +Card 95: 88 44 69 23 3 34 22 59 72 10 | 30 87 25 88 72 57 34 10 3 1 4 64 44 17 42 33 20 69 93 65 73 11 23 59 22 +Card 96: 51 94 55 41 65 57 59 98 39 96 | 38 54 83 39 96 94 88 65 32 41 9 55 51 73 93 59 58 63 64 98 45 57 8 33 74 +Card 97: 4 5 90 20 15 14 49 40 84 11 | 73 75 29 47 15 64 53 66 74 43 50 84 12 28 90 5 3 20 4 11 40 49 14 71 95 +Card 98: 74 49 44 95 50 71 77 22 41 57 | 4 75 47 3 45 82 58 53 88 69 59 17 51 68 66 67 36 25 7 14 78 12 49 73 9 +Card 99: 48 4 79 15 61 85 92 45 22 81 | 30 23 78 35 17 14 24 68 90 74 37 32 46 19 79 42 22 43 50 41 7 92 39 10 15 +Card 100: 26 67 9 17 82 11 34 47 10 20 | 56 10 73 34 59 62 97 67 12 48 47 20 17 90 7 40 50 13 55 9 27 65 31 38 26 +Card 101: 79 10 29 36 95 26 94 40 50 89 | 6 54 33 88 74 46 67 70 51 39 94 78 31 59 37 5 56 53 36 42 1 87 29 64 40 +Card 102: 50 40 34 30 2 32 95 70 10 16 | 57 66 90 83 94 31 79 67 14 93 28 52 32 22 9 43 54 39 49 40 35 21 87 78 88 +Card 103: 85 35 6 42 31 71 49 17 95 55 | 61 67 34 99 52 59 46 38 51 36 73 43 94 97 91 76 37 4 25 18 30 64 41 22 26 +Card 104: 89 73 65 69 94 68 28 49 19 55 | 14 33 51 92 40 70 73 91 45 20 72 21 69 43 19 52 56 27 4 37 67 1 13 80 83 +Card 105: 65 98 51 74 36 45 61 87 42 50 | 70 6 30 26 84 75 1 96 37 31 53 95 24 66 40 52 72 22 44 54 77 99 25 19 69 +Card 106: 35 46 52 34 28 3 36 42 80 10 | 53 43 20 22 62 75 83 11 44 85 78 39 72 90 73 18 86 16 65 19 98 47 41 45 88 +Card 107: 92 17 73 3 72 57 63 66 1 9 | 61 67 98 59 31 3 48 83 33 1 72 2 13 42 50 68 19 53 15 32 14 9 49 52 23 +Card 108: 9 27 28 88 4 82 63 73 36 92 | 99 45 20 57 31 94 7 22 39 55 86 79 95 62 96 58 97 12 15 56 34 40 17 51 33 +Card 109: 48 43 25 17 34 83 3 19 18 86 | 61 71 38 28 96 1 36 25 16 37 52 33 67 83 50 47 10 46 51 34 11 7 77 68 80 +Card 110: 36 41 13 69 51 66 48 79 60 93 | 94 80 37 81 66 97 17 73 13 35 27 67 88 29 84 57 96 77 12 75 33 85 15 82 58 +Card 111: 88 65 71 66 39 67 50 42 86 99 | 20 10 87 41 45 63 31 40 64 35 97 99 53 98 72 11 85 19 79 13 22 55 24 25 34 +Card 112: 75 37 68 18 90 88 42 20 50 40 | 31 5 15 80 33 66 35 69 64 97 11 92 85 94 24 65 44 22 71 36 17 12 29 39 98 +Card 113: 81 60 57 44 33 56 89 36 50 1 | 27 76 64 89 85 68 42 36 22 67 32 60 4 57 74 28 16 17 44 65 14 31 43 45 12 +Card 114: 34 92 35 43 65 63 29 49 48 13 | 13 16 12 31 14 75 2 30 3 37 28 11 91 50 69 72 87 52 95 85 58 94 49 19 23 +Card 115: 11 80 20 25 93 96 19 99 74 58 | 41 45 39 22 19 31 20 99 25 62 37 77 58 78 3 1 81 96 56 14 32 85 83 53 57 +Card 116: 86 83 81 62 55 80 77 46 53 38 | 62 80 30 83 70 54 38 51 25 46 84 81 27 53 58 55 56 41 9 77 22 49 18 86 78 +Card 117: 25 95 92 2 7 30 81 76 96 61 | 12 52 25 2 34 92 82 58 96 85 76 67 70 81 16 61 7 19 37 95 18 43 45 79 30 +Card 118: 91 45 52 34 15 28 48 83 67 38 | 98 6 92 67 38 79 34 4 80 48 58 91 88 72 3 33 52 73 45 89 28 63 41 54 17 +Card 119: 71 82 4 62 58 44 60 34 79 14 | 40 62 82 60 58 34 3 27 22 14 72 65 4 85 53 96 7 46 11 97 35 47 67 19 71 +Card 120: 63 80 9 4 94 46 66 61 81 76 | 47 35 81 21 73 23 63 38 55 65 94 76 54 15 87 43 8 72 70 75 9 3 66 80 85 +Card 121: 67 50 91 52 64 43 30 75 34 85 | 95 32 8 64 43 42 85 45 98 76 7 52 80 22 46 9 36 30 15 40 54 81 44 67 57 +Card 122: 19 62 83 56 46 1 47 64 25 23 | 67 50 30 79 58 90 39 80 69 20 91 37 94 65 88 98 18 92 73 33 66 44 70 11 64 +Card 123: 87 36 39 75 4 6 85 9 54 82 | 44 50 45 57 27 34 86 21 97 53 93 75 39 26 61 41 81 49 51 29 13 66 37 30 87 +Card 124: 74 32 61 26 52 21 3 30 5 58 | 38 69 20 31 68 60 44 61 97 79 29 66 23 62 37 94 33 54 96 71 87 1 9 50 11 +Card 125: 99 26 61 20 66 13 40 62 89 29 | 66 3 60 19 8 43 48 28 70 23 45 35 64 5 42 58 95 15 56 59 44 1 51 49 80 +Card 126: 51 1 62 49 98 34 36 74 73 17 | 67 92 29 10 21 59 91 89 11 15 83 9 63 99 41 60 31 73 78 25 72 96 35 87 71 +Card 127: 90 94 39 24 60 8 53 14 78 27 | 54 51 77 16 1 67 6 20 15 18 71 41 81 10 66 63 13 11 64 4 58 43 83 65 98 +Card 128: 43 40 76 96 65 82 15 5 23 16 | 70 24 56 40 52 71 96 7 87 91 11 75 43 41 5 45 30 33 34 15 80 27 61 76 17 +Card 129: 90 76 62 9 60 2 48 87 80 52 | 10 7 96 17 8 71 47 52 59 91 1 4 33 56 58 66 92 2 37 53 9 29 25 98 13 +Card 130: 88 44 46 35 38 33 86 60 40 83 | 86 92 43 88 40 26 80 4 11 54 46 44 50 60 58 33 35 83 82 48 67 18 55 89 38 +Card 131: 54 34 30 7 66 16 81 71 98 9 | 57 65 78 81 98 24 34 88 96 69 7 54 41 53 63 30 52 8 71 66 59 16 9 79 11 +Card 132: 51 40 52 69 64 83 5 39 2 58 | 58 5 88 83 93 56 78 50 40 63 39 62 89 52 75 81 6 43 65 55 69 2 64 33 51 +Card 133: 34 96 6 68 85 44 60 66 3 64 | 67 60 64 82 53 85 55 99 44 34 6 68 66 92 70 84 3 45 88 19 78 24 74 96 49 +Card 134: 60 85 79 76 9 68 49 67 5 44 | 79 2 68 59 76 14 3 96 67 13 7 73 88 4 77 71 43 44 11 5 27 95 60 80 20 +Card 135: 24 84 80 20 88 19 73 22 74 56 | 67 3 13 94 50 6 8 98 87 68 17 82 99 77 14 32 23 72 93 95 60 53 41 48 51 +Card 136: 40 21 95 86 63 78 61 49 14 67 | 92 84 85 79 55 56 96 52 34 22 57 26 65 93 43 5 50 8 36 94 46 30 97 64 80 +Card 137: 89 69 19 24 83 11 97 42 18 9 | 43 38 89 82 75 14 9 58 24 29 4 19 46 42 11 2 23 97 67 17 18 69 83 81 16 +Card 138: 70 72 41 78 17 85 93 97 99 73 | 11 93 97 81 52 17 49 10 91 20 99 58 13 89 74 47 70 23 65 90 7 4 64 28 85 +Card 139: 18 38 96 48 37 73 80 89 7 19 | 77 13 3 51 4 45 76 21 93 33 43 47 57 17 24 11 35 75 59 60 44 5 90 55 32 +Card 140: 52 2 39 85 65 97 5 87 20 90 | 42 26 34 60 65 50 90 94 23 84 73 16 89 37 79 3 17 55 33 14 9 98 70 96 56 +Card 141: 85 56 58 55 62 47 92 76 95 89 | 41 58 67 78 4 61 10 17 23 1 50 21 18 90 45 94 15 37 48 31 91 54 73 35 64 +Card 142: 71 70 76 83 96 52 33 66 6 50 | 16 82 5 21 55 80 51 52 72 65 1 86 15 11 54 57 77 70 59 18 48 12 78 67 25 +Card 143: 89 67 6 13 59 12 81 61 57 25 | 27 37 13 70 68 52 82 2 74 50 92 5 85 78 93 95 34 91 87 36 55 39 32 8 48 +Card 144: 69 50 46 63 88 9 7 20 49 79 | 5 91 95 71 86 32 66 76 56 33 96 15 42 22 34 27 6 67 43 19 36 23 63 20 75 +Card 145: 69 23 76 4 13 41 50 78 17 60 | 16 57 68 43 34 11 18 66 72 7 82 5 19 91 61 36 70 15 87 59 65 21 67 2 8 +Card 146: 27 79 9 28 56 36 48 31 20 6 | 78 94 48 1 97 12 99 19 74 47 30 98 14 70 44 23 35 42 73 60 17 29 89 58 95 +Card 147: 46 80 82 8 50 77 28 85 38 41 | 27 42 57 31 45 76 67 53 13 94 68 90 89 16 11 35 51 92 10 66 63 52 6 83 65 +Card 148: 80 44 24 98 26 78 4 1 55 88 | 53 8 55 79 86 16 77 93 88 58 23 44 64 73 11 83 24 29 26 74 13 40 21 62 9 +Card 149: 81 29 26 20 92 62 50 49 69 74 | 10 26 69 96 27 74 44 70 32 31 8 95 25 90 3 89 35 1 40 20 49 29 58 59 80 +Card 150: 32 43 16 36 73 18 22 30 63 38 | 38 91 18 45 67 22 96 32 71 5 95 93 28 69 33 34 14 72 36 11 35 83 43 70 30 +Card 151: 65 61 49 29 51 72 96 71 37 94 | 33 42 51 94 62 91 29 96 61 35 92 37 30 8 24 77 72 58 23 64 65 73 49 46 71 +Card 152: 32 35 90 5 81 84 76 63 41 52 | 11 76 29 77 81 80 4 18 16 88 90 98 41 35 43 84 28 52 63 1 65 5 72 32 56 +Card 153: 13 34 18 11 77 61 94 88 7 90 | 46 25 17 93 5 43 72 28 69 1 14 55 66 63 53 62 26 68 13 37 70 78 65 34 16 +Card 154: 74 84 32 51 5 93 4 13 87 83 | 85 62 75 36 10 43 68 47 90 79 6 66 26 67 34 60 65 1 25 81 98 91 16 28 82 +Card 155: 60 7 42 76 1 18 29 93 19 83 | 49 97 62 78 15 99 71 94 25 41 28 4 51 6 3 80 87 81 75 12 26 89 91 2 17 +Card 156: 95 57 27 1 97 33 50 38 22 64 | 38 65 67 27 85 57 3 97 60 21 34 79 20 89 86 22 13 8 64 50 11 39 41 12 2 +Card 157: 59 52 71 70 92 48 42 17 75 79 | 86 75 5 68 16 55 72 70 94 60 6 1 79 31 42 61 59 38 52 97 92 53 81 67 17 +Card 158: 31 58 68 7 24 18 30 32 19 92 | 57 2 92 33 60 84 76 51 79 31 42 99 81 3 88 17 47 67 48 13 6 28 20 23 56 +Card 159: 13 42 26 92 53 33 44 45 19 90 | 59 95 2 51 25 81 17 30 3 71 36 22 58 90 33 52 8 92 37 6 11 19 45 96 88 +Card 160: 94 76 70 63 43 53 59 19 20 64 | 5 68 35 74 79 19 62 40 33 11 63 69 60 24 1 85 96 55 91 70 10 12 71 50 42 +Card 161: 34 29 98 99 81 52 96 77 57 39 | 78 96 67 3 5 20 85 10 58 79 45 62 46 92 15 97 88 2 57 40 81 74 69 61 32 +Card 162: 38 24 86 51 80 1 16 83 58 34 | 77 58 45 18 80 39 31 23 11 13 46 28 65 62 60 33 20 6 10 99 37 35 40 91 64 +Card 163: 14 97 5 33 34 96 50 47 55 74 | 6 66 77 79 37 45 39 82 90 9 50 38 14 31 19 13 29 15 89 30 17 88 12 44 53 +Card 164: 56 31 76 6 69 27 65 74 39 49 | 4 89 2 75 20 12 53 73 14 40 86 24 82 11 88 90 57 22 35 54 30 64 15 32 41 +Card 165: 62 75 48 65 43 85 8 80 45 91 | 84 74 86 98 57 38 69 78 28 22 7 19 83 60 3 55 23 34 94 13 2 58 90 11 39 +Card 166: 17 21 56 71 10 84 50 83 25 61 | 39 83 68 27 43 4 15 10 21 25 54 7 17 29 14 28 56 64 24 58 33 62 44 22 48 +Card 167: 24 89 68 2 90 36 25 82 38 59 | 28 24 83 43 46 7 56 2 38 89 6 61 59 31 50 68 32 25 36 9 88 19 82 90 14 +Card 168: 58 22 65 47 99 85 72 29 25 12 | 11 21 39 51 26 77 2 31 99 33 15 95 4 62 69 22 38 50 97 20 55 72 8 12 89 +Card 169: 99 18 97 9 10 76 72 75 26 87 | 44 94 63 21 54 96 19 8 50 9 86 16 49 41 60 5 57 7 38 27 95 12 40 85 1 +Card 170: 43 35 76 13 27 60 70 54 23 83 | 46 56 59 54 62 70 76 29 23 83 13 27 68 15 11 7 60 4 28 43 61 33 35 47 99 +Card 171: 27 58 57 48 76 97 1 86 25 40 | 36 32 61 74 82 31 38 79 33 34 46 2 27 89 50 10 22 55 72 91 39 64 43 98 42 +Card 172: 12 13 84 55 27 67 10 78 11 16 | 24 9 11 96 12 60 46 6 76 31 85 49 53 8 64 74 65 15 18 90 82 67 16 57 73 +Card 173: 60 84 36 23 82 27 14 54 74 9 | 9 37 31 62 91 14 74 13 39 38 23 87 1 84 80 92 30 54 32 60 36 48 61 82 27 +Card 174: 25 21 34 45 32 86 99 42 72 98 | 95 34 91 32 25 86 98 21 80 75 84 70 7 99 72 45 17 40 79 63 1 19 61 42 87 +Card 175: 83 24 18 84 45 38 23 42 56 14 | 14 76 98 92 46 44 97 20 13 64 72 96 16 68 57 21 6 34 3 19 55 89 9 83 1 +Card 176: 25 71 31 38 3 55 96 76 6 69 | 63 66 19 98 27 87 96 8 54 31 67 46 53 33 28 12 99 88 80 17 13 71 93 14 1 +Card 177: 38 25 67 43 18 3 16 72 57 51 | 79 52 12 72 23 56 77 80 31 42 18 99 83 60 24 33 64 32 75 85 9 90 43 8 1 +Card 178: 39 62 57 1 11 79 8 99 56 52 | 20 81 94 41 95 1 39 82 5 97 7 68 24 64 99 98 67 53 9 86 33 43 17 46 88 +Card 179: 19 63 34 49 71 38 94 17 1 33 | 86 35 94 70 38 10 33 99 54 2 39 11 92 91 34 43 67 18 12 15 95 1 23 20 49 +Card 180: 45 81 55 44 80 73 7 25 31 59 | 83 93 49 20 72 29 92 35 91 89 52 70 27 75 48 33 21 41 46 74 56 4 6 87 36 +Card 181: 42 34 76 85 33 27 66 79 58 73 | 80 71 26 6 41 39 68 36 15 19 13 33 34 62 82 88 10 3 76 46 51 99 78 85 72 +Card 182: 68 80 84 58 75 67 44 92 18 65 | 34 89 9 87 40 88 72 73 33 74 11 6 69 4 63 70 86 2 7 82 66 81 24 77 22 +Card 183: 60 22 7 19 93 32 31 23 36 41 | 6 35 77 49 29 45 39 21 57 61 22 15 70 48 94 53 31 18 87 99 52 3 62 67 33 +Card 184: 95 26 39 98 51 33 67 43 59 11 | 19 44 30 10 18 47 57 95 25 78 53 61 2 87 88 22 37 45 75 83 29 34 48 97 84 +Card 185: 7 16 46 63 13 2 99 9 93 26 | 37 28 50 41 55 75 73 6 96 82 17 92 87 10 49 72 15 86 64 36 95 32 13 5 53 +Card 186: 14 21 68 8 64 78 15 89 19 59 | 43 22 10 85 63 60 90 62 97 17 33 39 7 6 58 51 47 54 11 50 36 2 31 46 34 diff --git a/2023/4/src/main.rs b/2023/4/src/main.rs new file mode 100644 index 0000000..feb8296 --- /dev/null +++ b/2023/4/src/main.rs @@ -0,0 +1,246 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +use lazy_regex::{lazy_regex, Lazy, Regex}; + +// --- Day 4: Scratchcards --- + +// The gondola takes you up. Strangely, though, the ground doesn't seem to be coming +// with you; you're not climbing a mountain. As the circle of Snow Island recedes below +// you, an entire new landmass suddenly appears above you! The gondola carries you to +// the surface of the new island and lurches into the station. + +// As you exit the gondola, the first thing you notice is that the air here is much +// warmer than it was on Snow Island. It's also quite humid. Is this where the water +// source is? + +// The next thing you notice is an Elf sitting on the floor across the station in what +// seems to be a pile of colorful square cards. + +// "Oh! Hello!" The Elf excitedly runs over to you. "How may I be of service?" You ask +// about water sources. + +// "I'm not sure; I just operate the gondola lift. That does sound like something we'd +// have, though - this is Island Island, after all! I bet the gardener would know. He's +// on a different island, though - er, the small kind surrounded by water, not the +// floating kind. We really need to come up with a better naming scheme. Tell you what: +// if you can help me with something quick, I'll let you borrow my boat and you can go +// visit the gardener. I got all these scratchcards as a gift, but I can't figure out +// what I've won." + +// The Elf leads you over to the pile of colorful cards. There, you discover dozens of +// scratchcards, all with their opaque covering already scratched off. Picking one up, +// it looks like each card has two lists of numbers separated by a vertical bar (|): a +// list of winning numbers and then a list of numbers you have. You organize the +// information into a table (your puzzle input). + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSING + +const CARD_REGEX: Lazy = lazy_regex!(r"^Card\s+([0-9]+):(.+)\|(.+)$"); + +fn num_list(s: &str) -> Vec { + s.split_whitespace() + .map(|s_num| s_num.parse().unwrap()) + .collect() +} + +pub struct Card { + id: usize, + winning_numbers: Vec, + our_numbers: Vec, +} + +impl Card { + fn score(&self) -> u64 { + let match_count = self + .winning_numbers + .iter() + .filter(|win| self.our_numbers.contains(win)) + .count(); + if match_count > 0 { + 1 << (match_count - 1) + } else { + 0 + } + } + // Return the number of copies won + fn win_copies(&self) -> usize { + self.winning_numbers + .iter() + .filter(|win| self.our_numbers.contains(win)) + .count() + } +} + +impl From<&str> for Card { + fn from(s: &str) -> Self { + let parts = CARD_REGEX.captures(s).unwrap(); + Card { + id: parts.get(1).unwrap().as_str().parse().unwrap(), + winning_numbers: num_list(parts.get(2).unwrap().as_str()), + our_numbers: num_list(parts.get(3).unwrap().as_str()), + } + } +} + +// PROBLEM 1 solution + +// As far as the Elf has been able to figure out, you have to figure out which of the +// numbers you have appear in the list of winning numbers. The first match makes the +// card worth one point and each match after the first doubles the point value of that +// card. + +// For example: + +// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +// Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +// Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +// Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +// Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +// Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 + +// In the above example, card 1 has five winning numbers (41, 48, 83, 86, and 17) and +// eight numbers you have (83, 86, 6, 31, 17, 9, 48, and 53). Of the numbers you have, +// four of them (48, 83, 17, and 86) are winning numbers! That means card 1 is worth 8 +// points (1 for the first match, then doubled three times for each of the three matches +// after the first). + +// Card 2 has two winning numbers (32 and 61), so it is worth 2 points. Card 3 has +// two winning numbers (1 and 21), so it is worth 2 points. Card 4 has one winning +// number (84), so it is worth 1 point. Card 5 has no winning numbers, so it is +// worth no points. Card 6 has no winning numbers, so it is worth no points. + +// So, in this example, the Elf's pile of scratchcards is worth 13 points. + +// Take a seat in the large pile of colorful cards. How many points are they worth in +// total? + +fn problem1(input: Lines) -> u64 { + let cards: Vec = input + .map(|line| Card::from(line.unwrap().as_str())) + .collect(); + + cards.iter().map(|x| x.score()).sum() +} + +// PROBLEM 2 solution + +// --- Part Two --- + +// Just as you're about to report your findings to the Elf, one of you realizes that the +// rules have actually been printed on the back of every card this whole time. + +// There's no such thing as "points". Instead, scratchcards only cause you to win more +// scratchcards equal to the number of winning numbers you have. + +// Specifically, you win copies of the scratchcards below the winning card equal to the +// number of matches. So, if card 10 were to have 5 matching numbers, you would win one +// copy each of cards 11, 12, 13, 14, and 15. + +// Copies of scratchcards are scored like normal scratchcards and have the same card +// number as the card they copied. So, if you win a copy of card 10 and it has 5 +// matching numbers, it would then win a copy of the same cards that the original card +// 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies +// cause you to win any more cards. (Cards will never make you copy a card past the end +// of the table.) + +// This time, the above example goes differently: + +// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 +// 17 32 24 19 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 Card 4: 41 92 73 84 69 | +// 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 Card 6: 31 +// 18 13 56 72 | 74 77 10 23 35 67 36 11 + +// Card 1 has four matching numbers, so you win one copy each of the next four +// cards: cards 2, 3, 4, and 5. Your original card 2 has two matching numbers, so +// you win one copy each of cards 3 and 4. Your copy of card 2 also wins one copy +// each of cards 3 and 4. Your four instances of card 3 (one original and three +// copies) have two matching numbers, so you win four copies each of cards 4 and 5. +// Your eight instances of card 4 (one original and seven copies) have one matching +// number, so you win eight copies of card 5. Your fourteen instances of card 5 (one +// original and thirteen copies) have no matching numbers and win no more cards. +// Your one instance of card 6 (one original) has no matching numbers and wins no +// more cards. + +// Once all of the originals and copies have been processed, you end up with 1 instance +// of card 1, 2 instances of card 2, 4 instances of card 3, 8 instances of card 4, 14 +// instances of card 5, and 1 instance of card 6. In total, this example pile of +// scratchcards causes you to ultimately have 30 scratchcards! + +// Process all of the original and copied scratchcards until no more scratchcards are +// won. Including the original set of scratchcards, how many total scratchcards do you +// end up with? + +fn problem2_award_copies<'a>(orig_cards: &'a Vec, card: &Card) -> Vec<&'a Card> { + let win_copies = card.win_copies(); + let mut new_cards = Vec::new(); + if win_copies != 0 { + for ofs in 0..win_copies { + // card.id is 1-indexed, so would need to be card.id-1, but offset needs to + // start at 1, which offsets this + new_cards.push(&orig_cards[card.id + ofs]); + } + } + + new_cards +} + +fn problem2(input: Lines) -> u64 { + let orig_cards: Vec = input + .map(|line| Card::from(line.unwrap().as_str())) + .collect(); + + let mut new_cards = Vec::new(); + let mut won_cards = Vec::new(); + + for card in &orig_cards { + new_cards.append(&mut problem2_award_copies(&orig_cards, card)) + } + + while let Some(card) = new_cards.pop() { + won_cards.push(card); + new_cards.append(&mut problem2_award_copies(&orig_cards, card)) + } + + (won_cards.len() + orig_cards.len()) as u64 +} + +#[cfg(test)] +mod tests { + use crate::{problem1, problem2}; + use std::io::{BufRead, Cursor}; + + const example_input: &str = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 +Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 +Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 +Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"; + + #[test] + fn problem1_example_test() { + let input = Cursor::new(example_input).lines(); + + assert_eq!(problem1(input), 13); + } + + #[test] + fn problem2_example_test() { + let input = Cursor::new(example_input).lines(); + assert_eq!(problem2(input), 30); + } +} diff --git a/2023/5/Cargo.lock b/2023/5/Cargo.lock new file mode 100644 index 0000000..5861f03 --- /dev/null +++ b/2023/5/Cargo.lock @@ -0,0 +1,183 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "day5" +version = "0.1.0" +dependencies = [ + "itertools", + "num", + "test-case", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "test-case" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2550dd13afcd286853192af8601920d959b14c401fcece38071d53bf0768a8" +dependencies = [ + "test-case-macros", +] + +[[package]] +name = "test-case-core" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcb7fd841cd518e279be3d5a3eb0636409487998a4aff22f3de87b81e88384f" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "test-case-macros" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "test-case-core", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/5/Cargo.toml b/2023/5/Cargo.toml new file mode 100644 index 0000000..c3f201a --- /dev/null +++ b/2023/5/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day5" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" +num = "0.4.1" +test-case = "3.3.1" diff --git a/2023/5/input b/2023/5/input new file mode 100644 index 0000000..7289e4e --- /dev/null +++ b/2023/5/input @@ -0,0 +1,188 @@ +seeds: 2494933545 159314859 4045092792 172620202 928898138 554061882 2740120981 81327018 2031777983 63513119 2871914181 270575980 2200250633 216481794 3289604059 25147787 3472625834 10030240 260990830 232636388 + +seed-to-soil map: +3272284283 2724782980 1022683013 +138187491 4195038636 99928660 +2359623759 797621236 127984779 +662451929 2224466386 266466256 +928918185 714355413 83265823 +1012184008 3891516474 303522162 +3063776460 1098322140 208507823 +2194238166 1306829963 50525692 +357106588 2091837170 132629216 +2244763858 2490932642 114859901 +2050187685 3747465993 144050481 +489735804 925606015 172716125 +2487608538 138187491 576167922 +238116151 2605792543 118990437 +1315706170 1357355655 734481515 + +soil-to-fertilizer map: +4265669768 2142212766 29297528 +2030756625 2171510294 69737894 +3038084234 3411621093 262803613 +2410534622 3266307064 145314029 +2667304792 2241248188 370779442 +2100494519 3921619167 310040103 +3611390334 2612027630 654279434 +2555848651 2030756625 111456141 +733063720 869238953 195075492 +3300887847 4231659270 63308026 +3364195873 3674424706 247194461 +928139212 733063720 136175233 + +fertilizer-to-water map: +0 772139976 154052576 +909628165 428370542 51644443 +3172969725 4109584032 185383264 +1116931128 1046566515 14194115 +223777814 10055892 255169216 +2512535520 1229983026 60386000 +3109777744 3899207072 16374329 +4030761870 3829858282 12540292 +828135093 718323602 53816374 +2358450176 2554590817 154085344 +3126152073 3152512175 46817652 +4043302162 3842398574 56808498 +3694349069 3493296400 336412801 +770911368 661099877 57223725 +3574254366 3032417472 120094703 +2689829955 2124052738 139986329 +3358352989 1925653441 3542661 +154052576 1060760630 69725238 +3460616091 3829709201 149081 +881951467 480014985 9494517 +1777535488 3915581401 135358522 +2829816284 1290369026 84715328 +478947030 489509502 171590375 +2686813330 1226966401 3016625 +3361895650 3199329827 98720441 +1226966401 1375084354 550569087 +3515610257 4050939923 58644109 +650537405 926192552 120373963 +4100110660 1929196102 194856636 +1912894010 2708676161 323741311 +3460765172 2264039067 54845085 +898932898 0 10055892 +891445984 265225108 7486914 +2914531612 3298050268 195246132 +2572921520 2318884152 113891810 +961272608 272712022 155658520 +908988790 1130485868 639375 +2236635321 2432775962 121814855 + +water-to-light map: +2821176146 2286693663 106119314 +3822234587 2463633329 180779736 +1725724347 2842879211 104224606 +3308097155 4172728180 122239116 +3299768179 2834550235 8328976 +525232540 357109336 38255672 +751267412 803626289 867213460 +2181067610 2392812977 70820352 +2251887962 2644413065 134698828 +3430336271 3994876090 163182805 +357109336 395365008 62712446 +3646455511 2158365540 128328123 +3084834769 2947103817 214933410 +1618480872 3954660777 40215313 +1673365470 751267412 52358877 +2927295460 3797121468 157539309 +2386586790 1723776184 434589356 +3774783634 2779111893 47450953 +4003014323 3162037227 291952973 +419821782 458077454 105410758 +3593519076 1670839749 52936435 +1658696185 4158058895 14669285 +2173080221 2826562846 7987389 +1829948953 3453990200 343131268 + +light-to-temperature map: +457330729 4090205185 204762111 +2982196520 3401667644 30193953 +2238727594 3778270640 263367024 +2540710222 1921368253 380651678 +2224576409 4041637664 14151185 +1803946096 1170025919 125923944 +947791690 3069412788 65888847 +3354708582 3597834895 180435745 +1173448701 516297801 630497395 +4079424710 2899828022 88028778 +1929870040 1295949863 294706369 +928458849 495946333 19332841 +3145865299 515279174 1018627 +3071616223 2825578946 74249076 +3012390473 3431861597 52676537 +0 156654134 174053721 +1013680537 1590656232 159768164 +2502094618 457330729 38615604 +4167453488 3484538134 113296761 +4058703342 3048691420 20721368 +3146883926 1750424396 164394644 +662092840 3135301635 266366009 +174053721 0 156654134 +4280750249 4055788849 14217047 +3311278570 1146795196 23230723 +3334509293 4070005896 20199289 +3535144327 2302019931 523559015 +3065067010 1914819040 6549213 +2921361900 2987856800 60834620 + +temperature-to-humidity map: +19014508 1616728169 261978440 +479364011 879054632 183139707 +3422279791 4197415651 97551645 +2947838505 2859883311 474441286 +3922771609 3441941550 130194267 +1538347549 1285663854 77335299 +4074125861 3334324597 17231539 +2692139672 3923798143 165313419 +3867887507 3572135817 45827004 +4091357400 2695950683 148956394 +280992948 874337342 4717290 +3585981058 4137845928 59569723 +1194193608 267159640 344153941 +3645550781 2226309992 39998882 +2606341883 3617962821 85797789 +2452453972 3769910232 153887911 +4279991062 2844907077 14976234 +4240313794 4089111562 39677268 +285710238 0 193653773 +662503718 1585706204 31021965 +4052965876 2266308874 21159985 +2226309992 2287468859 226143980 +0 248145132 19014508 +3685549663 2513612839 182337844 +2857453091 3351556136 90385414 +1139702249 193653773 54491359 +3913714511 4128788830 9057098 +916232734 1062194339 223469515 +1615682848 611313581 263023761 +693525683 1362999153 222707051 +3519831436 3703760610 66149622 + +humidity-to-location map: +3722067319 3568864729 46052123 +761939125 1263883488 182519766 +3952597071 3400791743 168072986 +1928058 204065059 218803536 +1797120632 863951513 248903371 +3409129274 3614916852 109595510 +0 1261955430 1928058 +3854871689 2940386871 97725382 +3518724784 3898809601 159455982 +220731594 1446403254 100124613 +944458891 1813379640 207908225 +3194065032 4058265583 117770911 +3059317673 3222157831 134747359 +3678180766 3356905190 43886553 +3375153977 3124864500 33975297 +2940386871 4176036494 118930802 +1152367116 2021287865 24736138 +3311835943 3158839797 63318034 +320856207 422868595 441082918 +1593055573 0 204065059 +4120670057 3724512362 174297239 +1177103254 1112854884 149100546 +1326203800 1546527867 266851773 +3768119442 3038112253 86752247 diff --git a/2023/5/src/main.rs b/2023/5/src/main.rs new file mode 100644 index 0000000..64bf3a8 --- /dev/null +++ b/2023/5/src/main.rs @@ -0,0 +1,523 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::ops::Range; +use std::time::Instant; + +use itertools::Itertools; + +// --- Day 5: If You Give A Seed A Fertilizer --- + +// You take the boat and find the gardener right where you were told he would be: +// managing a giant "garden" that looks more to you like a farm. + +// "A water source? Island Island is the water source!" You point out that Snow Island +// isn't receiving any water. + +// "Oh, we had to stop the water because we ran out of sand to filter it with! Can't +// make snow with dirty water. Don't worry, I'm sure we'll get more sand soon; we only +// turned off the water a few days... weeks... oh no." His face sinks into a look of +// horrified realization. + +// "I've been so busy making sure everyone here has food that I completely forgot to +// check why we stopped getting more sand! There's a ferry leaving soon that is headed +// over in that direction - it's much faster than your boat. Could you please go check +// it out?" + +// You barely have time to agree to this request when he brings up another. "While you +// wait for the ferry, maybe you can help us with our food production problem. The +// latest Island Island Almanac just arrived and we're having trouble making sense of +// it." + +// The almanac (your puzzle input) lists all of the seeds that need to be planted. It +// also lists what type of soil to use with each kind of seed, what type of fertilizer +// to use with each kind of soil, what type of water to use with each kind of +// fertilizer, and so on. Every type of seed, soil, fertilizer and so on is identified +// with a number, but numbers are reused by each category - that is, soil 123 and +// fertilizer 123 aren't necessarily related to each other. + +// For example: + +// seeds: 79 14 55 13 +// +// seed-to-soil map: +// 50 98 2 +// 52 50 48 +// +// soil-to-fertilizer map: +// 0 15 37 +// 37 52 2 +// 39 0 15 +// +// fertilizer-to-water map: +// 49 53 8 +// 0 11 42 +// 42 0 7 +// 57 7 4 +// +// water-to-light map: +// 88 18 7 +// 18 25 70 +// +// light-to-temperature map: +// 45 77 23 +// 81 45 19 +// 68 64 13 +// +// temperature-to-humidity map: +// 0 69 1 +// 1 0 69 +// +// humidity-to-location map: +// 60 56 37 +// 56 93 4 + +// The almanac starts by listing which seeds need to be planted: seeds 79, 14, 55, and 13. + +// The rest of the almanac contains a list of maps which describe how to convert numbers +// from a source category into numbers in a destination category. That is, the section +// that starts with seed-to-soil map: describes how to convert a seed number (the +// source) to a soil number (the destination). This lets the gardener and his team know +// which soil to use with which seeds, which water to use with which fertilizer, and so +// on. + +// Rather than list every source number and its corresponding destination number one by +// one, the maps describe entire ranges of numbers that can be converted. Each line +// within a map contains three numbers: the destination range start, the source range +// start, and the range length. + +// Consider again the example seed-to-soil map: + +// 50 98 2 +// 52 50 48 + +// The first line has a destination range start of 50, a source range start of 98, and a +// range length of 2. This line means that the source range starts at 98 and contains +// two values: 98 and 99. The destination range is the same length, but it starts at 50, +// so its two values are 50 and 51. With this information, you know that seed number 98 +// corresponds to soil number 50 and that seed number 99 corresponds to soil number 51. + +// The second line means that the source range starts at 50 and contains 48 values: 50, +// 51, ..., 96, 97. This corresponds to a destination range starting at 52 and also +// containing 48 values: 52, 53, ..., 98, 99. So, seed number 53 corresponds to soil +// number 55. + +// Any source numbers that aren't mapped correspond to the same destination number. So, +// seed number 10 corresponds to soil number 10. + +// So, the entire list of seed numbers and their corresponding soil numbers looks like +// this: + +// seed soil +// 0 0 +// 1 1 +// ... ... +// 48 48 +// 49 49 +// 50 52 +// 51 53 +// ... ... +// 96 98 +// 97 99 +// 98 50 +// 99 51 + +// With this map, you can look up the soil number required for each initial seed number: + +// Seed number 79 corresponds to soil number 81. +// Seed number 14 corresponds to soil number 14. +// Seed number 55 corresponds to soil number 57. +// Seed number 13 corresponds to soil number 13. + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PARSING + +#[derive(Debug)] +struct AlmanacMapping { + source_range: Range, + dest_range: Range, +} + +impl From<&str> for AlmanacMapping { + fn from(s: &str) -> Self { + let s_nums: Vec<_> = s.split_whitespace().take(3).collect(); + let length: i64 = s_nums[2].parse().unwrap(); + let source_start: i64 = s_nums[1].parse().unwrap(); + let dest_start: i64 = s_nums[0].parse().unwrap(); + AlmanacMapping { + source_range: source_start..source_start + length, + dest_range: dest_start..dest_start + length, + } + } +} + +impl AlmanacMapping { + fn lookup(&self, key: i64) -> Option { + if self.source_range.contains(&key) { + Some(self.dest_range.start + (key - self.source_range.start)) + } else { + None + } + } +} + +#[derive(Debug)] +struct AlmanacMappingTable { + name: String, + mappings: Vec, +} + +fn range_overlap(r1: &Range, r2: &Range) -> Option> { + let new_start = std::cmp::max(r1.start, r2.start); + let new_end = std::cmp::min(r1.end - T::one(), r2.end - T::one()); + + if new_start <= std::cmp::min(r1.end - T::one(), r2.end - T::one()) + && new_end >= std::cmp::max(r1.start, r2.start) + { + Some(new_start..new_end + T::one()) + } else { + None + } +} + +fn range_exclude(keep: &Range, exclude: &Range) -> Vec> { + let mut residual = Vec::new(); + if let Some(overlap) = range_overlap(keep, exclude) { + if keep.start < overlap.start { + residual.push(keep.start..overlap.start); + } + if keep.end > overlap.end { + residual.push(overlap.end..keep.end); + } + } else { + residual.push(keep.clone()); + } + residual +} + +impl AlmanacMappingTable { + fn lookup(&self, key: i64) -> i64 { + self.mappings + .iter() + .find_map(|map| map.lookup(key)) + .unwrap_or(key) + } + fn lookup_ranges(&self, ranges: Vec>) -> Vec> { + ranges + .iter() + .flat_map(|range| self.lookup_range(range)) + .collect() + } + fn lookup_range(&self, range: &Range) -> Vec> { + let mut residual = vec![range.clone()]; + let mut resolved_mappings = Vec::new(); + + for map in &self.mappings { + let mut local_residual = Vec::new(); + for range in residual.into_iter() { + if let Some(overlap) = range_overlap(&range, &map.source_range) { + let dest_start_idx = overlap.start - map.source_range.start; + let length = overlap.end.saturating_sub(overlap.start); + resolved_mappings.push( + (map.dest_range.start + dest_start_idx) + ..(map.dest_range.start + dest_start_idx + length), + ); + local_residual.extend_from_slice(&range_exclude(&range, &overlap)); + } else { + local_residual.push(range); + } + } + residual = local_residual; + } + + // unresolved maps map 1:1 + resolved_mappings.extend_from_slice(&residual); + + resolved_mappings + } +} + +impl From<&mut Lines> for AlmanacMappingTable { + fn from(lines: &mut Lines) -> Self { + let name = lines + .next() + .unwrap() + .unwrap() + .split_once(':') + .unwrap() + .0 + .into(); + let mut mappings = Vec::new(); + for line in lines.map(|l| l.unwrap()) { + if line == "" { + break; + } + mappings.push(AlmanacMapping::from(line.as_str())); + } + AlmanacMappingTable { name, mappings } + } +} + +#[derive(Debug)] +struct Almanac { + seeds: Vec, + seed_to_soil: AlmanacMappingTable, + soil_to_fertilizer: AlmanacMappingTable, + fertilizer_to_water: AlmanacMappingTable, + water_to_light: AlmanacMappingTable, + light_to_temperature: AlmanacMappingTable, + temperature_to_humidity: AlmanacMappingTable, + humidity_to_location: AlmanacMappingTable, +} + +impl From> for Almanac { + fn from(mut lines: Lines) -> Self { + let seeds_s = lines.next().unwrap().unwrap(); + let seeds: Vec = seeds_s + .split_once(": ") + .unwrap() + .1 + .split_whitespace() + .map(|s| s.parse().unwrap()) + .collect(); + lines.next(); + + Almanac { + seeds, + seed_to_soil: AlmanacMappingTable::from(&mut lines), + soil_to_fertilizer: AlmanacMappingTable::from(&mut lines), + fertilizer_to_water: AlmanacMappingTable::from(&mut lines), + water_to_light: AlmanacMappingTable::from(&mut lines), + light_to_temperature: AlmanacMappingTable::from(&mut lines), + temperature_to_humidity: AlmanacMappingTable::from(&mut lines), + humidity_to_location: AlmanacMappingTable::from(&mut lines), + } + } +} + +impl Almanac { + fn lookup(&self, seed_id: i64) -> i64 { + self.humidity_to_location.lookup( + self.temperature_to_humidity.lookup( + self.light_to_temperature.lookup( + self.water_to_light.lookup( + self.fertilizer_to_water.lookup( + self.soil_to_fertilizer + .lookup(self.seed_to_soil.lookup(seed_id)), + ), + ), + ), + ), + ) + } + fn lookup_range(&self, range: Range) -> Vec> { + self.humidity_to_location.lookup_ranges( + self.temperature_to_humidity.lookup_ranges( + self.light_to_temperature.lookup_ranges( + self.water_to_light.lookup_ranges( + self.fertilizer_to_water.lookup_ranges( + self.soil_to_fertilizer + .lookup_ranges(self.seed_to_soil.lookup_ranges(vec![range])), + ), + ), + ), + ), + ) + } +} + +// PROBLEM 1 solution + +// The gardener and his team want to get started as soon as possible, so they'd like to +// know the closest location that needs a seed. Using these maps, find the lowest +// location number that corresponds to any of the initial seeds. To do this, you'll need +// to convert each seed number through other categories until you can find its +// corresponding location number. In this example, the corresponding types are: + +// Seed 79, soil 81, fertilizer 81, water 81, light 74, temperature 78, humidity 78, location 82. +// Seed 14, soil 14, fertilizer 53, water 49, light 42, temperature 42, humidity 43, location 43. +// Seed 55, soil 57, fertilizer 57, water 53, light 46, temperature 82, humidity 82, location 86. +// Seed 13, soil 13, fertilizer 52, water 41, light 34, temperature 34, humidity 35, location 35. + +// So, the lowest location number in this example is 35. + +// What is the lowest location number that corresponds to any of the initial seed numbers? + +fn problem1_lowest_location(almanac: &Almanac) -> i64 { + almanac + .seeds + .iter() + .map(|seed| almanac.lookup(*seed)) + .min() + .unwrap() +} + +fn problem1(input: Lines) -> i64 { + let almanac = Almanac::from(input); + problem1_lowest_location(&almanac) +} + +// PROBLEM 2 solution + +// Everyone will starve if you only plant such a small number of seeds. Re-reading the +// almanac, it looks like the seeds: line actually describes ranges of seed numbers. + +// The values on the initial seeds: line come in pairs. Within each pair, the first +// value is the start of the range and the second value is the length of the range. So, +// in the first line of the example above: + +// seeds: 79 14 55 13 + +// This line describes two ranges of seed numbers to be planted in the garden. The first +// range starts with seed number 79 and contains 14 values: 79, 80, ..., 91, 92. The +// second range starts with seed number 55 and contains 13 values: 55, 56, ..., 66, 67. + +// Now, rather than considering four seed numbers, you need to consider a total of 27 +// seed numbers. + +// In the above example, the lowest location number can be obtained from seed number 82, +// which corresponds to soil 84, fertilizer 84, water 84, light 77, temperature 45, +// humidity 46, and location 46. So, the lowest location number is 46. + +// Consider all of the initial seed numbers listed in the ranges on the first line of +// the almanac. What is the lowest location number that corresponds to any of the +// initial seed numbers? + +fn problem2_lowest_location(almanac: &Almanac) -> i64 { + let seed_ranges = almanac + .seeds + .iter() + .tuples() + .map(|(range_start, length)| *range_start..*range_start + *length); + + // seed_ranges + // .map(|range| { + // range + // .into_par_iter() + // .map(|x| almanac.lookup(x)) + // .min() + // .unwrap() + // }) + // .min() + // .unwrap() + let ranges = seed_ranges + .flat_map(|range| almanac.lookup_range(range)) + .collect_vec(); + + ranges.iter().map(|range| range.start).min().unwrap() +} + +fn problem2(input: Lines) -> i64 { + let almanac = Almanac::from(input); + problem2_lowest_location(&almanac) +} + +#[cfg(test)] +mod tests { + use num::Num; + use std::io::{BufRead, Cursor}; + use test_case::test_case; + + use crate::*; + + const EXAMPLE_ALMANAC: &str = &"seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4"; + + #[test] + fn test_almanac_mapping_parse() { + let s = "50 98 2"; + let a = AlmanacMapping::from(s); + assert_eq!(a.source_range, 98..100); + assert_eq!(a.dest_range, 50..52); + } + + #[test] + fn test_almanac_mapping_lookup() { + let s = "50 98 2"; + let a = AlmanacMapping::from(s); + assert_eq!(a.lookup(99), Some(51)); + } + + #[test] + fn test_almanac_parsing() { + let c = Cursor::new(EXAMPLE_ALMANAC); + let a = Almanac::from(c.lines()); + assert_eq!(a.lookup(79), 82); + } + + #[test_case(0..5, 2..4, Some(2..4) ; "one range overlaps completely")] + #[test_case(0..5, 2..10, Some(2..5) ; "one range overlaps from below")] + #[test_case(0..5, 5..10, None ; "no overlap, touching")] + #[test_case(0..5, 6..10, None ; "no overlap, disjoint")] + fn test_range_overlap( + r1: Range, + r2: Range, + expected: Option>, + ) { + assert_eq!(range_overlap(&r1, &r2), expected); + } + + #[test] + fn test_problem1_example() { + let c = Cursor::new(EXAMPLE_ALMANAC); + let a = Almanac::from(c.lines()); + assert_eq!(problem1_lowest_location(&a), 35); + } + + #[test] + fn test_problem2_example() { + let c = Cursor::new(EXAMPLE_ALMANAC); + let a = Almanac::from(c.lines()); + assert_eq!(problem2_lowest_location(&a), 46); + } +} diff --git a/2023/6/Cargo.lock b/2023/6/Cargo.lock new file mode 100644 index 0000000..a16c0bf --- /dev/null +++ b/2023/6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day6" +version = "0.1.0" diff --git a/2023/6/Cargo.toml b/2023/6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/2023/6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day6" +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/2023/6/input b/2023/6/input new file mode 100644 index 0000000..38a3c3e --- /dev/null +++ b/2023/6/input @@ -0,0 +1,2 @@ +Time: 53 89 76 98 +Distance: 313 1090 1214 1201 diff --git a/2023/6/src/main.rs b/2023/6/src/main.rs new file mode 100644 index 0000000..71a9e7a --- /dev/null +++ b/2023/6/src/main.rs @@ -0,0 +1,242 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +// --- Day 6: Wait For It --- + +// The ferry quickly brings you across Island Island. After asking around, you discover +// that there is indeed normally a large pile of sand somewhere near here, but you don't +// see anything besides lots of water and the small island where the ferry has docked. + +// As you try to figure out what to do next, you notice a poster on a wall near the +// ferry dock. "Boat races! Open to the public! Grand prize is an all-expenses-paid trip +// to Desert Island!" That must be where the sand comes from! Best of all, the boat +// races are starting in just a few minutes. + +// You manage to sign up as a competitor in the boat races just in time. The organizer +// explains that it's not really a traditional race - instead, you will get a fixed +// amount of time during which your boat has to travel as far as it can, and you win if +// your boat goes the farthest. + +// As part of signing up, you get a sheet of paper (your puzzle input) that lists the +// time allowed for each race and also the best distance ever recorded in that race. To +// guarantee you win the grand prize, you need to make sure you go farther in each race +// than the current record holder. + +// The organizer brings you over to the area where the boat races are held. The boats +// are much smaller than you expected - they're actually toy boats, each with a big +// button on top. Holding down the button charges the boat, and releasing the button +// allows the boat to move. Boats move faster if their button was held longer, but time +// spent holding the button counts against the total race time. You can only hold the +// button at the start of the race, and boats don't move until the button is released. + +// For example: + +// Time: 7 15 30 +// Distance: 9 40 200 + +// This document describes three races: + +// The first race lasts 7 milliseconds. The record distance in this race is 9 millimeters. +// The second race lasts 15 milliseconds. The record distance in this race is 40 millimeters. +// The third race lasts 30 milliseconds. The record distance in this race is 200 millimeters. + +// Your toy boat has a starting speed of zero millimeters per millisecond. For each +// whole millisecond you spend at the beginning of the race holding down the button, the +// boat's speed increases by one millimeter per millisecond. + +// So, because the first race lasts 7 milliseconds, you only have a few options: + +// Don't hold the button at all (that is, hold it for 0 milliseconds) at the start +// of the race. The boat won't move; it will have traveled 0 millimeters by the end +// of the race. + +// Hold the button for 1 millisecond at the start of the race. Then, the boat will +// travel at a speed of 1 millimeter per millisecond for 6 milliseconds, reaching a +// total distance traveled of 6 millimeters. + +// Hold the button for 2 milliseconds, giving the boat a speed of 2 millimeters per +// millisecond. It will then get 5 milliseconds to move, reaching a total distance +// of 10 millimeters. + +// Hold the button for 3 milliseconds. After its remaining 4 milliseconds of travel +// time, the boat will have gone 12 millimeters. + +// Hold the button for 4 milliseconds. After its remaining 3 milliseconds of travel +// time, the boat will have gone 12 millimeters. + +// Hold the button for 5 milliseconds, causing the boat to travel a total of 10 +// millimeters. + +// Hold the button for 6 milliseconds, causing the boat to travel a total of 6 +// millimeters. + +// Hold the button for 7 milliseconds. That's the entire duration of the race. You +// never let go of the button. The boat can't move until you let go of the button. +// Please make sure you let go of the button so the boat gets to move. 0 +// millimeters. + +// Since the current record for this race is 9 millimeters, there are actually 4 +// different ways you could win: you could hold the button for 2, 3, 4, or 5 +// milliseconds at the start of the race. + +// In the second race, you could hold the button for at least 4 milliseconds and at most +// 11 milliseconds and beat the record, a total of 8 different ways to win. + +// In the third race, you could hold the button for at least 11 milliseconds and no more +// than 19 milliseconds and still beat the record, a total of 9 ways you could win. + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSER + +#[derive(Debug)] + +// Appropriate for Problem 1 only +struct Races(Vec<(u64, u64)>); + +impl From> for Races { + fn from(mut input: Lines) -> Self { + let timeline = input.next().unwrap().unwrap(); + let distline = input.next().unwrap().unwrap(); + + let times = timeline + .split_whitespace() + .skip(1) + .map(|s| s.parse().unwrap()); + let distances = distline + .split_whitespace() + .skip(1) + .map(|s| s.parse().unwrap()); + + Races(times.zip(distances).collect()) + } +} + +// PROBLEM 1 solution + +// To see how much margin of error you have, determine the number of ways you can beat +// the record in each race; in this example, if you multiply these values together, you +// get 288 (4 * 8 * 9). + +// Determine the number of ways you could beat the record in each race. What do you get +// if you multiply these numbers together? + +/// Calculate the 'score' of a race. Speed will equal button duration, and runtime +/// equals the remaining time +fn race_distance(button_duration: u64, race_duration: u64) -> u64 { + assert!(button_duration <= race_duration); + + let running_time = race_duration - button_duration; + + running_time * button_duration +} + +fn problem1_possible_wins(race: (u64, u64)) -> u64 { + let (duration, record) = race; + + (1..duration) + .map(|button_duration| race_distance(button_duration, duration)) + .filter(|achieved_distance| *achieved_distance > record) + .count() as u64 +} + +fn problem1(input: Lines) -> u64 { + let races = Races::from(input); + races + .0 + .iter() + .map(|race| problem1_possible_wins(*race)) + .fold(1, |accum, elem| accum * elem) +} + +// PROBLEM 2 solution + +// As the race is about to start, you realize the piece of paper with race times and +// record distances you got earlier actually just has very bad kerning. There's really +// only one race - ignore the spaces between the numbers on each line. + +// So, the example from before: + +// Time: 7 15 30 +// Distance: 9 40 200 + +// ...now instead means this: + +// Time: 71530 +// Distance: 940200 + +// Now, you have to figure out how many ways there are to win this single race. In this +// example, the race lasts for 71530 milliseconds and the record distance you need to +// beat is 940200 millimeters. You could hold the button anywhere from 14 to 71516 +// milliseconds and beat the record, a total of 71503 ways! + +// How many ways can you beat the record in this one much longer race? + +fn problem2_parse_all_digits_to_number(s: &str) -> u64 { + s.chars().filter(|c| c.is_digit(10)).fold(0, |accum, elem| { + accum * 10 + elem.to_digit(10).unwrap() as u64 + }) +} + +fn problem2(mut input: Lines) -> u64 { + let time = problem2_parse_all_digits_to_number(input.next().unwrap().unwrap().as_str()); + let distance = problem2_parse_all_digits_to_number(input.next().unwrap().unwrap().as_str()); + + problem1_possible_wins((time, distance)) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"Time: 7 15 30 +Distance: 9 40 200"; + + #[test] + fn test_races_parser() { + let c = Cursor::new(EXAMPLE); + let races = Races::from(c.lines()); + assert_eq!(races.0, vec![(7, 9), (15, 40), (30, 200)]); + } + + #[test] + fn test_race_distance() { + assert_eq!(race_distance(0, 7), 0); + assert_eq!(race_distance(1, 7), 6); + assert_eq!(race_distance(2, 7), 10); + assert_eq!(race_distance(3, 7), 12); + assert_eq!(race_distance(7, 7), 0); + } + + #[test] + fn test_possible_wins() { + assert_eq!(problem1_possible_wins((7, 9)), 4); + assert_eq!(problem1_possible_wins((15, 40)), 8); + assert_eq!(problem1_possible_wins((30, 200)), 9); + } + + #[test] + fn test_problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 288); + } + + #[test] + fn test_problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 71503); + } +} diff --git a/2023/7/Cargo.lock b/2023/7/Cargo.lock new file mode 100644 index 0000000..7af9b93 --- /dev/null +++ b/2023/7/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/2023/7/Cargo.toml b/2023/7/Cargo.toml new file mode 100644 index 0000000..b170ccb --- /dev/null +++ b/2023/7/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/2023/7/input b/2023/7/input new file mode 100644 index 0000000..362968f --- /dev/null +++ b/2023/7/input @@ -0,0 +1,1000 @@ +K99QT 53 +TKQ7T 320 +22A7J 490 +267J9 69 +665JJ 431 +K856J 605 +977K9 552 +KKK8K 285 +53697 370 +3J337 879 +84A9K 901 +K4289 211 +4JA46 100 +K452K 315 +3K48Q 239 +99TT9 985 +A7JA9 582 +836AT 770 +J2KKK 61 +KK7KK 37 +QTAJK 351 +T4578 198 +TK55T 513 +44K9J 853 +KQT58 203 +66A6J 881 +K77KK 31 +6666Q 410 +8J8KJ 522 +69KQ7 594 +T27J3 110 +44999 461 +98885 231 +55655 372 +43343 958 +82247 300 +33336 957 +59JT4 303 +AQAJA 691 +J3626 696 +9379A 788 +JTTTA 795 +442A6 989 +73933 857 +979QQ 817 +65566 150 +6T565 182 +4T5J5 9 +TT3TT 147 +J8338 229 +6AQ8Q 636 +79KK7 933 +8T6TT 145 +TTT4T 177 +KAK52 952 +9K83J 469 +55855 718 +TTT5T 209 +9555Q 309 +AA3A5 327 +647T3 310 +22234 717 +5TT9J 5 +T5A86 831 +3J333 544 +66767 571 +9A777 269 +43QTT 822 +888J5 422 +T4435 259 +75394 17 +KJA2A 95 +A2KK4 762 +4T73T 595 +7Q97Q 3 +9J998 964 +KTK2K 267 +87Q87 375 +4744T 909 +3TTA7 637 +6J767 409 +3KT7K 588 +7T6T6 302 +39959 887 +TKTKT 144 +K99K2 777 +48237 403 +K93A3 886 +369AA 350 +43QK6 193 +657KA 199 +7398Q 314 +77778 38 +ATTAA 903 +A9A93 112 +T8TT4 128 +776K5 509 +868T9 641 +6T5J5 910 +TQ722 976 +9A9QQ 224 +J93J5 948 +JTTT7 576 +JKAQK 184 +AAAA5 764 +J3939 262 +7K7K8 204 +88928 663 +T8T8T 352 +5KJQQ 477 +22TTT 833 +299Q9 437 +67J42 393 +3A73A 961 +982K7 614 +J55Q5 549 +99699 741 +4T4T6 366 +55522 256 +39898 301 +9AA49 137 +9A6AA 706 +Q6668 740 +322J2 716 +K5454 439 +Q3Q77 954 +78J49 946 +66828 242 +652A9 340 +QQJQQ 342 +2Q2Q2 92 +2K352 871 +7Q5A9 361 +QAQQQ 925 +QQA26 625 +97J3Q 932 +KQ965 675 +8J88T 738 +JK84K 19 +AA333 130 +TTTKT 536 +7K8Q3 902 +A4A5A 529 +393K9 192 +85558 216 +TQQ77 4 +A5AQ9 276 +62672 90 +246T8 674 +Q424T 436 +474K4 970 +733J7 156 +73T23 713 +K864J 676 +K6K6K 75 +5524A 270 +AA7AA 89 +A6764 105 +JK555 103 +3Q322 124 +89K36 66 +7J724 686 +KJKJK 939 +Q7793 219 +J5A5J 296 +7AATK 666 +4442K 429 +4J734 504 +28TQ3 668 +9T623 862 +7Q7A5 810 +468A7 517 +44Q8Q 569 +6TT6T 36 +69965 824 +2QT39 864 +585T5 744 +84888 865 +QQJ6A 702 +66656 365 +K9T38 377 +56576 604 +95665 516 +96496 657 +5KK6K 274 +23857 982 +855J9 13 +T25QQ 264 +Q9K2K 354 +37K73 631 +88A57 487 +689KT 971 +898QQ 701 +2QA2J 482 +99J97 427 +77A76 167 +7TK93 596 +34647 579 +46J88 460 +QT4AT 380 +8T488 553 +32A44 726 +J3332 635 +K2J42 897 +93339 55 +6J7Q6 311 +Q9QQJ 7 +47444 539 +93399 567 +ATJAT 77 +TTQQQ 859 +9Q999 281 +K752A 412 +87977 205 +92334 995 +79QQQ 26 +JJTA8 581 +A4J24 444 +4J444 921 +Q9KK9 255 +45K2A 728 +KA5J6 621 +7J277 850 +56884 67 +44JQQ 240 +67Q7Q 426 +QT6T5 265 +AA9A2 252 +A9KA4 649 +TTQTT 554 +4A444 698 +95586 70 +7J45Q 996 +48848 500 +Q3AAA 474 +K3A7T 980 +5655J 977 +75J92 577 +TA94A 951 +336T3 400 +442QA 761 +K2J53 537 +JQQ22 470 +AJA53 88 +73797 672 +657TT 869 +2TTTT 142 +JT49K 227 +A7A77 338 +5J2Q8 221 +92269 664 +7A965 307 +3355J 308 +J9T98 518 +69TQ5 899 +57455 355 +AKA5A 489 +79K77 39 +72767 298 +AA858 695 +K3JTJ 283 +4KKJ4 258 +7K248 704 +Q9J47 617 +3QQT8 820 +87J3Q 540 +9A6A9 438 +3AT7A 344 +5656K 478 +66663 385 +K2KQ2 572 +37747 612 +58T72 690 +Q47A9 368 +8K53Q 687 +99799 856 +TJAQ2 476 +KA4A4 1000 +Q634A 408 +TKK88 785 +829JK 633 +26446 132 +JJJJJ 512 +T666T 51 +QQQQ3 751 +5A35A 534 +2222K 432 +5K72J 378 +866JK 566 +5T2AJ 11 +KK5JK 898 +97T2J 163 +7757J 247 +33237 640 +T554K 975 +K666K 940 +2T272 91 +7A988 960 +T9736 671 +999K9 68 +95595 802 +A5JJ3 652 +78829 867 +4JJJ4 148 +AA84J 541 +4T4T4 563 +A334J 931 +9T29T 416 +96845 992 +A22AK 555 +224KK 584 +84484 271 +22A88 986 +37AKJ 49 +JQ6QQ 284 +33853 280 +TTJ77 935 +66J66 720 +468A3 185 +QA958 642 +888J8 580 +JQQJQ 560 +J29J9 57 +6888J 14 +QJ8TK 440 +AQQAA 136 +AA9AA 823 +6ATTT 175 +7T42A 402 +A7J9K 907 +53A87 805 +26262 930 +22262 507 +27JK4 854 +QAQTT 731 +8TKTK 418 +TTT44 244 +J45TJ 98 +86898 681 +444AK 929 +KK88K 521 +8356T 644 +TKT6T 401 +Q99QQ 415 +96299 659 +8T888 598 +5T955 888 +85572 658 +T59K7 471 +7Q77Q 927 +AAA7K 949 +TA55A 278 +KJJK8 692 +AA5J4 638 +AKK4A 861 +77767 774 +K4KKK 143 +TKKQQ 10 +Q598T 794 +4A44J 201 +288QQ 602 +3TT2T 80 +Q5855 955 +AKKKA 43 +66QQQ 942 +97977 29 +J444J 157 +3AAAA 273 +3J988 420 +JAAAA 715 +4KQJ3 250 +KJ4K3 480 +QQ3Q3 113 +3883Q 421 +6T4TT 44 +77373 472 +4K74K 76 +AA226 943 +4Q4Q4 305 +7J729 763 +Q4649 456 +TKTJA 966 +6JJ24 131 +5K855 101 +AAJ3A 743 +82Q5A 558 +854Q8 186 +TTTT9 74 +AKAAA 323 +KK5K5 556 +3A69Q 164 +2AAAA 404 +Q4QQ4 162 +7T577 313 +5QQQQ 618 +87535 266 +4A4A4 356 +875J5 33 +9K225 479 +JQK95 941 +KQT28 826 +53356 760 +QQ5Q5 16 +696J9 498 +TJTT6 836 +7A7TA 979 +999AA 170 +55K5K 523 +733T3 746 +39929 503 +686AJ 885 +QQKQQ 667 +A8KK8 96 +Q555A 446 +Q2QQJ 797 +QQJQ8 445 +6T6J6 583 +65565 451 +535A6 765 +45QKT 453 +A26K6 816 +J4JJ8 60 +35535 510 +T85KT 613 +9Q3Q9 358 +53425 680 +4JA45 878 +76328 481 +QJ2AA 813 +QK4KQ 324 +K9KJJ 919 +22822 734 +JJJQ9 441 +33393 928 +29299 495 +69666 825 +T5Q82 423 +43687 834 +TTQ6A 682 +7KQJ6 736 +TK2JA 65 +665Q3 232 +A7297 593 +Q6Q33 837 +522J5 272 +44424 891 +57T63 41 +222J9 722 +99797 228 +33A33 711 +5TT7K 458 +KTTT4 22 +4774J 430 +9J889 257 +88483 225 +KKKKT 678 +7QQQQ 322 +37337 607 +QAQJA 997 +46696 316 +4A4AA 924 +QQ3J3 835 +72Q72 236 +4AAKA 254 +T989T 557 +K7Q76 115 +77JT6 591 +683A7 390 +A97Q7 295 +AA8AA 895 +4KK5K 660 +J5665 155 +JAJA5 920 +3T333 806 +5TJ48 287 +J4434 332 +98TQT 492 +J539K 781 +9QQ94 452 +TA593 745 +A4JA4 515 +2T332 609 +2KKKK 494 +4KKKA 30 +Q254J 590 +4T28A 608 +T9Q7K 275 +48843 279 +9629K 526 +778J7 329 +J67K5 697 +26A22 220 +67J97 849 +84Q88 863 +73777 248 +34KA2 286 +A9A9A 126 +Q2457 984 +55672 106 +77T89 165 +TQQ44 413 +KKJKK 497 +Q9482 417 +88999 550 +TTAA5 911 +TK2T2 790 +44445 578 +J55T5 169 +T738A 847 +55554 789 +AJAKQ 829 +4Q492 394 +QT5Q6 433 +Q5Q55 917 +KQAA5 801 +QQ242 870 +92A76 334 +7TT96 610 +J334K 520 +T333J 527 +J464K 149 +QQJ8T 349 +2KAKK 561 +K3JK9 40 +TTJJ8 213 +57557 151 +9ATAA 434 +JTTT3 990 +TTTT7 998 +6T3K3 384 +45738 293 +999TK 288 +999JJ 312 +333TT 15 +8A7TK 650 +35JT4 784 +29889 796 +TT669 114 +8585J 502 +75555 772 +5JT59 173 +AJAJA 99 +5842T 326 +8TA26 214 +99T29 776 +8TK44 896 +74J48 730 +78888 123 +3T763 139 +TT8T9 548 +8Q737 405 +2AK5J 684 +8885Q 804 +33Q36 183 +2K935 987 +3Q55K 346 +36933 543 +K575K 435 +94847 947 +7K666 107 +9TTTJ 367 +KK636 799 +J7TTA 766 +74K74 484 +Q5445 945 +2A662 48 +886AK 525 +A2555 395 +26J66 188 +22333 771 +J2KJ2 533 +32329 330 +4T4TQ 455 +836KJ 318 +29999 830 +KKAJT 905 +QQA9A 673 +33QTJ 179 +AQ748 719 +4Q3T8 46 +AJ79J 978 +44Q48 524 +87277 347 +2JQ4T 611 +Q6868 773 +A7355 950 +89Q68 263 +4947J 1 +QQA7Q 134 +TK32T 138 +45544 838 +J5QQQ 936 +7T656 488 +KQQ8Q 111 +A6T95 122 +24T4T 291 +64323 646 +4JQ4T 268 +Q9J82 851 +9JQJQ 710 +22T2T 606 +9J969 564 +36633 735 +2222J 972 +2K77J 292 +2T836 406 +9J379 721 +2T3T3 253 +55Q55 875 +KKT3T 197 +754JJ 643 +25577 261 +96776 81 +33Q33 756 +4455J 723 +274TJ 506 +43432 118 +J6JA9 634 +68Q2T 923 +JTTJT 47 +44777 215 +K5K5J 937 +52252 391 +T6365 883 +JT4JA 808 +25K9A 336 +6TTTT 876 +67767 703 +88485 160 +AAJ8A 814 +Q4K66 798 +853K6 873 +K943A 135 +74T8A 732 +J8T77 780 +Q32A6 246 +733AT 965 +68536 108 +K8K88 815 +75A42 234 +9Q24T 967 +J4AAK 514 +77KK7 397 +89889 93 +KJAKK 129 +8KA88 973 +488A4 62 +555J5 226 +3AA6J 153 +35239 679 +22694 574 +J3344 450 +9J999 545 +TKAT6 768 +57JQ9 969 +22882 189 +777A7 24 +33323 858 +J85A9 260 +TT2AT 993 +KK29T 200 +55595 821 +A5555 615 +82243 125 +82338 586 +TK385 457 +QQ89Q 243 +7T98T 202 +34K37 597 +8889Q 508 +KKTKQ 289 +TTTTJ 944 +QA8A9 622 +33KK3 277 +2J233 630 +K5666 819 +4J824 812 +7888K 45 +K42K7 846 +23A49 485 +98888 337 +2JJ22 468 +966A3 662 +J4JT3 86 +8KJ33 369 +KQ3A8 237 +AAQA6 904 +JKAKA 538 +K9T43 968 +44745 34 +8A4J8 78 +K523K 465 +JKAK6 294 +37J77 207 +33TTT 988 +A8TA8 463 +Q56Q8 194 +QQ443 319 +382Q9 532 +J66J6 568 +A92T4 388 +K5J49 172 +84288 428 +2QK99 752 +5QT27 767 +37Q54 592 +TAAAA 475 +AA9Q9 868 +97T3Q 58 +TAAJJ 120 +7TQ85 360 +45585 119 +JQ252 793 +39229 655 +66JAA 133 +67276 233 +66646 493 +A8A89 725 +K5K4J 620 +79KK9 818 +AA4AT 218 +86888 685 +3A252 353 +T4TAT 589 +TJ269 190 +3366T 449 +J87Q7 800 +77K77 656 +Q5856 628 +9TT33 874 +8J8TA 158 +33335 35 +4K5J5 587 +87877 104 +9A66A 168 +77277 843 +52545 570 +2QA3K 841 +72K75 727 +A8AA3 535 +25J5Q 729 +T3939 363 +4K66J 832 +99989 845 +36Q53 916 +TTKJK 462 +764AT 963 +88328 934 +9229K 708 +2J886 328 +97A38 448 +799J7 755 +5AQAT 749 +7KK76 146 +KAA2T 109 +2222T 882 +32257 117 +3KQ3K 6 +AQAAA 54 +599QQ 442 +44423 809 +TT99T 84 +523Q2 501 +33433 884 +2J894 918 +44484 411 +5J9QT 926 +J7872 63 +K2KK2 981 +655AA 217 +9949Q 42 +4ATJQ 991 +2294J 811 +K32JA 79 +5T84Q 839 +A8K24 530 +57T67 333 +JQ4TK 791 +Q58Q5 235 +74756 627 +62TT6 742 +34443 208 +7Q7QJ 102 +9Q6TT 792 +TA797 72 +67K56 645 +5K5QQ 629 +62666 669 +6T5KT 348 +66K32 531 +J2AAA 331 +4KJ7T 223 +T88K8 191 +55244 714 +A4J57 983 +Q777K 251 +QQKKQ 71 +A8858 196 +4J8JK 387 +88636 386 +32J56 230 +6Q833 23 +TT8T2 877 +A8JK4 483 +22A24 600 +35555 166 +4AKQJ 748 +94448 52 +92229 842 +292J3 912 +6J4AA 783 +A85JJ 890 +64556 210 +QTT9T 304 +7KJKK 180 +7777T 915 +59TA7 238 +TT8TT 206 +44434 953 +33373 906 +2J57A 828 +6QJJ7 889 +9KA9K 27 +T22KK 651 +KKJQK 852 +Q7373 775 +9298J 994 +TT739 654 +88787 491 +J68QJ 661 +K8QK4 893 +25T25 381 +2K895 152 +J777A 626 +62562 364 +Q64JQ 32 +98337 28 +4A7T4 299 +36J33 245 +83883 639 +59495 962 +66T6K 127 +JTJ2T 779 +KJ4Q6 424 +J822J 306 +3KQ45 362 +25QQ5 807 +Q8TJ7 389 +QJQ37 94 +T2QKK 747 +25555 632 +Q6Q69 64 +J2767 737 +333JJ 195 +9A9A5 689 +823QQ 707 +8T5T8 443 +23AK6 59 +86446 892 +93964 8 +QKKQ8 97 +59T86 866 +Q3QQ9 677 +55344 414 +57KJ5 860 +337A7 575 +9JJ9K 699 +3AA38 343 +37273 85 +22AAA 154 +A9896 601 +45J97 18 +T7689 599 +55537 573 +JTQT6 473 +262Q2 616 +8J5QQ 486 +25444 705 +2K2K2 922 +93QK2 20 +JT9AA 396 +83JTK 757 +J9AA4 827 +J3Q44 159 +922TT 505 +Q22A9 803 +3753K 700 +T5545 222 +777QA 840 +2T987 908 +JJJ34 547 +J9Q85 187 +34994 880 +K7AKJ 670 +9QQ4Q 647 +522JT 855 +K8K58 750 +55JJ5 653 +J3977 249 +KJKJT 419 +8866J 383 +56AQ4 87 +5444A 392 +J77J7 345 +495K9 848 +4J449 844 +96T66 999 +66628 758 +66684 171 +47767 454 +TJ54A 21 +58888 778 +J8J88 467 +96JKK 585 +8A5J5 624 +2A222 603 +44494 759 +J3A9K 956 +8854J 141 +5JK22 341 +T5Q25 359 +K3533 693 +44294 914 +JJJ7A 382 +9K654 241 +5A44A 900 +777JQ 12 +A6AA6 176 +T888T 399 +AJ77A 739 +J636J 499 +55KQK 551 +79JK9 447 +A888A 528 +45T49 466 +683T4 459 +KT7KJ 140 +88855 282 +85T7T 753 +542A8 496 +96554 769 +45A46 56 +642K5 511 +87AAJ 297 +3T28J 754 +A266J 709 +9QQK2 83 +T32Q2 425 +777J7 398 +9A5AJ 373 +3958K 683 +9AAJA 733 +92Q9Q 665 +777T3 786 +529J8 290 +592TA 371 +9Q99Q 787 +8444A 913 +AJKAT 872 +95K99 174 +KK4K3 339 +34A73 694 +25222 374 +Q7Q7Q 325 +32T54 321 +53K9Q 542 +A8TAJ 73 +4242J 648 +27772 161 +2K242 335 +3J6AQ 464 +3999K 407 +52JTT 619 +688J5 959 +2TT6Q 376 +K83AJ 379 +372KQ 357 +A6T96 25 +65AT2 565 +AAA6A 181 +J8JJJ 623 +3Q3Q3 559 +K38KK 50 +72227 317 +537QQ 546 +99KK9 212 +5J7J5 121 +3QT42 782 +9QQ32 116 +JTKK5 724 +J99TT 938 +AQQQA 688 +7729T 519 +8TKQ3 712 +53447 562 +63679 974 +TTTA8 2 +9K642 82 +44T44 894 +T9TQ4 178 diff --git a/2023/7/src/main.rs b/2023/7/src/main.rs new file mode 100644 index 0000000..7ab85c9 --- /dev/null +++ b/2023/7/src/main.rs @@ -0,0 +1,267 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSING + +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq)] +enum Card { + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + T, + J, + Q, + K, + A, +} + +impl Card { + const fn count() -> usize { + Card::A as usize + 1 + } +} + +#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)] +enum HandType { + HighCard, + OnePair, + TwoPair, + ThreeOfAKind, + FullHouse, + FourOfAKind, + FiveOfAKind, +} + +impl From for Card { + fn from(c: char) -> Self { + match c { + 'A' => Self::A, + 'K' => Self::K, + 'Q' => Self::Q, + 'J' => Self::J, + 'T' => Self::T, + '9' => Self::NINE, + '8' => Self::EIGHT, + '7' => Self::SEVEN, + '6' => Self::SIX, + '5' => Self::FIVE, + '4' => Self::FOUR, + '3' => Self::THREE, + '2' => Self::TWO, + _ => panic!("Invalid card {}", c), + } + } +} + +#[derive(Debug, PartialEq, Eq)] +struct Hand { + cards: Vec, + bid: u64, +} + +impl Hand { + fn card_counts(&self) -> [u64; Card::count()] { + let mut counts = [0u64; Card::count()]; + for card in &self.cards { + counts[*card as usize] += 1; + } + + counts + } + fn hand_type(&self) -> HandType { + if !WILD { + let card_counts = self.card_counts(); + if card_counts.contains(&5) { + HandType::FiveOfAKind + } else if card_counts.contains(&4) { + HandType::FourOfAKind + } else if card_counts.contains(&3) && card_counts.contains(&2) { + HandType::FullHouse + } else if card_counts.contains(&3) { + HandType::ThreeOfAKind + } else if card_counts.iter().filter(|count| **count == 2).count() == 2 { + HandType::TwoPair + } else if card_counts.contains(&2) { + HandType::OnePair + } else { + HandType::HighCard + } + } else { + let mut card_counts = self.card_counts(); + let wild_count = card_counts[Card::J as usize]; + card_counts[Card::J as usize] = 0; + + let same_count = card_counts.iter().max().unwrap() + wild_count; + + if same_count == 5 { + HandType::FiveOfAKind + } else if same_count == 4 { + HandType::FourOfAKind + } else if card_counts.contains(&3) && card_counts.contains(&2) { + HandType::FullHouse + } else + // Full House with WILDs + if wild_count == 1 + && (card_counts.contains(&3) + || (card_counts.iter().filter(|count| **count == 2).count() == 2)) + { + HandType::FullHouse + } else if wild_count == 2 && card_counts.contains(&2) { + HandType::FullHouse + } else if wild_count == 3 { + HandType::FullHouse + } else if same_count == 3 { + HandType::ThreeOfAKind + } else if card_counts.iter().filter(|count| **count == 2).count() == 2 + || (card_counts.contains(&2) && wild_count > 0) + { + HandType::TwoPair + } else if same_count == 2 { + HandType::OnePair + } else { + HandType::HighCard + } + } + } +} + +impl From<&str> for Hand { + fn from(s: &str) -> Self { + let (cards_s, bid_s) = s.split_once(' ').unwrap(); + let bid = bid_s.parse().unwrap(); + let cards = cards_s.chars().map(|c| Card::from(c)).collect(); + + Hand { cards, bid } + } +} + +impl PartialOrd for Hand { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Hand { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let our_type = self.hand_type(); + let other_type = other.hand_type(); + if our_type != other_type { + our_type.cmp(&other_type) + } else { + let (ours, theirs) = self + .cards + .iter() + .zip(other.cards.iter()) + .find(|(ours, theirs)| *ours != *theirs) + .unwrap(); + if !WILD { + ours.cmp(theirs) + } else { + if *ours == Card::J { + std::cmp::Ordering::Less + } else if *theirs == Card::J { + std::cmp::Ordering::Greater + } else { + ours.cmp(theirs) + } + } + } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut hands: Vec<_> = input + .map(|line| Hand::::from(line.unwrap().as_str())) + .collect(); + hands.sort(); + hands + .iter() + .enumerate() + .map(|(idx, hand)| (idx + 1) as u64 * hand.bid) + .sum() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let mut hands: Vec<_> = input + .map(|line| Hand::::from(line.unwrap().as_str())) + .collect(); +hands.sort(); +hands + .iter() + .enumerate() + .map(|(idx, hand)| (idx + 1) as u64 * hand.bid) + .sum() +} + +#[cfg(test)] +mod tests { + use std::io::Cursor; + + use crate::*; + + const EXAMPLE: &str = &"32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483"; + + #[test] + fn test_hand_parser() { + let hand = Hand::::from(EXAMPLE.lines().next().unwrap()); + assert_eq!( + hand, + Hand { + bid: 765, + cards: vec![Card::THREE, Card::TWO, Card::T, Card::THREE, Card::K] + } + ); + } + + #[test] + fn problem1_test_hand_compare() { + let hand1 = Hand::::from("T55J5 123"); + let hand2 = Hand::::from("QQQJA 123"); + let hand3 = Hand::::from("KK677 123"); + + assert!(hand2 > hand1); + assert!(hand3 < hand1); + assert!(hand3 < hand2); + } + + #[test] + fn problem2_test_hand_compare() {} + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 6440); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 5905); + } +} diff --git a/2023/8/Cargo.lock b/2023/8/Cargo.lock new file mode 100644 index 0000000..0a10780 --- /dev/null +++ b/2023/8/Cargo.lock @@ -0,0 +1,201 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "day8" +version = "0.1.0" +dependencies = [ + "lazy-regex", + "num", +] + +[[package]] +name = "lazy-regex" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/2023/8/Cargo.toml b/2023/8/Cargo.toml new file mode 100644 index 0000000..6f69aa2 --- /dev/null +++ b/2023/8/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day8" +version = "0.1.0" +edition = "2021" + +[dependencies] +lazy-regex = "3.1.0" +num = "0.4.1" diff --git a/2023/8/input b/2023/8/input new file mode 100644 index 0000000..c8d6906 --- /dev/null +++ b/2023/8/input @@ -0,0 +1,740 @@ +LLRLLRRLRLRRRLRRLLRRRLRLRLRRLRRRLRRLRLRLLRLLLRRRLRRLRRRLRRRLRRRLRLRRLLRRLRRLRRLRRRLRLRRRLLRLRRLRRRLRLRRRLRRRLRLRRRLLRRRLRRRLRLRRLRLRRRLLRRLRRLRRLRRLRLRLRRRLLRRRLRRLRRRLRLRLRRRLLRLRRLLRLRRLRLRRRLRLRRLLRRRLLRRLRLRLLRLLRRLRRLLRRLRLRRLRLRLRRRLRRLRLLLLRRLRLRLRRRLLLRRRLRRLRRLRLLRLRRRLLLRRRLRRRLRRRR + +NQT = (TXC, RVJ) +FPT = (PNS, KJL) +BNQ = (THG, LCV) +SPL = (VBH, NNV) +TLM = (LVQ, PGT) +GHC = (XKN, SPR) +PHT = (HMF, DST) +FSF = (JCM, SMT) +GDD = (FHJ, RBS) +GVR = (FVD, FVD) +SST = (PMF, MGC) +ZZZ = (VLV, SQV) +SDV = (DBL, GTL) +XSP = (GRV, RFM) +SKG = (NGH, VDX) +BPK = (RMK, LCQ) +VSC = (DRN, SFR) +BFR = (BJR, GMD) +HTM = (PFM, LVD) +XVP = (LDP, LDP) +DRD = (SCH, LKD) +DSJ = (GSQ, JTN) +VTS = (BNQ, VFX) +KVF = (HTP, MQK) +GPS = (FXQ, TPF) +VRC = (RTK, RTK) +HRD = (PMQ, JCR) +DJK = (FTC, KXH) +VGJ = (PCJ, TVH) +QMN = (TLC, HNG) +DMH = (QSD, PDP) +CRL = (VFR, MDR) +XGV = (CMV, FTV) +XJX = (HTM, HHG) +SQP = (XKK, KJF) +PXZ = (JCK, PTH) +BDV = (JMH, CND) +FBD = (CQS, QCN) +TRS = (FGM, NKF) +TLC = (HBC, FRC) +DKR = (LCP, VLD) +GTF = (QHD, PBB) +NCC = (HJF, LNR) +KGS = (KPF, GTB) +CPQ = (SLR, DRD) +RJQ = (KXK, BFR) +XSQ = (PNN, NMG) +DVP = (MBH, QLV) +CQL = (DVP, VHQ) +CPV = (MDL, VFB) +VHV = (CQS, QCN) +NRD = (KSC, HKX) +STX = (CGJ, PGK) +CNP = (FVB, MCN) +JXT = (DXT, LHK) +GHG = (MCL, MPB) +PGK = (RMS, SBC) +DNH = (FSV, RNS) +TFZ = (NQT, PBD) +MBQ = (DPH, FDK) +SJQ = (JLB, LJJ) +MBN = (FTD, CDB) +TDJ = (FJB, PQX) +DXT = (FJP, HGQ) +MSG = (MSM, CXN) +RTK = (GVR, GVR) +SDX = (VTS, MRG) +VCX = (QTL, NVV) +XCS = (KLR, THD) +RDD = (QDS, JST) +FVB = (BLD, NPF) +LVD = (JXC, FQR) +TMR = (SNJ, DML) +RCH = (JST, QDS) +RVJ = (XMN, SDT) +SQV = (MVP, MKC) +HDB = (LTL, JJP) +LVC = (CLL, CLL) +FGM = (TPG, PHK) +SCS = (MLK, TKF) +PRF = (TLB, KTT) +MPM = (NMN, KGS) +MGC = (NLT, MGS) +CNK = (BFR, KXK) +FXQ = (JMX, LLL) +GTB = (MFM, PMV) +VXS = (DPN, FPN) +NRB = (SCS, SGH) +QLJ = (NLL, VMM) +PNN = (SFJ, HSR) +BRJ = (RMQ, XLJ) +SNF = (FMH, MNR) +VLD = (SHN, FBG) +FSM = (GKF, DBC) +MHP = (LBX, KQH) +NSQ = (QKK, KND) +RRT = (RMK, LCQ) +BCX = (GTM, MVV) +XSX = (XFK, XNF) +TKF = (MFF, VCV) +HPD = (GHC, RKN) +MRG = (BNQ, VFX) +QTK = (QRT, RRB) +HFF = (NKD, SXP) +MFM = (BRB, DDN) +VVL = (GPC, BRJ) +CST = (GQT, NLG) +THD = (MVK, DRJ) +XFN = (JNC, MNF) +NKH = (PMD, BDV) +NLT = (HKK, KCD) +VXR = (XCS, GRX) +SDS = (JBV, JFM) +BFM = (XXG, CHQ) +PMD = (JMH, CND) +VVF = (GQT, NLG) +FGG = (NRD, SNP) +LKD = (NGK, SQP) +RSV = (TXL, GPD) +LNR = (RDB, NTD) +RPL = (SNB, MHK) +CCG = (JDT, PJD) +TKR = (TMD, VCL) +DRJ = (CQL, XFH) +JFM = (MMV, GPS) +FJP = (BDM, QXN) +QJS = (BFM, RHM) +KTT = (PXV, VJV) +XXB = (VKG, HXL) +RMK = (DBR, HFQ) +NGK = (KJF, XKK) +NPF = (SMK, BTJ) +RBS = (PGN, GNH) +PNS = (VTJ, KSM) +BCK = (SDX, NRF) +RNS = (XVS, KFX) +PDB = (BHH, BHH) +GMQ = (MRD, JRG) +DGM = (HDV, GSD) +TLB = (PXV, VJV) +QRT = (FRS, FFT) +VSX = (NLL, VMM) +GSQ = (JRJ, LGV) +TXC = (XMN, SDT) +HGQ = (QXN, BDM) +PMQ = (KBB, NRH) +KRC = (DGM, XCR) +QLZ = (TKM, HTF) +NJM = (GPC, BRJ) +FRG = (QLS, GXP) +NMG = (SFJ, HSR) +JDT = (TDF, QHT) +LFC = (RFM, GRV) +TXL = (XSX, FTP) +DKG = (JNL, XBR) +VKF = (GTM, MVV) +VQV = (RHT, SDS) +SJH = (LPV, TRQ) +KSM = (LHX, CPQ) +QMH = (MMD, GPB) +VNP = (TRQ, LPV) +NBM = (JPF, KRM) +RGQ = (TMH, KQR) +RSM = (SMP, KSG) +HDV = (VXS, VQG) +TTD = (CDB, FTD) +GXF = (SLN, SCF) +QHT = (TMR, QTH) +MNR = (VXR, GDS) +GSA = (THS, NKH) +XKN = (GNB, VLQ) +HHQ = (RRB, QRT) +CLC = (THS, NKH) +BSN = (QKN, QKN) +QRB = (KDT, JSL) +JCX = (VCX, DXP) +BDM = (SQN, TBH) +GQT = (DNB, NTS) +QSF = (KXH, FTC) +HFQ = (NNQ, LLM) +VFB = (NGF, NXB) +JMX = (HFB, DNR) +VKS = (NBM, NQF) +PGN = (FBD, VHV) +PNQ = (KQR, TMH) +QLM = (VVL, NJM) +PRB = (KQD, QRB) +GTL = (VQJ, DJR) +KNL = (JRN, KVF) +VMT = (LTL, JJP) +QKN = (PHM, VQS) +QBL = (LXK, PRF) +PCT = (VNP, SJH) +QLV = (HXF, DXH) +HNN = (DSJ, XDN) +LGV = (VVD, XXB) +XCB = (XXQ, CHX) +GXC = (FGG, MSX) +SLN = (VLP, SST) +NQQ = (SXP, NKD) +DRN = (DPC, TCV) +JHF = (QTK, HHQ) +GPM = (GTF, NCX) +PFM = (JXC, JXC) +KBT = (KQH, LBX) +XFH = (VHQ, DVP) +CPT = (VSD, TKR) +GSX = (CTF, XXX) +SPR = (GNB, VLQ) +XBP = (MXH, TBJ) +VKG = (PKS, CPT) +THG = (FRP, FRP) +SHN = (FRG, TCF) +BFS = (RPL, JRV) +PHK = (BQG, BMH) +SMK = (VRV, FDG) +PBF = (MSG, XGT) +NKF = (TPG, PHK) +VDQ = (MHP, KBT) +JRG = (DXJ, JCX) +MHK = (QTS, CRL) +CXN = (FBR, FCT) +DHT = (XXQ, CHX) +DML = (SPL, TBX) +BTJ = (FDG, VRV) +KVG = (TJJ, TRS) +JPH = (CLC, DXZ) +HXL = (PKS, CPT) +RDB = (PKM, FLG) +CTF = (NLX, SJQ) +BMH = (GVP, NSQ) +GTP = (NMG, PNN) +KJL = (KSM, VTJ) +SLR = (LKD, SCH) +TJJ = (NKF, FGM) +DPH = (HHT, NRB) +PBN = (CLC, CLC) +FFT = (DJK, QSF) +RTL = (TBJ, MXH) +PJD = (TDF, QHT) +HTP = (BGK, NCC) +MDL = (NXB, NGF) +CGJ = (SBC, RMS) +RTR = (LHK, DXT) +MLR = (JNC, MNF) +DXL = (RKN, GHC) +QTS = (VFR, MDR) +LLM = (KSK, BHN) +FFJ = (HGD, HGD) +PQX = (VRC, KXG) +XFK = (QNM, TMC) +FVD = (HTF, TKM) +CSR = (XDN, DSJ) +VPN = (DPH, FDK) +TJN = (HGC, HJH) +KSC = (SVX, SVX) +TLH = (FSV, RNS) +QTH = (DML, SNJ) +VQJ = (SMR, KCL) +GNP = (JRQ, GBR) +TKB = (TLC, HNG) +FBV = (BFM, RHM) +DMS = (BTG, NXV) +VDX = (BCM, RNG) +MSN = (QTK, HHQ) +TKM = (LSM, PBF) +FSS = (MCN, FVB) +DLA = (PHM, VQS) +LXK = (KTT, TLB) +JBV = (MMV, GPS) +FFQ = (QJS, FBV) +MXH = (RPP, LKF) +RKN = (SPR, XKN) +FDG = (RSC, QMH) +NLL = (VKS, FPF) +XDN = (JTN, GSQ) +FLS = (JNL, XBR) +MLA = (PTH, JCK) +GTM = (FSS, CNP) +NVV = (LLR, NLV) +TBX = (VBH, NNV) +HTG = (VDQ, GFH) +VLQ = (HXR, QNQ) +JSL = (MVC, DJM) +BPX = (VVL, NJM) +FBG = (FRG, TCF) +PFS = (HMH, KFF) +GPB = (DDH, RSM) +JFB = (FXX, GXC) +XJZ = (VQS, PHM) +FRS = (QSF, DJK) +NKP = (PDB, SGT) +KSB = (CTF, XXX) +DTL = (FMH, MNR) +HSN = (PBN, JPH) +JCQ = (XCB, DHT) +GBR = (NKP, GHV) +BKL = (CKM, XJX) +JLH = (XGV, MXR) +TBJ = (LKF, RPP) +RSJ = (HVN, KKH) +SVX = (BSN, BSN) +GRV = (CHT, NMV) +TKX = (RHT, SDS) +MQK = (BGK, NCC) +KLR = (DRJ, MVK) +NCH = (BCX, VKF) +NLX = (JLB, LJJ) +VHQ = (MBH, QLV) +QXR = (NCH, FRT) +FQS = (VGJ, HFG) +LCP = (SHN, FBG) +NCX = (QHD, PBB) +SXK = (HGD, ZZZ) +RNR = (RTR, JXT) +VQG = (FPN, DPN) +RMJ = (HNN, CSR) +KSL = (BCK, QQJ) +HCK = (QMN, TKB) +LBX = (HFD, XQH) +MTL = (NCH, FRT) +XHR = (KFL, TLM) +LLH = (LFC, XSP) +JRJ = (XXB, VVD) +KGM = (FQS, KGC) +SDT = (MNQ, GTN) +GJQ = (KLG, DMS) +RNG = (RNR, GHB) +DCK = (TXL, GPD) +DXJ = (DXP, VCX) +SNP = (KSC, HKX) +KRS = (BMQ, GBP) +GPC = (RMQ, XLJ) +CDB = (MXQ, XLM) +NLV = (FGS, KNL) +GNB = (HXR, QNQ) +KFL = (PGT, LVQ) +HQL = (QXR, MTL) +BJR = (PBH, PHT) +QTL = (LLR, NLV) +SNB = (CRL, QTS) +LDP = (PBN, PBN) +DFJ = (FHJ, RBS) +JJP = (XMJ, VVQ) +GQC = (QRB, KQD) +SFR = (DPC, TCV) +MPB = (FSM, GGL) +SXP = (QFP, GFQ) +MNF = (DKG, FLS) +KLG = (NXV, BTG) +TVH = (JFB, PJJ) +SHJ = (SFX, TMT) +RHF = (SRD, KBV) +XBR = (NQQ, HFF) +PBD = (RVJ, TXC) +XXQ = (KSL, DDL) +FTP = (XNF, XFK) +HBC = (JTF, LLH) +BTG = (HDB, VMT) +KFX = (DMH, GBM) +SGH = (MLK, TKF) +CVS = (GBP, BMQ) +DDL = (BCK, QQJ) +PKM = (QNV, DPJ) +QSR = (KJL, PNS) +KPD = (PCT, MCP) +BHH = (LVC, LVC) +MDR = (HTG, SFD) +HKK = (DXL, HPD) +SMD = (RCQ, NJD) +XMJ = (MKM, NTN) +CQS = (DNH, TLH) +PBH = (HMF, DST) +MKC = (CJH, JDQ) +KLL = (FJB, PQX) +CXP = (VGS, HFX) +JPF = (STX, BXV) +CRV = (RCQ, NJD) +FDK = (NRB, HHT) +BTT = (CRV, SMD) +RHT = (JBV, JFM) +MXR = (FTV, CMV) +DBL = (DJR, VQJ) +CHX = (DDL, KSL) +JKX = (VDX, NGH) +MQA = (HTF, TKM) +DJQ = (DLT, SBS) +VFR = (SFD, HTG) +NMN = (KPF, GTB) +JNK = (VNQ, TPT) +KJH = (GTF, NCX) +QLS = (GXF, SCK) +NHR = (PNQ, RGQ) +FPF = (NBM, NQF) +BKP = (KLL, TDJ) +SBC = (TTF, SSS) +XMN = (GTN, MNQ) +PBB = (RRT, BPK) +XVS = (GBM, DMH) +HFX = (MPN, JLH) +KDS = (TXG, DBT) +DDN = (GDD, DFJ) +SSS = (CST, VVF) +NGH = (RNG, BCM) +HNG = (HBC, FRC) +CND = (RCH, RDD) +HFD = (NHR, RDV) +FRC = (JTF, LLH) +TBH = (KDS, CNQ) +QNQ = (TFB, KGM) +SMT = (MBK, TJN) +NGF = (BPV, JNR) +XLM = (XVP, QCK) +JLB = (SNF, DTL) +NXV = (HDB, VMT) +VSD = (TMD, VCL) +TMT = (GHG, PVG) +NNV = (JKX, SKG) +HFB = (QQK, BKP) +BLD = (SMK, BTJ) +JGH = (TKB, QMN) +XXX = (NLX, SJQ) +LQH = (VLD, LCP) +KRM = (BXV, STX) +HVN = (FTH, QBL) +QXM = (KLG, DMS) +DBR = (LLM, NNQ) +MVP = (CJH, JDQ) +FBR = (MBQ, VPN) +SFP = (HRD, SGC) +HGD = (SQV, VLV) +CJH = (JNK, DXR) +DJM = (XSQ, GTP) +KXG = (RTK, RKV) +HTF = (LSM, PBF) +FXX = (MSX, FGG) +MFF = (VSC, QCP) +TMH = (HQL, GSJ) +KBB = (CHN, FSF) +XTV = (SBH, SGR) +MFH = (JGH, HCK) +SGC = (JCR, PMQ) +TRQ = (LVM, FFQ) +VCL = (DXK, RBD) +QSD = (VQF, JCQ) +FQR = (FFJ, SXK) +KQD = (KDT, JSL) +XLJ = (DCG, MPM) +FXL = (TFF, SSP) +RRN = (DBL, GTL) +CKM = (HTM, HHG) +VLP = (MGC, PMF) +KPQ = (MDL, VFB) +XQH = (RDV, NHR) +JRN = (MQK, HTP) +JMH = (RCH, RDD) +JRV = (SNB, MHK) +QCP = (DRN, SFR) +LLR = (KNL, FGS) +NMV = (DLJ, DJQ) +KBV = (GQC, PRB) +JXC = (FFJ, FFJ) +LFS = (MHB, VXT) +DST = (XHR, PCS) +SNJ = (TBX, SPL) +KND = (FDH, CCG) +CMT = (KPK, RMJ) +HHG = (PFM, LVD) +RPP = (MBN, TTD) +MBK = (HJH, HGC) +NQF = (JPF, KRM) +FPN = (BFS, RVB) +GFQ = (RTL, XBP) +SCH = (NGK, SQP) +VFX = (THG, LCV) +CTG = (TMT, SFX) +PGT = (CMT, RFB) +RLB = (TFF, SSP) +STD = (SJT, TFZ) +VMM = (FPF, VKS) +SFX = (GHG, PVG) +BXV = (CGJ, PGK) +FBT = (TBM, RHF) +LJJ = (SNF, DTL) +LKF = (MBN, TTD) +QNV = (KRC, VMF) +VGS = (MPN, JLH) +MKM = (JJD, BKL) +HKX = (SVX, RKF) +MSL = (CRV, SMD) +RHM = (CHQ, XXG) +FRP = (MSF, MSF) +VCV = (VSC, QCP) +FMH = (VXR, GDS) +FLR = (VGS, HFX) +GHV = (PDB, SGT) +FHQ = (MRD, JRG) +HJF = (RDB, NTD) +MSM = (FBR, FCT) +GHB = (JXT, RTR) +BMQ = (MFH, GTR) +MCP = (VNP, SJH) +SRD = (PRB, GQC) +DXR = (TPT, VNQ) +RFB = (KPK, RMJ) +GSJ = (MTL, QXR) +FHJ = (PGN, GNH) +LCQ = (DBR, HFQ) +GBM = (PDP, QSD) +TPT = (JHF, MSN) +MVV = (CNP, FSS) +KXD = (LQH, DKR) +PXV = (QXM, GJQ) +BHN = (KPQ, CPV) +SBS = (RRN, SDV) +LHX = (SLR, DRD) +TBM = (KBV, SRD) +DXK = (PLP, FGX) +NKD = (QFP, GFQ) +PVG = (MCL, MPB) +TTF = (VVF, CST) +PDP = (VQF, JCQ) +MSX = (NRD, SNP) +CMV = (RSV, DCK) +PMF = (NLT, MGS) +MSF = (SJT, SJT) +NTC = (DKR, LQH) +GNH = (VHV, FBD) +GSD = (VQG, VXS) +VBH = (JKX, SKG) +GTR = (JGH, HCK) +SSL = (MHB, VXT) +NLG = (NTS, DNB) +JRQ = (NKP, GHV) +MLK = (MFF, VCV) +BLF = (HRD, SGC) +BGK = (HJF, LNR) +VQF = (XCB, DHT) +GVP = (QKK, KND) +FTD = (MXQ, XLM) +JST = (JFC, KVG) +JFC = (TJJ, TRS) +MMD = (DDH, RSM) +SBH = (TKX, VQV) +PDH = (CVS, KRS) +NRH = (CHN, FSF) +MRD = (JCX, DXJ) +KGC = (VGJ, HFG) +RRB = (FFT, FRS) +KQR = (GSJ, HQL) +FSV = (XVS, KFX) +DBC = (PSR, QXX) +GMR = (KKH, HVN) +CLG = (CLL, PXZ) +JNL = (HFF, NQQ) +JCR = (KBB, NRH) +FTV = (DCK, RSV) +DPC = (FHQ, GMQ) +MVC = (XSQ, GTP) +KXK = (GMD, BJR) +DPJ = (KRC, VMF) +FTC = (GSX, KSB) +TBK = (QKN, XJZ) +VFG = (SHJ, CTG) +VXT = (QLJ, VSX) +MHB = (VSX, QLJ) +MMV = (TPF, FXQ) +QCN = (DNH, TLH) +KPK = (CSR, HNN) +RDV = (PNQ, RGQ) +DCG = (KGS, NMN) +FJB = (VRC, KXG) +NTD = (PKM, FLG) +DBT = (GPM, KJH) +LLL = (HFB, DNR) +SMR = (BPX, QLM) +MGS = (HKK, KCD) +HHT = (SCS, SGH) +MVK = (CQL, XFH) +GFH = (KBT, MHP) +JNC = (FLS, DKG) +QHD = (RRT, BPK) +NTN = (BKL, JJD) +VQS = (CNK, RJQ) +KQH = (XQH, HFD) +GGL = (DBC, GKF) +QQK = (KLL, TDJ) +LHK = (HGQ, FJP) +RKV = (GVR, PLT) +VKV = (TBM, RHF) +MPN = (XGV, MXR) +PHM = (RJQ, CNK) +TPG = (BQG, BMH) +CLL = (PTH, JCK) +MXQ = (XVP, QCK) +NXB = (BPV, JNR) +GTN = (PFS, HGV) +FRT = (VKF, BCX) +GRX = (THD, KLR) +VLV = (MVP, MKC) +NJD = (QBX, XTV) +VJV = (QXM, GJQ) +KPF = (MFM, PMV) +JCM = (TJN, MBK) +HFG = (TVH, PCJ) +HJH = (QNB, PDH) +JTN = (JRJ, LGV) +CHQ = (XFN, MLR) +JNR = (TXN, KPD) +BCM = (RNR, GHB) +PCS = (TLM, KFL) +DXP = (QTL, NVV) +KJF = (NTC, KXD) +DLT = (RRN, SDV) +QXN = (TBH, SQN) +TMC = (FBT, VKV) +PJJ = (GXC, FXX) +KXH = (KSB, GSX) +GDS = (XCS, GRX) +BQG = (GVP, NSQ) +XNF = (TMC, QNM) +RFM = (CHT, NMV) +JDQ = (JNK, DXR) +LKP = (MSF, STD) +TFB = (FQS, KGC) +DNR = (BKP, QQK) +FGX = (FPT, QSR) +HXR = (KGM, TFB) +XKK = (NTC, KXD) +FDH = (JDT, PJD) +FTH = (PRF, LXK) +TCF = (GXP, QLS) +TXN = (PCT, MCP) +TPF = (LLL, JMX) +LPV = (LVM, FFQ) +HXF = (FXL, RLB) +HGV = (KFF, HMH) +MCN = (BLD, NPF) +SMP = (BTT, MSL) +LVM = (FBV, QJS) +TCV = (GMQ, FHQ) +FLG = (DPJ, QNV) +PTH = (LFS, SSL) +SQS = (SHJ, CTG) +GBP = (MFH, GTR) +QCK = (LDP, HSN) +JCK = (LFS, SSL) +TNL = (LVC, CLG) +GKF = (QXX, PSR) +PCJ = (PJJ, JFB) +MBH = (DXH, HXF) +PLP = (FPT, QSR) +VXQ = (JRQ, GBR) +DDH = (KSG, SMP) +PKS = (TKR, VSD) +JRT = (CXP, FLR) +HMH = (SQS, VFG) +DNB = (JRT, BRM) +BRB = (DFJ, GDD) +PSR = (SFP, BLF) +KSK = (KPQ, CPV) +QXX = (BLF, SFP) +SSP = (GMR, RSJ) +VMF = (DGM, XCR) +MNQ = (PFS, HGV) +HSR = (VXQ, GNP) +KSG = (MSL, BTT) +VRV = (RSC, QMH) +XXG = (MLR, XFN) +RVB = (JRV, RPL) +KDT = (MVC, DJM) +SQN = (CNQ, KDS) +BPV = (TXN, KPD) +SJT = (PBD, NQT) +LCV = (FRP, LKP) +CNQ = (TXG, DBT) +GPD = (FTP, XSX) +CHN = (SMT, JCM) +XGT = (MSM, CXN) +TFF = (GMR, RSJ) +QKK = (FDH, CCG) +CHT = (DLJ, DJQ) +BRM = (CXP, FLR) +KFF = (VFG, SQS) +FGS = (KVF, JRN) +SFD = (GFH, VDQ) +RMQ = (MPM, DCG) +AAA = (SQV, VLV) +JJD = (XJX, CKM) +VVD = (HXL, VKG) +SCF = (VLP, SST) +RKF = (BSN, TBK) +LVQ = (CMT, RFB) +SCK = (SCF, SLN) +TXG = (GPM, KJH) +DXZ = (NKH, THS) +HMF = (PCS, XHR) +KKH = (QBL, FTH) +THS = (PMD, BDV) +FCT = (MBQ, VPN) +VVQ = (MKM, NTN) +QNM = (VKV, FBT) +LTL = (XMJ, VVQ) +TDF = (QTH, TMR) +VNQ = (JHF, MSN) +KCL = (QLM, BPX) +RMS = (TTF, SSS) +HGC = (QNB, PDH) +JTF = (LFC, XSP) +PMV = (DDN, BRB) +VTJ = (CPQ, LHX) +NNQ = (BHN, KSK) +NRF = (VTS, MRG) +QNB = (KRS, CVS) +RCQ = (QBX, XTV) +MCL = (FSM, GGL) +NTS = (JRT, BRM) +QQJ = (NRF, SDX) +QFP = (XBP, RTL) +DPN = (BFS, RVB) +XCR = (HDV, GSD) +SGT = (BHH, TNL) +JGA = (PBD, NQT) +LSM = (XGT, MSG) +PLT = (FVD, QLZ) +RSC = (MMD, GPB) +DXH = (RLB, FXL) +TMD = (DXK, RBD) +QDS = (JFC, KVG) +KCD = (DXL, HPD) +GMD = (PHT, PBH) +DLJ = (DLT, SBS) +DJR = (SMR, KCL) +SGR = (VQV, TKX) +RBD = (FGX, PLP) +SFJ = (GNP, VXQ) +GXP = (SCK, GXF) +QBX = (SBH, SGR) diff --git a/2023/8/src/main.rs b/2023/8/src/main.rs new file mode 100644 index 0000000..21c4a9c --- /dev/null +++ b/2023/8/src/main.rs @@ -0,0 +1,171 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +use lazy_regex::{lazy_regex, Lazy, Regex}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSE + +const LINE_REGEX: Lazy = lazy_regex!(r"([A-Z0-9]{3}) = \(([A-Z0-9]{3}), ([A-Z0-9]{3})\)"); + +enum Instruction { + LEFT, + RIGHT, +} + +impl From for Instruction { + fn from(c: char) -> Self { + match c { + 'L' => Self::LEFT, + 'R' => Self::RIGHT, + _ => panic!("Invalid instruction"), + } + } +} + +#[derive(Debug)] +struct Element { + left: String, + right: String, +} + +type Nodes = HashMap; + +#[derive(Debug)] +struct Map { + instructions: Vec, + nodes: Nodes, +} + +fn parse_node(nodes: &mut Nodes, node: &str) { + let matches = LINE_REGEX.captures(node).unwrap(); + + let node_id = matches.get(1).unwrap().as_str(); + let left = matches.get(2).unwrap().as_str(); + let right = matches.get(3).unwrap().as_str(); + + nodes.insert( + node_id.to_string(), + Element { + left: left.to_string(), + right: right.to_string(), + }, + ); +} + +impl From> for Map { + fn from(mut lines: Lines) -> Self { + let instructions = lines.next().unwrap().unwrap().chars().collect(); + let mut nodes = HashMap::new(); + lines.next(); + for line in lines { + parse_node(&mut nodes, line.unwrap().as_str()); + } + + Self { + instructions, + nodes, + } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let map = Map::from(input); + + let mut count = 0; + let mut cur_node = "AAA"; + for side in map.instructions.iter().cycle() { + count += 1; + cur_node = match side { + 'L' => &map.nodes[cur_node].left, + 'R' => &map.nodes[cur_node].right, + _ => panic!("invalid instruction"), + }; + if cur_node == "ZZZ" { + return count; + } + } + + 0 +} + +fn node_cycle_length(map: &Map, node: &str) -> u64 { + let mut count = 0; + let mut cur_node = node; + for side in map.instructions.iter().cycle() { + count += 1; + cur_node = match side { + 'L' => &map.nodes[cur_node].left, + 'R' => &map.nodes[cur_node].right, + _ => panic!("invalid instruction"), + }; + if cur_node.ends_with('Z') { + return count; + } + } + 0 +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + let map = Map::from(input); + + let starting_nodes: Vec<_> = map.nodes.keys().filter(|s| s.ends_with('A')).collect(); + + starting_nodes.iter().map(|n| node_cycle_length(&map, *n)).fold(1, |accum, elem| num::integer::lcm(accum, elem)) +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ)"; + + const EXAMPLE2: &str = &"LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX)"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 2); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE2); + assert_eq!(problem2(c.lines()), 6); + } +} diff --git a/2023/9/Cargo.lock b/2023/9/Cargo.lock new file mode 100644 index 0000000..23a49d5 --- /dev/null +++ b/2023/9/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day9" +version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] diff --git a/2023/9/Cargo.toml b/2023/9/Cargo.toml new file mode 100644 index 0000000..4b5adc8 --- /dev/null +++ b/2023/9/Cargo.toml @@ -0,0 +1,9 @@ +[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] +itertools = "0.12.0" diff --git a/2023/9/input b/2023/9/input new file mode 100644 index 0000000..5a57379 --- /dev/null +++ b/2023/9/input @@ -0,0 +1,200 @@ +27 49 92 176 327 586 1039 1879 3511 6711 12850 24194 44291 78456 134365 222769 358339 560653 855336 1275364 1862543 +15 25 36 57 117 286 706 1643 3584 7420 14791 28745 55012 104457 197703 373565 703877 1318603 2446886 4482001 8081143 +2 -1 -1 5 35 140 429 1101 2482 5067 9586 17171 29829 51664 91688 169675 327410 648943 1295161 2560233 4960367 +17 40 75 124 195 314 553 1083 2269 4852 10332 21799 45683 95258 197352 404834 819561 1634448 3210609 6218318 11892083 +12 28 47 77 148 320 690 1410 2740 5170 9648 17942 33138 60228 106667 182671 300884 474856 715541 1024739 1384064 +23 34 56 98 179 335 630 1192 2323 4773 10330 22981 51089 111404 236460 488303 984007 1942732 3771076 7214380 13617999 +-3 9 34 76 147 268 465 760 1157 1623 2064 2296 2011 738 -2201 -7740 -17123 -31963 -54306 -86700 -132269 +2 5 10 28 85 240 617 1451 3148 6359 12068 21694 37207 61258 97323 149861 224486 328153 469358 658352 907369 +2 8 19 26 30 54 164 525 1536 4108 10175 23584 51670 108250 219765 438340 869318 1727304 3445143 6876026 13651774 +-7 -3 9 42 129 327 726 1476 2850 5366 9996 18495 33888 61158 108183 186975 315279 518595 832691 1306680 2006739 +14 15 24 47 93 174 306 517 868 1493 2664 4887 9035 16524 29538 51309 86458 141403 224840 348303 526809 +-4 10 50 125 241 410 669 1105 1873 3186 5269 8334 12802 20376 37376 83418 211850 554706 1416484 3461131 8070127 +15 39 76 141 268 519 996 1856 3329 5739 9528 15283 23766 35947 53040 76542 108275 150431 205620 276921 367936 +24 38 52 61 53 10 -73 -138 42 1075 4356 12809 32195 73394 156469 318214 626801 1211053 2319569 4439602 8532786 +4 10 36 100 243 556 1233 2668 5629 11577 23262 45830 88824 169667 319485 592472 1080426 1934604 3397664 5849192 9869161 +-4 7 35 84 170 335 665 1316 2552 4799 8719 15308 26022 42935 68933 107948 165236 247703 364283 526372 748322 +-7 4 31 85 196 437 976 2172 4734 9973 20207 39455 74747 138831 256043 475047 895657 1722829 3370197 6649632 13104456 +16 32 58 111 217 410 745 1344 2499 4865 9805 20028 40853 82880 167863 341774 703534 1466538 3085872 6511449 13672565 +-5 -11 -19 -29 -41 -55 -71 -89 -109 -131 -155 -181 -209 -239 -271 -305 -341 -379 -419 -461 -505 +5 8 27 84 220 507 1078 2190 4332 8382 15798 28785 50293 83555 130708 190077 252623 301509 328232 401228 871348 +22 31 48 86 172 357 726 1417 2671 4960 9297 17952 36037 74887 159044 340266 724833 1525270 3155647 6402851 12728283 +13 26 37 45 58 113 311 867 2175 4888 10013 19021 33972 57655 93743 146963 223281 330102 476485 673373 933838 +12 37 78 151 286 522 901 1478 2383 3991 7275 14433 29888 61749 123771 237731 435898 762853 1275224 2036831 3105150 +8 21 55 123 237 408 646 960 1358 1847 2433 3121 3915 4818 5832 6958 8196 9545 11003 12567 14233 +3 19 44 71 100 149 259 491 916 1602 2605 3974 5783 8206 11654 16996 25889 41245 67866 113281 188822 +8 19 57 138 279 498 814 1247 1818 2549 3463 4584 5937 7548 9444 11653 14204 17127 20453 24214 28443 +9 4 5 23 80 223 554 1285 2829 5953 12058 23725 45786 87356 165507 311589 581617 1072658 1947779 3472867 6069516 +4 5 15 44 100 191 336 594 1120 2264 4761 10142 21660 46317 99077 211200 446093 928628 1898502 3807091 7495429 +15 29 52 91 168 327 635 1168 1976 3031 4181 5172 5894 7252 13693 39949 128019 387751 1089032 2861339 7106618 +4 27 76 163 300 499 772 1131 1588 2155 2844 3667 4636 5763 7060 8539 10212 12091 14188 16515 19084 +0 14 49 126 288 620 1290 2632 5308 10611 21004 41028 78750 147953 271285 484572 842448 1425348 2347731 3767130 5893244 +3 5 19 56 140 317 663 1291 2357 4065 6671 10486 15878 23273 33155 46065 62599 83405 109179 140660 178624 +15 24 50 100 174 275 432 735 1381 2730 5370 10190 18460 31917 52856 84225 129723 193900 282258 401352 558890 +3 10 28 67 151 337 751 1653 3540 7288 14321 26777 47618 80603 130010 199955 293113 408598 538706 664167 747489 +20 41 74 126 209 345 579 1013 1892 3804 8106 17770 38987 84130 177153 363333 726644 1420251 2718970 5107486 9425181 +15 27 39 51 63 75 87 99 111 123 135 147 159 171 183 195 207 219 231 243 255 +8 21 58 139 303 630 1288 2630 5374 10904 21738 42245 79828 147184 267217 484267 889432 1675307 3248534 6450147 12967977 +12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 +0 4 25 68 141 272 548 1191 2693 6050 13171 27607 55878 109928 211696 401582 753886 1404336 2597893 4769494 8676715 +4 -3 -3 13 61 170 394 850 1809 3869 8243 17220 34937 68787 132155 249818 468390 875793 1636077 3049219 5650065 +-7 -12 -10 18 99 265 551 994 1633 2510 3672 5174 7083 9483 12481 16214 20857 26632 33818 42762 53891 +10 11 22 61 152 325 616 1067 1726 2647 3890 5521 7612 10241 13492 17455 22226 27907 34606 42437 51520 +-3 8 30 79 184 392 785 1526 2963 5845 11749 23884 48524 97419 191628 367293 683905 1235574 2165672 3684933 6092624 +2 3 19 75 213 511 1120 2320 4588 8669 15665 27239 46225 78349 136635 251895 496486 1036135 2239379 4901527 10688757 +1 16 38 75 146 280 530 1025 2091 4478 9733 20768 42711 84231 159727 293088 524193 920024 1593528 2736001 4674548 +3 25 64 128 238 443 857 1732 3581 7365 14758 28504 52880 94279 161927 268748 432391 676433 1031772 1538224 2246338 +15 31 66 135 254 453 816 1572 3270 7080 15271 31926 63963 122540 224931 396969 676161 1115589 1788720 2795257 4268172 +27 52 87 132 187 252 327 412 507 612 727 852 987 1132 1287 1452 1627 1812 2007 2212 2427 +6 4 2 0 -2 -4 -6 -8 -10 -12 -14 -16 -18 -20 -22 -24 -26 -28 -30 -32 -34 +4 11 20 44 117 313 790 1867 4137 8611 16871 31171 54336 89114 136235 189681 226470 186864 -59204 -757963 -2305449 +4 11 27 57 125 292 679 1491 3030 5674 9787 15514 22407 28825 31055 22114 -9784 -83075 -224933 -473457 -879727 +13 17 21 35 75 166 351 719 1485 3189 7136 16294 37055 82679 180193 384661 809354 1689777 3517915 7317730 15196654 +10 17 24 31 38 45 52 59 66 73 80 87 94 101 108 115 122 129 136 143 150 +27 49 91 159 267 461 851 1657 3290 6505 12672 24205 45163 81989 144294 245595 404241 646129 1014881 1605299 2658415 +19 34 60 116 232 449 819 1405 2281 3532 5254 7554 10550 14371 19157 25059 32239 40870 51136 63232 77364 +18 24 42 87 174 318 534 837 1242 1764 2418 3219 4182 5322 6654 8193 9954 11952 14202 16719 19518 +6 29 59 90 126 189 331 657 1366 2817 5627 10808 19950 35457 60843 101095 163110 256213 392763 588854 865118 +24 38 63 112 208 396 772 1544 3141 6387 12773 24924 47533 89430 168250 320635 623454 1235720 2477499 4972172 9893262 +20 31 47 90 193 406 812 1566 2988 5773 11448 23343 48599 102174 214505 445531 909284 1815331 3536133 6713024 12418165 +2 8 15 29 71 183 441 988 2108 4377 8967 18275 37245 76104 155813 318424 645830 1292206 2537887 4875649 9144499 +12 27 60 123 244 489 1000 2064 4242 8598 17076 33085 62395 114591 205730 363815 640895 1140372 2078175 3917166 7655393 +18 41 85 154 264 458 825 1540 2955 5784 11438 22579 43975 83751 155144 278883 486328 823515 1356267 2176544 3410218 +25 47 94 176 295 447 637 917 1470 2781 5959 13315 29395 62880 130203 262551 519331 1013463 1957376 3743766 7082563 +12 36 78 153 285 506 857 1391 2178 3312 4920 7173 10299 14598 20459 28379 38984 53052 71538 95601 126633 +-9 -10 -9 -9 -1 49 226 731 2030 5162 12340 28097 61446 129887 266659 533459 1041993 1989246 3713317 6779115 12106201 +20 29 35 31 19 21 90 321 862 1925 3797 6851 11557 18493 28356 41973 60312 84493 115799 155687 205799 +-6 -3 11 42 98 202 434 1017 2457 5737 12550 25536 48463 86262 144791 230163 347428 498349 677957 869510 1037416 +4 7 5 -2 -13 -16 32 239 835 2267 5358 11580 23540 45868 86847 161361 296086 538343 970707 1734362 3065353 +23 34 45 56 67 78 89 100 111 122 133 144 155 166 177 188 199 210 221 232 243 +15 24 53 118 232 398 608 856 1169 1659 2616 4730 9709 21946 52622 128909 313015 739022 1680219 3666426 7680242 +17 38 88 196 409 802 1506 2779 5162 9791 18983 37288 73326 142974 274971 521020 974399 1804584 3321362 6091664 11147623 +8 8 10 17 39 104 275 677 1544 3319 6903 14301 30250 66118 148768 339775 775365 1746373 3853149 8297308 17418410 +-3 2 15 47 120 283 651 1479 3282 7013 14320 27926 52219 94208 165102 282908 476627 792860 1305925 2132937 3455722 +16 26 41 60 86 138 269 592 1329 2920 6269 13283 28019 59072 124461 261459 545998 1130142 2312689 4668734 9281108 +-6 6 46 126 262 491 897 1643 3003 5392 9425 16132 27661 49173 93242 189001 399616 853530 1797416 3682042 7296424 +-4 -1 13 47 127 326 824 2022 4746 10587 22427 45208 87060 161160 289492 511743 907304 1648299 3121165 6190937 12747973 +17 31 61 134 285 567 1071 1956 3494 6140 10642 18211 30776 51354 84570 137367 219951 347021 539339 825700 1245367 +4 3 11 37 90 179 313 501 752 1075 1479 1973 2566 3267 4085 5029 6108 7331 8707 10245 11954 +19 46 84 140 224 358 596 1065 2064 4295 9340 20529 44357 92619 185525 356467 660393 1191126 2121031 3794357 6943216 +8 16 37 100 246 537 1085 2114 4078 7876 15236 29393 56278 106613 199710 370762 685812 1274056 2398767 4611270 9075822 +2 -4 -19 -38 -51 -33 87 495 1640 4543 11410 26772 59475 125979 255657 499296 943299 1735447 3136428 5630823 10173850 +13 27 38 49 83 210 581 1472 3345 6937 13400 24555 43443 75674 132840 240992 460991 935765 1996936 4399387 9824965 +3 12 35 67 93 81 -25 -310 -889 -1902 -3501 -5827 -8975 -12945 -17577 -22468 -26869 -29560 -28701 -21657 -4795 +15 27 46 66 87 125 222 456 951 1887 3510 6142 10191 16161 24662 36420 52287 73251 100446 135162 178855 +14 23 39 75 169 398 911 2007 4290 8934 18087 35434 66925 121655 212859 358956 584542 921193 1407895 2090869 3022505 +15 36 81 166 312 559 986 1730 2993 5021 8033 12071 16734 20750 21330 13237 -12509 -70382 -182663 -382436 -717404 +19 32 45 58 71 84 97 110 123 136 149 162 175 188 201 214 227 240 253 266 279 +-2 5 22 60 151 373 887 1980 4103 7888 14123 23659 37218 55066 76510 99173 117996 123911 102124 29942 -125927 +9 12 22 34 39 22 -29 -77 87 1096 4442 13136 32617 71938 145339 274569 492877 852644 1440422 2405998 4016403 +-9 -6 6 35 100 239 526 1098 2199 4271 8176 15743 31038 63112 131591 277503 583471 1210275 2461445 4894923 9512306 +12 23 55 123 242 427 693 1055 1528 2127 2867 3763 4830 6083 7537 9207 11108 13255 15663 18347 21322 +11 14 21 26 25 31 109 449 1515 4352 11208 26742 60269 129816 269400 542212 1064843 2052166 3900239 7337374 13693713 +10 31 73 150 276 476 823 1514 2996 6142 12452 24210 44460 76567 122998 182788 246942 290761 261763 61494 -480916 +-4 -8 -10 -5 30 161 548 1551 3942 9304 20736 44029 89532 174989 329698 600421 1059560 1816208 3030786 4934087 7851666 +-4 3 20 47 84 131 188 255 332 419 516 623 740 867 1004 1151 1308 1475 1652 1839 2036 +8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 +21 30 36 46 86 222 598 1499 3452 7392 14956 29058 55103 103623 195940 375980 734087 1453470 2898156 5775242 11427276 +-6 -9 -4 16 58 129 236 386 586 843 1164 1556 2026 2581 3228 3974 4826 5791 6876 8088 9434 +8 13 20 29 40 53 68 85 104 125 148 173 200 229 260 293 328 365 404 445 488 +19 26 32 48 96 211 458 978 2082 4425 9321 19308 39132 77369 148902 278349 504201 882744 1489634 2415052 3745420 +10 16 24 40 81 183 409 857 1668 3034 5206 8502 13315 20121 29487 42079 58670 80148 107524 141940 184677 +21 26 30 39 59 99 190 425 1023 2422 5411 11315 22256 41568 74658 131212 231061 418918 798584 1606547 3360157 +9 8 8 22 86 266 662 1403 2625 4431 6867 10055 14884 25203 53477 133622 347577 876546 2094306 4729204 10134266 +-1 -2 8 32 73 143 280 580 1265 2834 6384 14244 31139 66195 136212 270772 519915 965310 1736072 3030632 5146357 +-2 -2 13 54 132 258 443 698 1034 1462 1993 2638 3408 4314 5367 6578 7958 9518 11269 13222 15388 +-2 6 37 114 269 538 965 1638 2793 5036 9744 19705 40030 79298 150754 273141 470376 768738 1189479 1733748 2355379 +16 23 24 15 6 48 281 1011 2839 6897 15303 32048 64706 127665 248103 476869 908233 1716234 3223529 6036316 11315929 +19 32 68 142 265 447 721 1214 2309 4976 11411 26234 58716 126943 265692 541495 1081721 2131187 4163142 8097743 15731633 +1 -3 -6 -2 22 89 251 650 1660 4159 9982 22608 48171 97033 186547 346470 630043 1136417 2054362 3743666 6880068 +24 47 92 170 291 461 690 1024 1630 2995 6347 14470 33172 73795 157408 321954 635304 1221423 2316722 4394762 8439429 +23 48 86 149 276 546 1093 2138 4061 7537 13765 24853 44523 79524 142575 258436 475994 891197 1688199 3208496 6058065 +3 13 49 128 272 504 852 1373 2208 3676 6407 11496 20625 36039 60164 94507 137265 178775 193541 127056 -125026 +4 -2 -11 -25 -36 3 218 875 2497 6123 13899 30391 65368 139404 294608 614273 1257478 2518044 4922297 9387695 17475856 +5 19 49 109 219 403 687 1103 1706 2616 4111 6822 12116 22794 44271 86434 166379 312193 567853 999139 1700177 +21 50 107 220 438 835 1512 2597 4243 6624 9929 14354 20092 27321 36190 46803 59201 73342 89079 106136 124082 +27 51 101 202 395 740 1313 2194 3454 5171 7538 11170 17772 31396 60592 121846 244797 479835 908803 1659658 2926089 +17 37 67 109 166 250 412 817 1900 4669 11281 26120 57765 122464 250040 493560 944611 1756661 3179751 5610679 9663912 +13 15 16 16 15 13 10 6 1 -5 -12 -20 -29 -39 -50 -62 -75 -89 -104 -120 -137 +12 13 11 -2 -39 -104 -149 24 963 3946 11669 29464 67455 144426 294890 582251 1123729 2138238 4039298 7616181 14386452 +7 20 51 104 183 292 435 616 839 1108 1427 1800 2231 2724 3283 3912 4615 5396 6259 7208 8247 +5 5 8 19 51 134 329 746 1571 3135 6123 12145 25114 54288 120647 269980 599712 1314292 2840161 6063874 12820008 +12 15 13 7 0 -3 5 33 92 195 357 595 928 1377 1965 2717 3660 4823 6237 7935 9952 +1 8 16 31 81 244 689 1737 3973 8477 17295 34347 67092 129523 247677 470351 891245 1696557 3268454 6407922 12805039 +9 33 76 143 237 360 525 789 1317 2487 5046 10327 20537 39126 71247 124317 208689 338445 532320 814767 1217173 +11 31 64 110 169 241 326 424 535 659 796 946 1109 1285 1474 1676 1891 2119 2360 2614 2881 +26 42 70 127 236 435 800 1487 2809 5398 10572 21146 43107 88829 182848 371662 739580 1433330 2698962 4935561 8771430 +2 11 40 93 179 314 517 803 1177 1632 2152 2725 3398 4496 7371 16631 46060 132925 369880 971301 2402941 +5 15 28 44 63 85 110 138 169 203 240 280 323 369 418 470 525 583 644 708 775 +12 24 48 106 232 475 909 1662 2974 5293 9422 16753 29700 52649 94250 173043 330936 664202 1391576 2997148 6517328 +-1 17 59 136 265 479 854 1563 2960 5693 10849 20148 36241 63285 108309 184739 321329 583473 1120691 2265768 4730029 +2 -3 -5 14 91 282 664 1350 2548 4733 9060 18229 38123 80680 168633 342961 674142 1278589 2341985 4151616 7140235 +11 32 67 124 226 434 878 1803 3653 7238 14067 27011 51650 99105 192160 378598 759877 1551137 3202487 6635077 13683300 +22 49 84 133 214 355 588 943 1457 2233 3620 6653 14027 32153 75410 174913 396773 880766 1923511 4158780 8947225 +15 33 73 148 287 553 1078 2128 4219 8323 16251 31416 60433 116531 226750 446724 890047 1784573 3576629 7117548 13987187 +8 16 40 102 243 527 1056 2021 3824 7316 14206 27706 53487 101031 185474 330045 569216 952688 1550348 2458342 3806419 +14 32 56 87 145 286 626 1379 2934 6028 12118 24115 47717 93666 181356 344335 638374 1152920 2026908 3470079 5791137 +16 30 47 82 166 351 715 1367 2452 4156 6711 10400 15562 22597 31971 44221 59960 79882 104767 135486 173006 +15 21 33 65 152 369 870 1969 4302 9132 18888 38064 74645 142274 263427 473922 829153 1412511 2346531 3807387 6043446 +2 11 34 82 177 374 809 1793 3980 8650 18179 36848 72337 138680 262338 494747 937838 1794641 3466872 6738086 13108833 +8 32 85 189 374 673 1107 1667 2323 3124 4498 7917 17159 40477 94074 207383 430762 846336 1582851 2835549 4892228 +23 44 84 148 239 358 504 674 863 1064 1268 1464 1639 1778 1864 1878 1799 1604 1268 764 63 +8 11 30 73 153 292 535 989 1912 3905 8328 18210 40229 88947 195693 426912 921729 1968565 4161259 8717037 18122990 +19 20 16 7 -7 -26 -50 -79 -113 -152 -196 -245 -299 -358 -422 -491 -565 -644 -728 -817 -911 +-1 5 18 47 118 279 616 1295 2650 5354 10743 21411 42250 82159 156668 291686 528445 929423 1584524 2615995 4179378 +1 4 5 4 1 -4 -11 -20 -31 -44 -59 -76 -95 -116 -139 -164 -191 -220 -251 -284 -319 +10 30 63 115 204 363 637 1086 1835 3268 6569 15003 36649 89801 213009 482818 1043768 2157242 4278403 8173867 15097050 +25 41 67 117 222 433 820 1480 2579 4477 8040 15359 31320 66885 145720 317282 682310 1442050 2992534 6103186 12250215 +12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 +15 22 40 80 159 300 532 890 1415 2154 3160 4492 6215 8400 11124 14470 18527 23390 29160 35944 43855 +8 7 11 41 134 350 790 1640 3278 6516 13097 26628 54204 109065 214728 411149 763596 1375053 2403127 4082595 6754906 +19 29 36 40 53 119 342 922 2199 4705 9224 16860 29113 47963 75962 116334 173083 251109 356332 495824 677949 +0 0 -5 -5 35 192 613 1574 3615 7833 16464 33946 68728 136175 263016 493891 900674 1595382 2747625 4607709 7536673 +-2 0 7 14 16 6 -27 -83 -90 254 1829 6796 19932 51036 119067 258967 532494 1044852 1969467 3583930 6320920 +26 35 52 99 221 492 1020 1960 3549 6182 10553 17890 30318 51389 86823 145509 240820 392301 627794 986069 1520035 +5 21 46 78 125 234 550 1426 3608 8527 18748 38671 75709 142525 261777 476692 874459 1636085 3137694 6146600 12183961 +-2 -6 -6 18 104 324 825 1919 4252 9083 18697 36969 70134 128019 226586 393988 684961 1212859 2215513 4180473 8066083 +9 3 0 17 89 288 750 1702 3476 6492 11187 17862 26414 35915 43995 45981 33739 -5839 -92766 -256377 -538305 +8 14 18 21 28 50 115 309 881 2459 6437 15606 35115 73861 146420 275644 496062 858236 1434236 2324411 3665646 +-7 -11 -6 15 60 157 387 931 2131 4565 9136 17175 30558 51837 84385 132555 201853 299125 432758 612895 851664 +10 19 24 25 22 15 4 -11 -30 -53 -80 -111 -146 -185 -228 -275 -326 -381 -440 -503 -570 +13 19 31 52 101 226 526 1205 2703 5983 13105 28309 60021 124633 253872 509555 1013313 2005639 3960067 7796758 15268381 +1 8 29 69 132 232 426 877 1954 4375 9398 19064 36495 66249 114733 190674 305647 474658 716779 1055831 1521110 +16 24 26 23 18 16 24 51 108 208 366 599 926 1368 1948 2691 3624 4776 6178 7863 9866 +2 9 16 23 30 37 44 51 58 65 72 79 86 93 100 107 114 121 128 135 142 +-6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 +16 35 60 94 145 232 391 684 1223 2231 4173 8002 15578 30332 58262 109364 199618 353667 608346 1017238 1656455 +-2 3 31 105 258 541 1053 2022 3981 8098 16734 34318 68643 132702 247198 443877 769848 1293069 2109193 3349983 5193520 +-8 -13 -18 -23 -28 -33 -38 -43 -48 -53 -58 -63 -68 -73 -78 -83 -88 -93 -98 -103 -108 +10 30 63 126 257 536 1124 2334 4755 9470 18464 35449 67620 129460 250952 495036 993952 2023078 4146993 8500420 17313419 +-9 -12 -14 -15 -15 -14 -12 -9 -5 0 6 13 21 30 40 51 63 76 90 105 121 +-5 -8 -10 -4 22 91 247 575 1252 2676 5755 12487 27048 57780 120827 246847 493437 967924 1868360 3555370 6674502 +1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 103 109 115 121 +10 12 19 46 131 355 869 1923 3896 7344 13120 22688 38891 67754 122697 234467 474563 1009589 2223578 4982855 11186148 +3 -1 7 43 125 273 509 857 1343 1995 2843 3919 5257 6893 8865 11213 13979 17207 20943 25235 30133 +13 24 36 47 70 147 361 848 1829 3712 7353 14609 29360 59215 118142 230266 435053 794032 1399090 2382195 3926146 +9 6 11 46 151 398 912 1911 3803 7420 14528 28829 57767 115566 228065 440074 826157 1505954 2665385 4585336 7679711 +16 39 76 127 192 287 472 902 1936 4380 10002 22541 49545 105547 217409 433311 838156 1580625 2921540 5320719 9591714 +0 3 21 71 173 355 678 1297 2575 5268 10799 21634 41766 77341 137657 237491 403773 694697 1250635 2419574 5045700 +6 13 17 26 67 205 584 1500 3516 7629 15499 29750 54353 95101 160186 260888 412386 634701 953781 1402738 2023247 +13 25 45 80 128 176 209 245 427 1232 3904 11298 29466 70580 158268 337300 691065 1372831 2660967 5054944 9439092 +12 27 56 109 217 450 952 2016 4237 8811 18102 36684 73186 143428 275536 517963 951618 1707611 2992452 5122887 8572903 +11 9 18 50 123 268 550 1128 2407 5389 12427 28744 65328 144230 308068 637127 1280826 2517520 4871558 9346510 17891302 +21 38 67 119 209 358 602 1007 1685 2817 4726 8115 14700 28634 59344 126700 269817 561276 1129161 2190075 4097255 +20 42 87 173 334 639 1220 2307 4268 7652 13233 22053 35462 55153 83190 122027 174516 243902 333803 448173 591246 +-8 -17 -31 -55 -87 -106 -57 174 807 2254 5266 11168 22214 42099 76670 134883 230058 381489 616471 972811 1501895 +0 7 34 95 211 424 819 1555 2919 5449 10239 19663 38964 79482 164782 341635 698754 1397451 2720024 5143775 9451177 +19 26 34 59 144 369 871 1896 3913 7829 15369 29767 57136 109398 210718 411416 816946 1645551 3337551 6752638 13509007 +17 28 54 109 207 362 588 899 1309 1832 2482 3273 4219 5334 6632 8127 9833 11764 13934 16357 19047 +17 24 43 87 168 300 509 860 1528 2974 6339 14232 32144 70737 149186 299524 571463 1036320 1787319 2931489 4565420 +0 8 33 87 192 392 779 1532 2965 5579 10114 17600 29411 47333 73666 111391 164446 238170 339991 480453 674698 +-10 -13 -4 36 137 338 676 1159 1720 2174 2272 2106 3430 13019 50131 161667 451022 1130269 2610728 5654808 11623123 +12 27 59 121 226 387 617 929 1336 1851 2487 3257 4174 5251 6501 7937 9572 11419 13491 15801 18362 +-6 5 43 135 328 690 1307 2277 3702 5679 8291 11599 15636 20404 25875 31997 38706 45945 53691 61991 71008 +-4 -10 -24 -54 -111 -209 -365 -599 -934 -1396 -2014 -2820 -3849 -5139 -6731 -8669 -11000 -13774 -17044 -20866 -25299 +14 13 10 15 48 152 421 1050 2418 5219 10660 20749 38700 69486 120575 202888 332022 529785 826094 1261291 1888936 +13 32 68 142 287 548 982 1658 2657 4072 6008 8582 11923 16172 21482 28018 35957 45488 56812 70142 85703 +-5 0 15 47 113 262 609 1386 3024 6296 12579 24342 46049 85805 158353 290736 533880 988511 1866498 3637488 7371468 +4 4 8 16 28 44 64 88 116 148 184 224 268 316 368 424 484 548 616 688 764 +3 5 9 15 23 33 45 59 75 93 113 135 159 185 213 243 275 309 345 383 423 +8 23 45 76 121 182 252 312 334 293 191 96 199 892 2870 7260 15780 30931 56225 96452 157989 diff --git a/2023/9/src/main.rs b/2023/9/src/main.rs new file mode 100644 index 0000000..3b64d5e --- /dev/null +++ b/2023/9/src/main.rs @@ -0,0 +1,115 @@ +use itertools::Itertools; +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + println!("Problem 1 solution: {}", problem1(get_input())); + println!("Problem 2 solution: {}", problem2(get_input())); +} + +// PARSE + +#[derive(Debug)] +struct History(Vec>); + +impl From<&str> for History { + fn from(s: &str) -> Self { + let hist: Vec = s + .split_whitespace() + .map(|num| num.parse().unwrap()) + .collect(); + Self(vec![hist]) + } +} + +impl History { + fn build_row(&mut self) { + let last = self.0.last().unwrap(); + self.0 + .push(last.iter().tuple_windows().map(|(a, b)| b - a).collect()) + } + + fn build(&mut self) { + while !self.0.last().unwrap().iter().all(|x| *x == 0) { + self.build_row(); + } + } + + fn extrapolate(&mut self) { + self.build(); + self.0.last_mut().unwrap().push(0); + for (lower, upper) in (0..self.0.len()).rev().tuple_windows() { + let new_value = self.0[upper].last().unwrap() + self.0[lower].last().unwrap(); + self.0[upper].push(new_value); + } + } + + fn extrapolate2(&mut self) { + self.build(); + self.0.last_mut().unwrap().insert(0, 0); + for (lower, upper) in (0..self.0.len()).rev().tuple_windows() { + let new_value = self.0[upper].first().unwrap() - self.0[lower].first().unwrap(); + self.0[upper].insert(0, new_value); + } + } +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> i64 { + let mut histories: Vec = input.map(|s| History::from(s.unwrap().as_str())).collect(); + + for history in &mut histories { + history.extrapolate(); + } + + histories + .iter() + .map(|history| history.0.first().unwrap().last().unwrap()) + .sum() +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> i64 { + let mut histories: Vec = input.map(|s| History::from(s.unwrap().as_str())).collect(); + + for history in &mut histories { + history.extrapolate2(); + } + + histories + .iter() + .map(|history| history.0.first().unwrap().first().unwrap()) + .sum() +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &"0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45"; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 114); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 2); + } +} diff --git a/2023/boilerplate/src/main.rs b/2023/boilerplate/src/main.rs new file mode 100644 index 0000000..e6bd15f --- /dev/null +++ b/2023/boilerplate/src/main.rs @@ -0,0 +1,55 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Lines}; +use std::time::Instant; + +// BOILERPLATE +type InputIter = Lines>; + +fn get_input() -> InputIter { + let f = File::open("input").unwrap(); + let br = BufReader::new(f); + br.lines() +} + +fn main() { + let start = Instant::now(); + let ans1 = problem1(get_input()); + let duration = start.elapsed(); + println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + + let start = Instant::now(); + let ans2 = problem2(get_input()); + let duration = start.elapsed(); + println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); +} + +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + 0 +} + +// PROBLEM 2 solution +fn problem2(input: Lines) -> u64 { + 0 +} + +#[cfg(test)] +mod tests { + use crate::*; + use std::io::Cursor; + + const EXAMPLE: &str = &""; + + #[test] + fn problem1_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem1(c.lines()), 0); + } + + #[test] + fn problem2_example() { + let c = Cursor::new(EXAMPLE); + assert_eq!(problem2(c.lines()), 0); + } +}