From 82f19b26673628f5d2f911d0b1a9d2e7e3dd3d98 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Mon, 8 Dec 2025 18:01:15 +0100 Subject: [PATCH] 2025: day05 --- aoc2025/benches/aoc2025_bench.rs | 2 + aoc2025/input/day05.txt | 1168 ++++++++++++++++++++++++++++++ aoc2025/input/day05_provided.txt | 11 + aoc2025/src/day05.rs | 127 ++++ aoc2025/src/lib.rs | 1 + aoc2025/src/main.rs | 3 +- 6 files changed, 1311 insertions(+), 1 deletion(-) create mode 100644 aoc2025/input/day05.txt create mode 100644 aoc2025/input/day05_provided.txt create mode 100644 aoc2025/src/day05.rs diff --git a/aoc2025/benches/aoc2025_bench.rs b/aoc2025/benches/aoc2025_bench.rs index ee66bd3..e64ba8b 100644 --- a/aoc2025/benches/aoc2025_bench.rs +++ b/aoc2025/benches/aoc2025_bench.rs @@ -4,12 +4,14 @@ use aoc2025::day01; use aoc2025::day02; use aoc2025::day03; use aoc2025::day04; +use aoc2025::day05; fn aoc2025_all(c: &mut Criterion) { c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day02", |b| b.iter(|| day02::run().unwrap())); c.bench_function("day03", |b| b.iter(|| day03::run().unwrap())); c.bench_function("day04", |b| b.iter(|| day04::run().unwrap())); + c.bench_function("day05", |b| b.iter(|| day05::run().unwrap())); } criterion_group! { diff --git a/aoc2025/input/day05.txt b/aoc2025/input/day05.txt new file mode 100644 index 0000000..0a62223 --- /dev/null +++ b/aoc2025/input/day05.txt @@ -0,0 +1,1168 @@ +335974207908975-335974207908975 +166378971253254-166378971253254 +202256907733789-202256907733789 +21286600651630-24648609504166 +558745805536874-559599518272636 +435394114820892-441719509061826 +174441875641519-175271524124959 +92531249332742-97299413301312 +513484599234147-520481731528370 +173874995893993-174120643897356 +174821166255466-175271524124959 +293613115143021-293613115143021 +425676293461608-427469272423527 +261699778868308-267782959886821 +62792025293181-64427754150033 +213359143863329-216920173874119 +485300942212668-489793905834071 +13378432871871-18518223043577 +175718286653119-176590936466342 +193998237963829-197306069406792 +224922375146937-226805648022207 +485300942212668-487043192651483 +560583161419561-561040752839097 +335974207908976-340360894525194 +173492439912652-173874995893993 +456128568685693-461311211763581 +554853132666292-555060503953561 +345187503361263-351437898258577 +415331359030508-416758070551384 +175718286653119-176131896992802 +559865818114046-559990685160468 +504484787497948-511084091741178 +41812357709935-46323709619503 +400944889652503-401137154895499 +38566586092786-38566586092786 +71063392748694-78768155867248 +88401531130325-90283598868170 +366212386842113-369559574733375 +41812357709935-49638287193527 +80820430981784-82386862701922 +392205177095718-392504737949797 +248578080710452-250780185041297 +473027494433883-479114462366037 +555860029504106-556466347855858 +414519044783763-415742529794645 +233578509446508-236721976075246 +122852185571020-126919155995876 +318807362064738-318807362064738 +559990685160468-560583161419561 +241484697563377-243682180887337 +197306069406793-199659117082727 +132511850415150-138777911998374 +402736436544865-411103997061827 +226805648022208-226805648022208 +393924466477451-394165734508863 +418277983121492-419855611026273 +176388216880188-176590936466342 +115216308429265-115634137713851 +397880740944623-398712454597673 +356534427403335-360529778470432 +244741606128524-246874615680412 +274184272176093-276891099082351 +553786879235470-554225646956219 +441719509061827-441719509061827 +497223143248878-499936465753045 +543649640475870-550684888609470 +375914800960974-380372536476131 +400723725598098-401137154895499 +429547893930521-431760667174505 +400053049630881-400514724465965 +554225646956219-554853132666292 +135239239818335-138777911998374 +182027374246065-189422637659825 +144155552966923-144155552966923 +87392235254912-89213336560437 +283530251930645-285296872972707 +213359143863329-216920173874119 +380372536476131-380372536476131 +156220005513438-158084001614371 +854928928779-854928928779 +82779951437902-84787684996565 +533710125364597-539267469576012 +264489803972606-271074611857554 +854928928779-8566391540121 +119445951907973-120022385782033 +13378432871871-18518223043577 +354155862714865-360529778470432 +455264846030324-456128568685692 +494219560192751-499936465753045 +406873622656898-408148431482032 +468585580330055-471967905222337 +383865841363033-390795142574897 +427014313885862-428814784979861 +559599518272636-559990685160468 +559599518272636-560410632727482 +504484787497947-504484787497947 +64427754150034-67890971713727 +233578509446508-239818719089833 +416411266176555-417812862711511 +24648609504168-29263080242135 +314869508360391-318807362064737 +560583161419561-561040752839097 +111419310349724-112233031390255 +110883288953568-111665610587406 +399209502052219-400053049630881 +561040752839097-561316255371618 +112233031390255-112666262934332 +324828185977870-328086858004231 +324828185977870-328086858004231 +107587576693307-107587576693307 +524080596593938-532342489388312 +417390509854428-419093317118083 +394413024861195-394757137029331 +112666262934332-113317864269805 +446662754943806-450103995140459 +85221235761715-87036012386502 +31473941440477-38566586092786 +412514374136009-413804208813214 +343730583502464-348034601898665 +92531249332741-92531249332741 +383865841363033-386606350017845 +111419310349724-111665610587406 +559246284857000-559990685160468 +165182752983584-166378971253254 +543649640475870-543649640475870 +246083189515790-247908798756856 +420256891645835-421932264678660 +173380777142810-174120643897356 +144155552966924-147110359740417 +101753444912368-107587576693306 +119091563626635-119445951907973 +285296872972708-289812470209470 +400288079402009-400723725598098 +113008816697274-113662773409190 +424135858939915-425998486358274 +174975048817048-175271524124959 +428496401741058-430734630493823 +464500784968198-468585580330053 +303866335136566-307890118348895 +202256907733789-210125636959977 +124455006477459-129196124218540 +394413024861195-395118980044980 +413587981094322-414864801923854 +419335851296203-420942147944880 +86107799660651-87988102400429 +526807835244889-528743093879030 +446662754943806-450103995140459 +253843593355425-258292954265193 +84158911770863-85542009864131 +422677329720344-424462640002629 +152176426627367-158084001614371 +113878761125354-114156526358922 +247249187230399-249243372682639 +276891099082353-280656369807064 +539267469576012-539267469576012 +395905589789358-396469906815526 +172496676347961-173001774144952 +476653069393498-481331058855155 +81957654709794-83295826585653 +242969727811069-244959373538347 +74394496875907-76781648657372 +363577973505683-366212386842111 +293613115143021-298938356609908 +118225058422162-118558339178687 +395118980044980-395905589789358 +52883578495993-56916924366480 +183815199442521-188204070048092 + +95421034754432 +187179043703356 +518586140516029 +286087724210976 +171547521407314 +118796582870366 +80700451913997 +521304944811388 +497468960146556 +120606342021435 +260394049436757 +200596862923178 +188589752480046 +197064627901683 +360568655614345 +331520423406200 +237943558918272 +282775400579729 +480634058235661 +387123238330877 +407311889566058 +276576032963344 +108810466370754 +212436708456393 +378152926139933 +9539258390456 +281625824028267 +539618529896649 +256138651620246 +359014620151679 +253880903370040 +184624512447647 +82311226354056 +516734033336448 +357403910169806 +303119157923575 +543161445154172 +264618797247246 +466707387250480 +88011917280175 +95476869924856 +383715343175304 +473642625792138 +178333505393210 +318686646529518 +62714303498196 +38712704045944 +240698891130918 +435248429254210 +227025814289600 +33915893072946 +62526691936577 +248790560032824 +453661476529089 +462227344128986 +555662229460224 +473624561372123 +276377583721064 +558508284289223 +203299005563602 +396993851834484 +137896622349447 +346865373468445 +373506787536335 +288467334194362 +347997654929570 +146607305666440 +447601507813676 +51495128381982 +231637467929814 +299606936878056 +557626860612469 +354054394144975 +150627318630636 +278880679271512 +477587880734738 +193400650974752 +393435084719475 +55283577724959 +211152190911931 +3925818696675 +135681614102409 +362212511935836 +15245439499461 +469268230455889 +280306907226699 +13108165441170 +450961761646095 +493135537724534 +543513801509751 +412257224038 +394071927077474 +521079984516881 +492359630059532 +366412387640018 +177699407790013 +414487427672158 +529846119496680 +137301446715144 +512053011848440 +432979864636899 +161350530359498 +112854252365641 +533458423746447 +318632973652408 +314944771805149 +174334343800494 +455121110191088 +345947433453361 +323714520499482 +179443839376817 +548082988887097 +18267001378267 +283608421420521 +520321056536851 +234263961645426 +8190929262671 +538828046340449 +33254683016337 +425572185983473 +488557445980242 +11951846999189 +521283998853305 +561659516040865 +329313844231447 +368466415820673 +540149017603243 +280695472377801 +152121629837146 +114305019133387 +406714162273808 +232201283017301 +140506397608555 +9248763541481 +124445354405428 +137657422393595 +280192423076119 +146692574387499 +211306199762647 +319809016642867 +84012182209918 +169899064816585 +364799307008810 +483304088820862 +385494390424332 +327981529459437 +31394677952634 +245372471891013 +438327981906171 +74927526680816 +261818081429269 +144334098124209 +31758233428411 +52710106266718 +238166478887568 +169562321358767 +330654407646253 +493962129811822 +504551302357996 +453228700142237 +60661783473372 +304618837615069 +314419727746404 +507901124142683 +366928218804813 +71077994649221 +143312746817111 +208864183320714 +158669774376852 +38415894633069 +426285938708603 +173732342198265 +381596583122544 +139333902849444 +562930746320361 +299153278548982 +258702673209260 +461207679800758 +515482702223393 +77893020634835 +332006390167407 +392880606704720 +227442635091447 +391106972192016 +157112754399357 +250958689609784 +557099760724962 +324128042383584 +448495819564965 +68622143164569 +132156875443396 +429978653437826 +542601391784931 +164667455575880 +385308998578707 +303568535743369 +397956049210214 +509095691945333 +167697561181675 +315445489488298 +349657346264055 +380672309472065 +316812211996017 +458179549034732 +233848958077938 +200255966676269 +255123710170889 +408264438219290 +499465274034481 +59184775106056 +115754353359652 +45436745326892 +529343025925307 +60987202120266 +355171194124646 +59334813063361 +218271963649499 +81798752961535 +382613802205632 +81164643446055 +192263130358918 +14950842718816 +561625591981174 +95560028634521 +451339980674243 +273556597459920 +75832351369826 +267462302692896 +498818218165393 +370132175272712 +480380307767814 +354994148632894 +320403156490950 +455081389885802 +455069242867452 +141663717234570 +50398520399781 +550026750664252 +456070589691856 +441784144333129 +506677145361245 +441655718359854 +290827731264154 +460018451485172 +548902559600377 +398495694436003 +4803494865017 +482772676466232 +551605955871700 +175425566524033 +276396995779074 +19885198042189 +520871233429048 +319047176585574 +548704188271087 +159773928469700 +261969546702883 +458920737300731 +551481293707 +223222988760505 +57120960533451 +253888547194942 +390943257567901 +369759578840532 +322499433638314 +381466669710836 +47508010730741 +98385139440756 +61220160510811 +22082641517702 +75725980493680 +202197220498977 +495388824624335 +556029079944015 +343105576135909 +226547101746838 +221291764485430 +122160416023622 +10407162470410 +300553098166486 +124918384122246 +439622208725188 +70161758138109 +130132511023479 +211815660014820 +173731351552218 +84879638458890 +347442089561375 +381193659045772 +376365628500085 +109371947052760 +260704799640162 +505291657065252 +7979499160298 +141939784233605 +396359642039323 +381223416637207 +403742798792521 +41875944940582 +286832825547004 +418521937962215 +468364877455071 +397405547685448 +302986613091056 +53570221215055 +137947070089911 +494320748922078 +278495605713418 +356583765894996 +166096544430923 +462961652181124 +178728098109514 +27465208031789 +457594112275071 +306343677134663 +80913003939829 +165744304257405 +484195292627758 +41127402258904 +457233114196134 +542937220417300 +77002920969070 +401450824164946 +121166679631716 +272769445220787 +451015965504175 +421966175139574 +12458477947374 +382119036417204 +451053055860974 +309791497558758 +423426748752492 +12122329348700 +185447302912028 +66052691280574 +465635908582021 +113545299950352 +532311736607203 +270085319988406 +104442614627868 +301656913331806 +249141122093886 +415591144012947 +334103228980608 +336535426596669 +411997402766609 +114456852432120 +131331849015344 +186480502336660 +154403871216046 +304183379436527 +352925086311243 +185872055326627 +15839530743895 +181510405374135 +374796393472737 +103765041706181 +148244694839096 +181373724702688 +201729373985416 +113554788206222 +225516678903365 +69244219558408 +356560178686019 +124989349975293 +195066814362826 +334490858380620 +82162929539730 +223419421588339 +398128563330487 +458196454433780 +150089126753276 +69405508222389 +159514596618637 +521648241238531 +255172259034208 +171389847853039 +182837229449722 +288389808294500 +553680446954477 +322822311673700 +330336577305119 +286309285024163 +40516696906700 +247527149720990 +429589296140858 +357016505064933 +170200616025067 +499813712174605 +517107049325407 +69063148554856 +48476243865555 +443305960626541 +429832460423833 +49739807098888 +227669926768160 +423193948126066 +65192185454036 +41470571839975 +522386153384224 +232829721412542 +150150633348933 +101191218688765 +75293107589941 +537984532351798 +103907120888053 +557922541520927 +489537649785016 +446442275945984 +345847605313075 +306212088830421 +208558642425658 +311860396978414 +392987506091906 +22955118182998 +527255806516331 +314274955991567 +296208433749607 +252809667953039 +483408440522500 +172623897731241 +53242341360513 +201500807021580 +390163998578841 +176815282150252 +447532216209997 +487690188770716 +262931147359511 +170988511893621 +496229042271059 +316060892128770 +214613795356459 +225605271366387 +94549602951308 +275959508605998 +8284604692648 +64053360408335 +539432357242415 +435222374800551 +414672972555117 +128233252869997 +102272351347327 +116522193401692 +201857337030305 +347590540484565 +123112797328761 +221115412681745 +149631199034761 +344107701572476 +531412855412105 +542257392327843 +216830528547531 +257656367851810 +193547003521438 +331634211891451 +403987187284510 +533644872886638 +160098661575060 +60403330318185 +536064917422223 +99448496001858 +448081211272760 +373821644035517 +231082300675709 +149284086893486 +372325828224406 +229703679694849 +483711790988476 +552131322755803 +72265630766658 +491735328313862 +533092924843969 +193514382668152 +23797480452068 +186371363154517 +93331650325380 +337103192206080 +134115079574231 +256853142206226 +433789348045944 +481386698088705 +90687339442155 +404644194188888 +430613124564471 +84194472837243 +544089520572005 +100792883404662 +450785412879140 +343703884994314 +147420335837520 +33661533334802 +242887372440726 +289825013209262 +550038599399527 +541853958674420 +39332775480172 +191415476771788 +400091896779043 +382358141380220 +202766845674265 +233522322989061 +44850699798379 +373489511039324 +282966142248632 +423726525714994 +31537933715736 +55789409663904 +354282688323879 +92231368452235 +538632572952411 +310180431981757 +253214402336065 +142108738355414 +437032300797093 +57397552529102 +452589157777360 +417910168991845 +281521449448197 +281015162964286 +358223196670003 +390785794829198 +216818088555904 +16237398558994 +190951100824182 +253116003033510 +221368855042658 +283771172694190 +221637388508121 +408775986978227 +526424901809270 +559500174409978 +8647376508198 +180828934669520 +308413261737029 +274212982987007 +556598369404348 +130892471937712 +412024269092182 +284619482039589 +449209603648357 +166822994767279 +116193675590975 +288180200805145 +188906357915178 +176615895595748 +534374705282857 +191838606127882 +250287000698286 +201747936389005 +287838443368718 +338077402118203 +163654763724702 +42826889359315 +101985589186349 +520582780271745 +505272525595020 +561860524744086 +205152516681187 +4895971342969 +370056745185454 +76143844870237 +560144953069720 +39305949105169 +209531363573695 +512282084038965 +412134754917120 +344032447488470 +532569461028918 +341353711217851 +289864786824566 +53355779166554 +24687728439462 +400850422940873 +107014940064546 +511805340631895 +317875374582678 +382716792220559 +226097816525508 +354749176148555 +36099849853061 +377169056414991 +348013459262141 +147811778966724 +445429499774647 +433115967693775 +284212097589816 +506326958741273 +287524503348018 +333236257973532 +281062835849571 +380840324852618 +559844062358945 +277027716387511 +519384919578872 +316224943483573 +156784388641315 +505215894238498 +130141984639967 +392793378533943 +13224684055658 +482653875412070 +516527515357501 +230044031292554 +231862395973170 +311438099204190 +259798380873481 +51554643436754 +298526900274115 +507539764682959 +330449204439923 +73165356925665 +551702795313308 +466493894676249 +251473622701408 +107509600028968 +189166696856729 +140671794791830 +74306956796738 +433829982574787 +433723582306720 +391825563346993 +109630305555162 +81395465181618 +157625322363571 +516854323250527 +281504797984592 +400067616910224 +105436697603904 +229699727006957 +132778387932962 +493071858013900 +446500410097013 +178521235148409 +405118884302417 +310964014902516 +283428742056226 +422800666813583 +149012412338622 +234710699299466 +298892335586193 +364173773823456 +87160044977794 +411144543638134 +350160287777301 +245139969993230 +556662202446102 +526990275140446 +314870512949420 +111624080541873 +315204819385145 +222119828246667 +326924513186993 +8659189033900 +20256822965274 +363101794093322 +235814621139851 +296638902350040 +388542083984054 +375122720220590 +352492443920159 +181330892672632 +281889710415904 +22831651642942 +551741327595504 +20700134643062 +227596448425817 +508369334436594 +380817702826230 +25511982575204 +372163508724291 +390244110467746 +341840725411045 +70718166910888 +549649818646187 +513080082829412 +76648058958878 +153099978152043 +551514534373235 +388065115299017 +121579025657971 +471847713053547 +93820229884611 +330826518502790 +64528135513552 +201888768497198 +388995191181410 +426685907757687 +10777532390991 +182495310767794 +503308992744884 +297642026017377 +92208827896039 +321792582347914 +361783248156295 +205065979304128 +126294995441096 +382986098141725 +221686417935066 +483652060660630 +94799586177512 +16474038517879 +414353924759033 +441147596282000 +545522288187641 +227165048219502 +295538877609619 +248008890675937 +350755058302893 +19487978835907 +515683561247901 +234838078160510 +56449555863632 +251553847095049 +338822526308054 +291502159087373 +476165708168989 +536189274049429 +476079748049996 +103227934627942 +454908816407177 +251158099637640 +453658268016572 +134017743020550 +123093789273235 +470419351973539 +3809650037568 +201399871080774 +345345380774778 +71081520975086 +285655232188015 +172428050192722 +495349596357676 +364851486794391 +391018030138335 +274078747963131 +243511701141230 +283238092660637 +278261998579264 +51399287811428 +113874366313686 +91247698099561 +475557908016002 +420575945042145 +500248422741280 +58853699655581 +440743995611400 +119587327466360 +111720822109282 +250953559234761 +383004771425591 +178723941060230 +554429334175803 +323563016584697 +57855805920625 +281371372578578 +98763265875081 +461565306190030 +288900741329413 +363150535290545 +418157891344504 +153825025523759 +485033683854616 +330422880664827 +311757139609034 +80448286387316 +163023276475222 +287173914898269 +343666475225965 +346793280954931 +361263979971885 +561490373869593 +3614002179772 +117399452494110 +363543445412243 +8877262513069 +16883301847154 +390978731290652 +433400770935345 +56985416513890 +267596817518410 +511298927374687 +461462179143054 +446987847643040 +242302061985386 +96661323985 +267316974488682 +375036882005448 +295739770723614 +43765683901849 +101084334057326 +159561485432029 +189947157268058 +15399867702218 +544079607650663 +45059396174813 +288417883227504 +5439119452896 +486142739685112 +13611061315655 +112245074159684 +126429986746827 +542985652811207 +532462153301676 +384346522589209 +491835486960407 +37855686081193 +471525572178769 +114307023636473 +545538255768260 +380887069193431 +352702579425042 +242265279258350 +516910336926260 +100681474935838 +496927172515771 +106697385562092 +67974173849272 +484278733826678 +97682826351151 +278239736846884 +252049875660153 +100065825143746 +375432012816302 +151643205724906 +215590255507510 +513964516321982 +28370576862981 +546240373608981 +254925568847056 +6266651574443 +508368848670820 +112346877356014 +78970060200101 +457050488132862 +542033372141799 +248856247043616 +545524296374306 +532543564069688 +496882529123951 +287908226724540 +503899802459456 +251989974907190 +509292219937076 +272006508695865 +220073222056757 +281839453952112 +392771584363583 +521369229961835 +99184015854510 +538403451393787 +553847015769243 +4097924653470 +78065681016525 +227046892984218 +128171661502493 +376913717090072 +31252442255035 +14144461061554 +119483400515711 +495599704376578 +150339319840037 +553864971279650 +390534079309244 +130970752676272 +130392068898320 +369884820312428 +333985898501743 +543054607457354 +561717181875858 +421233461758509 +370491328439900 +218594764623325 +500426895952749 +25922854607 +369885110878027 +89037537305033 +472828409278784 +46305656364008 +220340956496876 +216921964608246 +544457849832962 +297175572110108 +432397709514668 +552909038059609 +84156995966998 +221170988039583 +143900478110436 +93091530844470 +157007950514667 +425522921444969 +263027491433660 +34249393165417 +241116853605812 +394705658607775 +492487019658364 +562375650620430 +368078010963087 +353493623993357 +5814674762325 +100118303588444 +293319140424835 +7078081833517 +530289173652828 +172106336310292 +55485804997382 +11339491146805 +470998919854599 +74049757677719 +79424614458144 +503859428006781 +142685386196862 +193224325563181 +492414041169026 +251736352602296 +507252544599045 +253143356098888 +551346180162044 +63032009426426 +231866483096537 +281113571420257 +458558158668632 +324880671392348 +68384155807353 +63067268193497 +449050003797863 +193296990626007 +216664257237356 +25213388754786 +71424308686528 +448291335497686 +72039475564748 +561564591649160 +399995127583647 +82465668060024 +352741647416640 +241243864047807 +172653383298080 +49324372278565 +433259557465849 +320458572136013 +329543033251757 +394210785509811 +2910655110622 +357218221458892 +322188948574535 +302964888693601 +281712128724603 +478253463462087 +233492764442868 +427545456324171 +433170864341001 +195090886503403 +117544682732036 +453719938126579 +377530884893577 +251694822050724 +126182477924317 +249995171506842 +390134080778451 +476507244787390 +403455880324958 +244536445564530 +130029742228114 +468029800779647 +6686227056079 +231630419073412 +122659203731415 +258134485519163 +398848772698429 +243040454410648 +7695054461760 +449704059482137 +394530298025519 +530661826187054 +484804492731711 +525672239263451 +44912629375687 +539974255727970 +82548282990712 +401158184218159 +455091251648958 +323201220638987 +396003870153279 +469643904519487 +80557547864924 +355664161323688 +483031479270917 +329950232424461 +454036159410706 +463551341141276 +218754103552578 +549652397885697 +296265383407070 +340425702904394 +212712628297422 +505723926238965 +2864620608008 +462188510540059 +223005744328557 +34360091407176 diff --git a/aoc2025/input/day05_provided.txt b/aoc2025/input/day05_provided.txt new file mode 100644 index 0000000..2e9078d --- /dev/null +++ b/aoc2025/input/day05_provided.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/aoc2025/src/day05.rs b/aoc2025/src/day05.rs new file mode 100644 index 0000000..9f506ca --- /dev/null +++ b/aoc2025/src/day05.rs @@ -0,0 +1,127 @@ +use std::{ + fmt::Write, + ops::{Bound, RangeBounds}, +}; + +use anyhow::{Context, Result}; + +const INPUT: &str = include_str!("../input/day05.txt"); + +pub fn run() -> Result { + let mut res = String::with_capacity(128); + writeln!(res, "part 1: {}", part1(INPUT)?)?; + writeln!(res, "part 2: {}", part2(INPUT)?)?; + Ok(res) +} + +fn part1(input: &str) -> Result { + let mut fresh_id_ranges = Vec::new(); + let mut available_ids = Vec::new(); + + let mut lines = input.lines(); + for line in lines.by_ref() { + if line.is_empty() { + break; + } + let (left, right) = line + .split_once('-') + .with_context(|| format!("failed to split range `{}' on `-'", line))?; + let (left, right) = (left.parse::()?, right.parse::()?); + let range = left..=right; + fresh_id_ranges.push(range); + } + + for line in lines { + available_ids.push(line.parse::()?); + } + + let mut count = 0; + for id in available_ids { + for range in &fresh_id_ranges { + if range.contains(&id) { + count += 1; + break; + } + } + } + + Ok(count) +} + +fn part2(input: &str) -> Result { + let mut fresh_id_ranges = Vec::new(); + + for line in input.lines() { + if line.is_empty() { + break; + } + let (left, right) = line + .split_once('-') + .with_context(|| format!("failed to split range `{}' on `-'", line))?; + let (left, right) = (left.parse::()?, right.parse::()?); + let range = left..=right; + fresh_id_ranges.push(range); + } + + // sort all ranges to prepare for merging of overlapping ranges + let mut new_ranges = Vec::new(); + fresh_id_ranges.sort_by_key(|range| match (range.start_bound(), range.end_bound()) { + (Bound::Included(&beg), Bound::Included(&end)) => (beg, end), + _ => unreachable!("We're only handling inclusive ranges"), + }); + + let mut fresh_id_ranges = fresh_id_ranges.into_iter(); + new_ranges.push( + fresh_id_ranges + .next() + .context("Input did not contain any ranges!")?, + ); + for range in fresh_id_ranges { + let latest = new_ranges + .last_mut() + .expect("new_ranges always contains at least one element"); + let (last_start, last_end) = match (latest.start_bound(), latest.end_bound()) { + (Bound::Included(&beg), Bound::Included(&end)) => (beg, end), + _ => unreachable!("We're only handling inclusive ranges"), + }; + let (cur_start, cur_end) = match (range.start_bound(), range.end_bound()) { + (Bound::Included(&beg), Bound::Included(&end)) => (beg, end), + _ => unreachable!("We're only handling inclusive ranges"), + }; + + if cur_start <= last_end + 1 { + *latest = (last_start)..=(last_end.max(cur_end)); + } else { + new_ranges.push(range); + } + } + + Ok(new_ranges.into_iter().map(|r| r.count()).sum()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED: &str = include_str!("../input/day05_provided.txt"); + + #[test] + fn part1_provided() { + assert_eq!(part1(PROVIDED).unwrap(), 3); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 509); + } + + #[test] + fn part2_provided() { + assert_eq!(part2(PROVIDED).unwrap(), 14); + } + + #[test] + fn part2_real() { + assert_eq!(part2(INPUT).unwrap(), 336790092076620); + } +} diff --git a/aoc2025/src/lib.rs b/aoc2025/src/lib.rs index e051fbd..f290640 100644 --- a/aoc2025/src/lib.rs +++ b/aoc2025/src/lib.rs @@ -2,3 +2,4 @@ pub mod day01; pub mod day02; pub mod day03; pub mod day04; +pub mod day05; diff --git a/aoc2025/src/main.rs b/aoc2025/src/main.rs index 8c2e4be..74ed9a0 100644 --- a/aoc2025/src/main.rs +++ b/aoc2025/src/main.rs @@ -6,9 +6,10 @@ use aoc2025::day01; use aoc2025::day02; use aoc2025::day03; use aoc2025::day04; +use aoc2025::day05; fn main() -> Result<()> { - let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run]; + let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run, day05::run]; aoc::run(days) }