From 8a22da8ba11a4645de8009fb0a818286f49a724b Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Mon, 8 Dec 2025 12:08:24 +0100 Subject: [PATCH] 2025: day03: part 1 --- aoc2025/benches/aoc2025_bench.rs | 2 + aoc2025/input/day03.txt | 200 +++++++++++++++++++++++++++++++ aoc2025/input/day03_provided.txt | 4 + aoc2025/src/day03.rs | 92 ++++++++++++++ aoc2025/src/lib.rs | 1 + aoc2025/src/main.rs | 3 +- 6 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 aoc2025/input/day03.txt create mode 100644 aoc2025/input/day03_provided.txt create mode 100644 aoc2025/src/day03.rs diff --git a/aoc2025/benches/aoc2025_bench.rs b/aoc2025/benches/aoc2025_bench.rs index 6f8d4de..4cbc578 100644 --- a/aoc2025/benches/aoc2025_bench.rs +++ b/aoc2025/benches/aoc2025_bench.rs @@ -2,10 +2,12 @@ use criterion::{criterion_group, criterion_main, Criterion}; use aoc2025::day01; use aoc2025::day02; +use aoc2025::day03; 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())); } criterion_group! { diff --git a/aoc2025/input/day03.txt b/aoc2025/input/day03.txt new file mode 100644 index 0000000..611b5dc --- /dev/null +++ b/aoc2025/input/day03.txt @@ -0,0 +1,200 @@ +2213322222223222132231233322432224423222226232323522232215252332221122222231232224232722131522422232 +9869755494885236452767647548826258555668838497687557968652485746757835445145546687436356799481798587 +1333642233323333373233223535543332333343733333323333333333315334333323343343333333332321233332333333 +2412331232332223332211413223433332213222252142221323322122212223222321231322513323122332321233233222 +7233333554333634354333355353442655334523373532334435335733343124433353732333236335456441345343535443 +2542623755573343945454412366525442424744334132133146357442166333737542732631343535567546644733453327 +3223423125721322245221313311333222222222322321672232321322215121332232323522331112523842131211222231 +1341524443834221222553325236443534253327533543493534348242333119334424249244524325448544522322512344 +2122222234421423322222232231221455261322312321232223321232322112233222233223322222121221322232225222 +2172228252452422574523272252246532513514251543422251252233622213162235313243341322723224223472223342 +4333333322374312333333442252233332323343236421333332132232233523333223643444244332233333333314343374 +5564564445425635543326425252871657266475435535644335444428545455444345414454654413464444444543452332 +7454483454246843436686564547347643444463364743844643844454433455547944454424656442455444258453453949 +3545239364734434356884585159793673287754643667512332857278643456465359873642643268698837552366444279 +2222614212122412212232521222222222322162224122212255224652222252322242352254132522221123221222222222 +4654734524468553536655551947342343744798353537531365525555255373383337465222438553556563343546674795 +4223343232434234223443433132443232333433233241333323334444234334443434435341513522333132223342423334 +2434743225152133244934233332223333342332742413225323434322133133342225263461325222442331344323333621 +4363333625436326121432327152115232112437232426739557622735333213153454854223553223332333163322332262 +3291224524551531661124222122232433443332372352223452323152254212524224475353217335338132112225138382 +2532334224334412444233531343333232434672372334424223232433234233235242243334444434532142565432224543 +6575467444354636644724356545439443727633232211663455734655446347433346552413539455535454456546344467 +2212123522212256112142223212223223926221423333322111142121322246521222623231121222222233122682282232 +2427422153354454221223483334425221233131444333342323144413423342141422222343422242225233412322852334 +2223222262222351122221222542222356232422221312224223222121224242221142612223533222222326212112222231 +1122221221423121122222221122242426222322222218222222222122222424232223133224227122232222225212322431 +2223225222263221223122232222231322132249422122222222212343223233221231112333321122224822122212112221 +2234133242122472222333323231152222313322242231321234122233314332232125212342222222141222224223222243 +3222421121132228332482223722323212227931233222232311122728212342422323911121332241322122212225222224 +3342221445144453253254314734532247546356242543223144334535244724343441632342442234353332554531544544 +6644254436227552262158566262526533915631231552333722416565536354544652263755643341664563253253335325 +2212221212221266252415512232524121224221462212225225525255242115212235231241252544312222525112533222 +1212222242222222122121231123212212232222221112225222122222212212222222222322221222222333222222232121 +8437515934322544325244332733235426312213422543233274311523432242324234425333225643164333335325158723 +4955559385766953565255766876476558356667683243721462495465455956585541549636358359356553352726754357 +2321212231311222332231233622451222223233632131325333155422313242233246235373323463222222233151352225 +5554455544442245353244364445444442964455743554423443344234626245334485444521454444353644544434348444 +3584546435435438362463553335553744339333343334752434593543434442333333333374362737434422835322244974 +2224322223132222332322223422213222223821212223222243223232222232232222362222323122422922222223222321 +7532213233323334215212222423122123222322425222323732234174212342242222239222238232423135916233324313 +4725333553331231732334534535433413336333334433335633862233333333343434333253574633253313333533322533 +2222122222233222222212322321212122221322122123122322232112231321222234222233122122113113222142221222 +7618667762444757529352444283355721866946122758937266864545275866446765437263255944277544354266743555 +8775885395735793768798676737467545866325994734656888365669834844567693699478564767598836687838775796 +2323222163231422333232265212225222433552125522231421223232222242111255222111234232222234643453222212 +3212222223211242222322222423924221333231242222215221222222222412222332242222342226222112342222222423 +2312517442152222775232225322152322134642256726212312232253122631232442222322774322261522237322225221 +1412232232122132322214222245123232224232423122523323323512434322222122322223232324512223213223221225 +4624745444557739264643334474753442344576454464364444542355443444953574753765466446534445555247546645 +4444445543553433353354455144453453542444435444454565454246452554274346433345434464454144453534444454 +5222221211223232461222322221422324122212222121524122122224222221222226222282222211433212232262313272 +4443424217342232322234144234322244432422332123432122562423442343241152444222734444343343324245233331 +4625363444334325314433134332342626273324257534444232232533221335211222233354622142319633834554323252 +6411222232214222222522522212115422324312321212512352222222222521221222222422213215222323222542332225 +4443435744444443443454344444553646453444473243634337443343333445443444444444544642353434424344143344 +4222322212142222131123232424122212623122325223232222122222324172123313113213221112221214131212326232 +3628228272226822792212222291522222262281252222222122523312222224388323226222431232246223822272242222 +6452232522272722232314236322514222212124422122363122133442752472323254631272561324232244232223412433 +2232232212252263221723231243261132732225353342123222262123232223621135231232353226314132222321322523 +2234532142534352552432224445238325213424373452334233522333242532331322332283352427475226322153223244 +9472212243122222252422922222363422333321126243222123963224222212173222233224252234412121222122821213 +8687898777877956884675896696763989727598788596888366975787785653266667396852928885385879777658746578 +4742438327346661542272567435214432666247432532364464374423432252594887642242783264284248334549366538 +2843543444552553332333549456453152452655123452245534625343434434394353525145545815233325253353558525 +3244334344333322333336326524543522543353334524333542335243334463433424253344265152434334435422433433 +5123213122132653223552342918633223322124141221532722222222633832112321232362321212223224311252232323 +2254522224536212312521242224331285151323234522312332222153222222233213252422324212324255413222233635 +6932637327758743246546437713551343621222422345652667325366263533277364672223242653614462113113621452 +2664332353263783333136373783525676334732434213262462433235335292339233763346833362333754633332336332 +1223222424223252223213121222322322216422412224222612422223221222312122222125212222415122232226422521 +3542584332435333333233354353545344224553534123354453332523352334552335453523343512333213234387634133 +6477957673773844477447976677777779677788757572445797735766653875666759654537677675353627655576767927 +2122863223233922223222231121222335332223122512323122233223421212114232222322232221223392322222521322 +4312524534123246451432343643224311242447241134644264223443332245433262232641376442432172472222642235 +2275325124326274246343242342674425241126422624221639763313155173323211554216434553212654542522244512 +1538376354635334634345152866523443431672342341845227336436433755352916223414454255126676464326465453 +2462132262212214732224191166344355414456333232523325425323262172634333344212243223136153134622523124 +2631319623233673572328447522142326723725312315363222557334327382237347133534333342445538226351733877 +6674525765645736755277426478666374433478565771746335482576661384446336527753576379765114777324835247 +1445214354534443124443143152322124342543358432243223532332224344453244325421233463452464443336435444 +3723543344762463142452771243123333533262534743357433433442333434323237223413433322343122246434425297 +2522212232142222322222221222222123222513232222522212222241212423221222221232222422123182421222222352 +4649425575652455655345524446535555664456453353551334555552535554525353745544253454663345473375355545 +2634673446446644485645555244654443495523355587664334545525752374784735443457755455456485493456463553 +2242233312853232212222233154124241433323223342432114322372333333333232333133312323424253242333222527 +2252222222122622131222221211221252222222262122342222322122323222252212223225224113322222213227122112 +7145153421321212213232213242421325372523143232242542622222334162232643213463222315224332241623233632 +3324124233321322222222324392322321322222222224223432342142234421321364212212331212326242323313313333 +2472266245644522634774327358522262635513323335182372254462662143946277975225436422277562365266655423 +3454443243442454643335544534416344433344445654442214445434214514463442433444324544444345444345543344 +4367979552245565165576424776225962637573364823785465568368636312742298786276647827575676469244525653 +3225626443313133323723323323333314253554364347343365333323333336333733232322233333323433327322332332 +2573343343233434433434335363345452556353493431488327365413335557454378339752343364644435453325754543 +3332172133227223334324253377332133233323223333532233333322112735223232322223234272123323333632212223 +3454534475343444535444424745434444444344455444623375454534443434344544444438232474343544535454434444 +2644342235344483342633422636454456365233515643444645826444442446956214445156322264445433443453544256 +2223435334423434443323343432344434441333333333334433523433332124432343243433314444324423343343444233 +3212252222442321323221225293213122528329522211182223418222232142225234221221222222423222321246232122 +4353432493542334325235547332734383332234542144323893258347384435444429271453353353448434142247423122 +8337935245577558348845559668464454534353766484438335543755757443777274244484345834388345496944455467 +3443463333334453433232243443222334133233433234432324222432323533323324433242132332332124242343233322 +1435542554154543564533441335544555244531645445572234345443345431434433534143565444437433564233354423 +3333312334342233331223233234233423333333333222332233423337233233333323213132222322314332323333244443 +3144453439443334333662444343463453336433435352354493435346436453383544432444466337453337853423233436 +2621273163344422333343232136211636224222434231116342222542232213433324642324241176224342723221422332 +3233236263233353563335231233252323223521334126123623332512327322312223333223215221283522467362232326 +2232223222222222223224222222222221221211221222123322422321223212222221322222212221222221222222342222 +4443435431454234435334436543423453441422434424434246438432132274643321323342524533232532212314424334 +3434222234424123524222452333431221233121324331412242454221434433122432415442443243322313221343241245 +2321552222525121432422322331221528511324222222222223251323221222325322125252122222212222223233125422 +2113322212122312221212122222122232221222332422312233422223123112132122232232231222235122334122222212 +3732434222332433314434542433334343433225344323314343343423334323322433232435323333324234332413313341 +2243352221342443222271236434365322934422234223125222337624122643272322622437441322243413243473882412 +2527574826427351353683632227267232139241272336778354926836323453363482545233625417243233594635373371 +2352124323843333323253365635322233542433122438331432234333233233552321434213213222343343494522232371 +1322321231222372222321132512312222322322224462222223322222222221223222233121722212522142211233221233 +5444455244355434653244514353733425555432435524445462355644544453443433445226446336553853553454444444 +3332233333334338391321272312134322233863322333432334333383232342334223332333234786233333432424325933 +1423223524424321433522322244232223331233231333313336222333934223422133232353433212332232224322322433 +3846414434329535333324434434463744365584534935533343538345434362733943434244645366346976364533243345 +4643437654442357354452643453433595563365355434644545556753685648354446745633345454324745655324554344 +2131424213433334334241312123344324133131413343442431244222441243341422141411322341334123242221356789 +7416322321423422532433252263461535237341336423224758925417544133763249354532465584557477436637134868 +3532232342543633244356234447125413363324323324222289345334243223343344349423623323132345434545334456 +2222222223122224523322222414412221222312521311252212471135216121622212122422823223224222252322223322 +6844572852724766364376246428458471423425425655656355452483274422366737555436524246373644225524226684 +3242324353115355355323223333323433133532332325223553622433333532532333223134223363333333733357422553 +3626353353644274241533142456273326314323662575363324353232556433624425644535422316323346623244545532 +2724454534154225253459433524763422545426423332642224232423242219414234215362382511227546231223755232 +7442133222243344231433423324322522433233342443244242433423242443424324213224132442463664334134342334 +3463633243546333375266535312435244564434333724665664625374363683533955743535443448446155363433352435 +2264222222213232253131424222232214292212282234282122225323422451223621252222122222122222532326531252 +7615864866564643665565345652664464866686665436644563376466655765566766633576745646454764727756626448 +3232682631537442445212595232662334363343227323835122465351345356353233643332543416323346345346643365 +2477354147362541612246656262342473335717542264534245221467537422444573332674431531423425215312544289 +2334432533153324242344341754352431132342242483614364474473434824424423481248441342313143738143793223 +2453444254242142424354527214334534514423135772455224332544144347235744422234224425443544453243344614 +5212223252122422331232123222142312233422245334462222222225244224221242232221512243232224234433555222 +4663361654535434444435333426543336213653424435533654435334427263236324336542736224533733545413433239 +3363338333145338474323437333462332324223453341452444327323423238343353864322373218432131334133331333 +6252146332321485753632148626252215423622661447732432532425663473353884553422633662223652246821433466 +6323212434222112525725474243264325545472241252227222635137276274422143227243432646722261633476517122 +2292128532222284521512222622221222422421324224222681233623522223223262427233231272222123235231221181 +4286246955215254312515631624225942342242521826335223234535339245216232525515324431229213242425522424 +2622122232123521122122322223322225522212213235316222222233213212622122172232222411222222312122211732 +2392322272325242232246227222232731732621613276422282332227135645532724216422152724335327741242231462 +2133344331342334353334326416233445424328334428226343632383533126256345224276253624433232223322429335 +1313445155233335142345133432424315243344534322414122115152312323134555232124514425531444334531116789 +2176422423121432443422442232123322224224142721224212222312511322231432233234223323244211222332222122 +1323212331222132233231423322222222214333233212344223434312442222492221354321222342223332373533322621 +4664434362444745561444535453345435524434346435454274864444654324476633565349444454455353443364156363 +1638584275546117346163717254773527552852888544638242767347444874466266733617618166164466374362765419 +6484454656734464566656446354242553953738747675494353263545534655932284732414565465456217445446466422 +3485414422321524833475231333422824247332244623522414142222262253355414452434234344554242224422224624 +2333263233433521134582215333224332925436375333333336732316531375333522444212233753383365335354643243 +2322132222223221323111333232222233332322122232221312232253222222232322332431323222322231242312325223 +5335322444334334442443232426223323234447129221322222234335221422223321222322222324423233342232233364 +2233232223252234233222422323332233122122213133422132233223243122322122142422223322242222332521212211 +2243222246232222212215234223244341293823233413422262122231434142334224211122243244723212111327343722 +3313433324333323233333323332233423333523353312313633334213233333425232634123313333625322322224342122 +2223221221321122424222323113232211222413233225122222222312222222225361224122154344233232252313223232 +6426445154869355363363722336455335472343572534323456253645532493615956527636643453556642545246266326 +4342252241221222341214232232226432422223222214311224253224847131242221312222431142152243223533228364 +3122222221312333212221331241133322212322122433312122322332322212332222212322222142232224224432241333 +4758777554587868737644678898656677787655757385768789874455858856835848887478458889776575587488744887 +2222222322222222132444434221222223222211212222421221422124222221223422331422622421122212232221222222 +5543453232434923234235323433433652346343532235133533353325643323414321344433315332531344244423332533 +1422222221222222212122311222212124122222322232222122112222222222122221212242222322212222211512242221 +5624318328343613762342133122287362233222333228233526432235723228212317225232222321322843831822233343 +4745555462555745374333447645563565846565658565674475358542453455774373535925543556456253364455655565 +2231323341221332332231232222211222122232242312343424133212222221221623132213242312314213213122222113 +2342415122621132242242322221225542255222222222112241222232223222323213522222122246221231225232243224 +6223131445352273462225532517237771421222422217426141752312626563272261233221417286234434252324661125 +5222222212212422212223223322211422113212222441222322222222432222222222222222132132522224131242222451 +3422323526233255413253234422122625354565276426242722342264341342322432242227231221345261423232251243 +4455444352444254693544224343146552455424553454457554355354334375444452434455453544443455535455225454 +6695865665676765678566666576961755966654564567555567567556642646397966556665359666465757356556757769 +8357544347353366537753222233513443263326227543433534321424453266343553435425423918643343433235261323 +2222222222222221222113122221132222411225322222222222222441122332222222232132115232313221145522221221 +2224124212242232553331122222231252712234224682324732533127244253223113212252422561212242222267412142 +2155131243437642435225242233334327262528545127324323242251223242215932221342512431224563331215223232 +3233233821422242243223423226322333331221244225322333614732324322164332232342254245324342223324333333 +5323233433722283363642223343334264332334256668558433344333335344774523223633116853423338533332265733 +6636218835567665856535755855763526554865876567668671763558535573733875754666654864563576447378687647 +3242212222532222453222221222221414445243225223253221212124423422213112225222222212136322222122223223 +3246242324335263534434465364444433354444733144444444634444442415237463482176214523646425165642842254 +2222122121452242222112223321223211222122412432421222323122222422224234323112221412442225322222244223 +5555533353644375536354623527576345645257454546563754453235344566547743535526353443675773695567647537 +3333334233422333245534448312255264263533563132333625233423334335232332233333924442432512333355242243 +5335324246344212334344347222166443335324633214664133136352235644245321635434234446546333352834514533 +3232232233742222122234123363441422152241262422235221425422492124252122242244323222152522222164124334 +3462333528242224232212331221524224225335332337777262223233218633225162762134332832833282226832233352 +2632332326122221121112263242232222122432223221342223342224123122112324132321112262322121522112113221 +2643223122321323333222122131322331333331122234423732236222323333323223232221311311224272231332243223 +1222223121241124122212232223223222122522221122332123122232231322212311251223333222513332338312123112 +2152323434738233433164425517324546331332242235424332433322422413432633344543243344744343237342432312 +4131322231422253332322233423433322322232222122431221333113333223333234213123233223231323332233334323 +2463363425242612416264341654545345115236122326652335252424331225332623114245461624526144136446312789 +5832235534333334323334233312133213343133323333875344483233343683364244333343536273324539333242273393 +5336252424463368242344342182144524854253313325351235445322225455244424225253125634815634443442624924 diff --git a/aoc2025/input/day03_provided.txt b/aoc2025/input/day03_provided.txt new file mode 100644 index 0000000..7255fca --- /dev/null +++ b/aoc2025/input/day03_provided.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 diff --git a/aoc2025/src/day03.rs b/aoc2025/src/day03.rs new file mode 100644 index 0000000..bf0e1d3 --- /dev/null +++ b/aoc2025/src/day03.rs @@ -0,0 +1,92 @@ +use std::{cmp, fmt::Write}; + +use anyhow::{Context, Result}; + +const INPUT: &str = include_str!("../input/day03.txt"); + +pub fn run() -> Result { + let mut res = String::with_capacity(128); + writeln!(res, "part 1: {}", part1(INPUT)?)?; + Ok(res) +} + +fn find_max_tens_digit(bank: &[u8]) -> Option<(usize, u8)> { + // We don't include the last byte since it cannot be the digit for tens, otherwise we'd have no + // byte left for the units digit. + bank[..bank.len() - 1] + .iter() + .copied() + .enumerate() + // We want the max, but use min + Reverse because min selects the FIRST minimum, while max + // selects the LAST maximum, and we want the first (to leave more choices available to us + // for the units digit). + .min_by_key(|&(_idx, val)| cmp::Reverse(val)) +} + +fn find_max_units_digit(sub_bank: &[u8]) -> Option { + sub_bank.iter().copied().max() +} + +fn compute_max_joltage(bank: &[u8]) -> Result { + let (idx, tens) = find_max_tens_digit(bank).context("couldn't find tens digit in bank")?; + let units = + find_max_units_digit(&bank[(idx + 1)..]).context("couldn't find units digit in bank")?; + let (tens, units) = ((tens - b'0') as u64, (units - b'0') as u64); + Ok(tens * 10 + units) +} + +fn part1(input: &str) -> Result { + let mut sum = 0; + for line in input.lines() { + let joltage = compute_max_joltage(line.as_bytes()) + .with_context(|| format!("couldn't compute joltage for bank `{}'", line))?; + sum += joltage; + } + + Ok(sum) +} + +#[cfg(test)] +mod tests { + use super::*; + + const PROVIDED: &str = include_str!("../input/day03_provided.txt"); + + #[test] + fn part1_provided() { + assert_eq!( + compute_max_joltage("987654321111111".as_bytes()).ok(), + Some(98) + ); + assert_eq!( + compute_max_joltage("811111111111119".as_bytes()).ok(), + Some(89) + ); + assert_eq!( + compute_max_joltage("234234234234278".as_bytes()).ok(), + Some(78) + ); + assert_eq!( + compute_max_joltage("818181911112111".as_bytes()).ok(), + Some(92) + ); + // added this test to check for min + Reverse usage + assert_eq!(compute_max_joltage("9892".as_bytes()).ok(), Some(99)); + assert_eq!(part1(PROVIDED).unwrap(), 357); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 17034); + } + + //#[test] + //fn part2_provided() { + // assert_eq!(part2(PROVIDED).unwrap(), 4174379265); + //} + + //#[test] + //fn part2_real() { + // assert_eq!(part2(INPUT).unwrap(), 31680313976); + //} +} diff --git a/aoc2025/src/lib.rs b/aoc2025/src/lib.rs index 28326d5..89345b1 100644 --- a/aoc2025/src/lib.rs +++ b/aoc2025/src/lib.rs @@ -1,2 +1,3 @@ pub mod day01; pub mod day02; +pub mod day03; diff --git a/aoc2025/src/main.rs b/aoc2025/src/main.rs index 1cb26fa..7719183 100644 --- a/aoc2025/src/main.rs +++ b/aoc2025/src/main.rs @@ -4,9 +4,10 @@ use aoc::DayFunc; use aoc2025::day01; use aoc2025::day02; +use aoc2025::day03; fn main() -> Result<()> { - let days: &[DayFunc] = &[day01::run, day02::run]; + let days: &[DayFunc] = &[day01::run, day02::run, day03::run]; aoc::run(days) }