diff options
| author | ame <[email protected]> | 2025-12-05 07:19:12 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2025-12-05 07:19:12 -0600 |
| commit | 610b11cc5d32b9d19acf0809535183852fb77fc9 (patch) | |
| tree | d12ed9f5d73c47ccdef47e2b03f38c8c80c9bc2e | |
| parent | 22a92ee2ed33a5f2246f1c6498714ead9b9c65cf (diff) | |
day 5
| -rw-r--r-- | src/day-5/input.txt | 1188 | ||||
| -rw-r--r-- | src/day-5/part-1.zig | 43 | ||||
| -rw-r--r-- | src/day-5/part-2.zig | 58 |
3 files changed, 1289 insertions, 0 deletions
diff --git a/src/day-5/input.txt b/src/day-5/input.txt new file mode 100644 index 0000000..fe4627f --- /dev/null +++ b/src/day-5/input.txt @@ -0,0 +1,1188 @@ +540454167785597-540711647857062 +47164826750116-47361024801998 +27474961693948-29893795471178 +264187939062085-266337614971861 +293208116268102-299836122735369 +144972871770017-146438622404687 +323511129733906-327067169188838 +386649940982427-388297695380796 +73018885785882-77834026423292 +340002267637139-340423346550934 +47711414732167-47816322021519 +260262011998310-260826912112224 +168565238838403-170517892204396 +382102874239472-384159769313151 +260262011998310-261010376339511 +513425155207406-519242373171613 +547113177391816-550487902990229 +254687108603027-255198701548672 +485306754309739-489648627846884 +45678368924024-46206678322473 +547113177391816-550487902990229 +83381185442305-89649554304739 +475569256770359-477139243485814 +73018885785881-73018885785881 +33993693620108-37919611527078 +472794482814995-474292513055893 +166421815953204-167901400575335 +258927042113314-259389584228189 +155241990375049-158753341898209 +474862027613498-476224424122179 +385710089455260-387122378737037 +424773421487922-427909317529086 +443650041368133-449711789570714 +253909423627332-254552448702484 +354090997627981-360400073357417 +45831876545960-46206678322473 +364781170615862-370593133584923 +47711414732167-48101459314590 +199348484912081-199348484912081 +256008294655316-256636628520694 +42184811751483-42682516906390 +473773913132785-475412990208328 +527069855755229-528592316960898 +455385585931523-459078577415435 +392760691823518-398125184937122 +274791516493515-281138057310686 +257950740608376-258422000237680 +478874126966345-480309227839684 +48326561073747-48599748552764 +497337647859080-501303458833742 +20692197063956-23092969644467 +162163791764019-164366271638659 +536935676896000-537505737647812 +164712017369033-166853360820255 +167642349569820-169073614669423 +340608453651022-340947657402530 +225088098372733-228903696285896 +41349907562740-41774522574207 +390013339422826-391759866838103 +407187118727525-409439422719588 +44028355347876-44353049092078 +243032338439851-248434525048913 +538678601952706-539508731838689 +45678368924024-45831876545960 +337671295783509-338443713548755 +44353049092078-44878901857542 +10969489721640-18403764801933 +539508731838689-540169691934971 +539636923212502-540454167785597 +111393026131754-116938935377521 +334299926097448-335143065701699 +294923081766845-297213155191351 +211893144138359-220416214547869 +534147464247588-534621793593873 +346252325310312-349558579467732 +494606678393202-497337647859079 +228903696285896-228903696285896 +89649554304739-89649554304739 +538460631796040-538678601952706 +332717616529180-333279017622435 +325101148651025-330998081341259 +536154434489667-536776174176003 +43774488718992-44353049092078 +21946294718820-24528674478081 +453019836282384-461450574663037 +266337614971863-269075739287179 +134151642134711-140451557409157 +463955630048892-468633815583198 +338443713548755-339218340247309 +123060670164629-128981806607990 +63771016673447-67073553206566 +340947657402530-341399417684031 +43408475984169-43675077324539 +254232000311871-255017715380721 +256636628520694-257565932343078 +257950740608376-258308132270004 +131956244068873-137127079390932 +211893144138358-211893144138358 +392760691823518-400527303704705 +246338384337094-248434525048913 +255810052866577-256515412495176 +204345522950590-207871654375939 +313598152797785-313598152797785 +477868844001775-479129700052927 +42184811751483-42939927863714 +344557419547678-349558579467732 +504829478003777-509416972891685 +144972871770017-146438622404687 +537765521372718-538026764120355 +106761159794207-109797286672589 +260578492864582-261010376339511 +335803852255792-336387086275389 +48101459314590-48599748552764 +338168580267807-338928679802043 +103413617754955-106761159794206 +370593133584923-370593133584923 +519242373171614-519242373171614 +538124733727473-538460631796040 +163637092812629-165542675761152 +2926849656827-6639293324113 +387960137643219-389395266528203 +388795164871414-390585414413842 +403689979353004-407187118727523 +506791032763760-511098353423119 +334299926097448-335143065701699 +335143065701699-335803852255792 +540711647857062-541466479471885 +23852018083598-26268590281663 +173766890438257-175957758472865 +175957758472866-180125042122148 +96500992032243-99132130072424 +285767263902242-290808407176951 +48979218601718-49564147441704 +554887650114065-560713227264706 +433693270104647-437881862299513 +468633815583198-468633815583198 +182040467171807-188667090942650 +414963061556680-417705391099450 +373228386404357-377950047962450 +541968326469388-542149392091081 +414963061556680-417705391099450 +32473099854200-37919611527078 +231837945632642-238563024987961 +340947657402530-341214100494085 +92322809915656-96500992032241 +253136032139253-253606844838174 +253136032139253-253606844838174 +271796885540557-277006839171499 +373228386404357-373228386404357 +111393026131754-116938935377521 +332717616529180-333279017622435 +50706119275619-53964824210181 +332319403698102-332912747021598 +203262950779385-210427885914978 +303126505727739-310298147703265 +253438872172155-253606844838174 +310298147703266-310298147703266 +182040467171807-185420456545167 +541238915960481-541968326469388 +534621793593873-535263461409367 +252347920603579-253136032139253 +155241990375049-155241990375049 +542149392091081-542533569726705 +476780475201064-478171015798688 +42184811751483-42939927863714 +50706119275619-57471846270140 +533506887654215-534147464247588 +485306754309739-485306754309739 +25262408385979-28074434070277 +534920859509743-535529424319425 +128981806607991-128981806607991 +282968252939607-285767263902241 +313598152797786-320447294233974 +161136839625356-163038166329909 +339558788109240-340423346550934 +433693270104647-441649413313212 +384411421486550-386205671495504 +383406631215165-384974257669361 +12954172791733-15610887630558 +193243486057894-199348484912080 +336585523222916-337297442663494 +63771016673447-63771016673447 +480606378042301-482278771585482 +427909317529088-430361397578492 +479913703942275-481460647464452 +446911012675040-449711789570714 +527069855755228-527069855755228 + +382839938971561 +447434811249226 +348210610413371 +23528822942208 +85988034357557 +194671187991301 +99824211457541 +159984808765965 +85422147166694 +511534027238946 +555375700763923 +52515598836021 +14408529630278 +140493547696754 +308719565974968 +537011098851969 +390624582939241 +268314963855915 +527606620951678 +474353790604050 +443096388063473 +204171254701933 +10467439737834 +376443656563101 +196580664742312 +138352311132292 +550455135130123 +369869955532733 +228292964245482 +209946128283418 +382544299003090 +304850953054012 +302708658368759 +473332172349276 +90546420305086 +527184810216091 +179344578276627 +97452901968883 +294671361827759 +339292091033138 +288341733520611 +495993850228528 +176877626303456 +458871640158806 +328403182753711 +16430965806449 +345096697201293 +74592036199098 +500022801860384 +200399479761410 +287283694778057 +203721120003958 +104444913397657 +144998136592647 +397387664612927 +407878388168845 +57220822220697 +167331688113467 +333955324819300 +514364456629289 +511674589829677 +81379625672598 +13614574034430 +314104335234012 +47467555302303 +284329673758408 +421404644860907 +455669585861269 +426283760986170 +220377888903027 +541804332826809 +65335360818361 +262242961234005 +115762694026857 +496825522493648 +264697863276373 +285086267360837 +342443644083136 +393987287093834 +105894908373324 +395616933856847 +254844306049852 +558168602430614 +375229787948549 +147894361239897 +330002764477058 +245183499471471 +394436482888113 +11152852917319 +125980697983069 +290539205746503 +242469834763652 +107841320105052 +326375589520438 +95287668272792 +434198172836042 +41464507018561 +390620949096099 +460267710012352 +516558164159562 +86063768541616 +295586876547405 +29044264108514 +365869596821297 +253367528894115 +252706453825703 +344694920657477 +343921153889083 +395978012049558 +473666516136838 +255067180768890 +123477953325848 +254906779592362 +508899089144486 +55495064478382 +516932997889695 +318051810854530 +116713791986110 +273790788212491 +436296741262164 +260844985583634 +168071256003198 +539525641356921 +195726650109983 +478988651076940 +517391763549912 +314517083422715 +213147710234537 +7963993156539 +337345983796093 +547899495275017 +233971202240819 +187750328264848 +294969130308213 +481199494100942 +387557195841196 +473247584737973 +34404463194603 +465729625848588 +247058744775882 +216285604225485 +359538542302917 +553197269650341 +84839342894745 +275559604901380 +86579740434983 +235025195555550 +456050281366569 +242944726051513 +172333207529250 +266121348758300 +113088074808791 +342629250799777 +457388851637950 +97929520959347 +127950024847508 +16367270078223 +509610477968229 +259457132637135 +43630787661353 +93281853321090 +109480139138080 +462221279580485 +447963913967167 +540344966721572 +305032508573519 +298696360877979 +158618502188946 +76835989888591 +508043714734454 +556908589656477 +268823263037351 +397978204958102 +315795912864561 +549171525558226 +85082059056753 +354206549844257 +370578308759406 +114520148898852 +183366346394289 +548473534066129 +225378743388984 +13925038011536 +142462203540894 +404474421661377 +53159773407499 +459568966410784 +155553932435100 +289933451982334 +109241513578613 +37041655996803 +142854999819703 +6554904317591 +520624710285791 +64119576376150 +287362511993646 +135014011725429 +386584846689976 +559717837968327 +313530145095588 +167360065421644 +61600321168509 +274048303272874 +26312639326248 +43910586315895 +391159835444045 +238074823906384 +365348161814833 +217316439326930 +478575611735754 +556968982007431 +21045935141728 +233710876722817 +84238467343806 +502182358675396 +456949534300085 +534274189071299 +300364752122685 +133493096417758 +27135227588011 +167693933242754 +123107593884922 +127490939928926 +254278650997450 +145855377420705 +371780155970132 +23589634778240 +307748514486034 +76257779074241 +307163622384419 +123101566415140 +52775881475450 +519220589229781 +300768005649361 +488610513702966 +442763454109579 +237728754024091 +135070768170576 +27435613674479 +469960774597388 +197373152599498 +14520820089728 +184115074915869 +503762198507297 +258918869361155 +434465652603265 +373721161995218 +55419261146263 +352902363039084 +446137321222876 +480477793241004 +514954044778648 +288086109779445 +82996868612543 +309354875303837 +355957459307195 +349066229394654 +194946961672852 +506455134784235 +495778192879709 +394366283007215 +242905076185229 +212037707457802 +125012819749448 +268170025868451 +290811293769894 +396369894193601 +382431581446817 +267255565833966 +258556579758828 +354449216494937 +279879905967039 +334292262290527 +236401513260350 +457667429366668 +131743036107796 +123336681236175 +549227891771426 +553626977426283 +465338499149714 +327202754347260 +466682430594006 +435060910049291 +10539103089162 +338800235581462 +459942759878698 +155298312666852 +109697721191519 +518564489047660 +319480615339952 +256517660240034 +356310641977878 +376638270930447 +164674813753382 +93555192236514 +474597215871001 +355640672520852 +7295346905028 +181254350819514 +357774669926396 +116378068978947 +127463324729491 +212780663323296 +537295645459946 +2638574030914 +205982835117945 +457165415287529 +515735873997276 +365724602715004 +360675799392124 +33168525281483 +73312084324166 +100709393076794 +393648750914735 +174184825915432 +360267370863040 +338163974537439 +446810396220383 +286905080765851 +308577435545202 +253436985410139 +429366186852221 +113603628906560 +336683174799732 +295052561658131 +538546940759973 +458875954703713 +529712024400754 +64947543181590 +35254076699243 +179023412905599 +162147630924536 +115596890842148 +560403834949721 +26946746537252 +395325968328506 +278738189012887 +400451238386283 +483222301596470 +266028443272195 +291616933007556 +375923828710703 +177329515225451 +185020081779596 +557977130985342 +126623406630979 +267720212844246 +469987877375894 +128024018624973 +316735821541027 +433988539861646 +76532946918621 +208585429382858 +236247693371618 +399292427209563 +43303253837693 +547895534393173 +387450202415237 +125229783113192 +508935156309245 +22198197287938 +25915627081350 +151113419196003 +113784545853342 +345448292880917 +175595947746851 +127020898398050 +385884296200968 +432567430493566 +232465521365422 +40308335784604 +44396095626889 +264833782476464 +138096977072741 +279847043850608 +157345375023625 +16650341034345 +229636891792264 +298339168498648 +179658890504240 +267049127947522 +159557106848908 +40698698354248 +227698416046185 +169532002352031 +92968134245744 +149517559324058 +43985680099807 +289273168187268 +205629406289516 +339788683657464 +552857069279086 +111443713614645 +268656598388094 +163120771192092 +176836795028417 +365969564451137 +264102185031088 +176621750120684 +87789857368406 +44011580131727 +110076343197051 +191044819761843 +137763889632064 +19969249232232 +538343307548250 +227371008079210 +448213741985118 +282365937409626 +130616023317369 +21063920007224 +407567931909653 +152113733769334 +393472217726448 +219445544314733 +480057484875121 +389974407721597 +125966454269005 +130544088896713 +407603198529392 +441807699857625 +436326928990525 +75356006229888 +536528577466159 +428356070787137 +156096908261898 +487834170176901 +349416452786083 +406512784375193 +105738228654179 +207497448721994 +325099913296528 +366337388308951 +537017139715553 +105381749352033 +558133778374616 +450295228666395 +158320495153122 +542209011672459 +428338587933182 +448622046784134 +406853760516439 +215368084290095 +160995955206377 +559248453434782 +500403305747724 +538660332690806 +229032586006599 +447706842400089 +162628429893301 +34822296196803 +333019800813619 +428082136735201 +368297974653091 +193512802164111 +515197371835334 +164004844681383 +97094186246848 +403619612731319 +84916618524082 +514741683152261 +457753779969587 +376208968853967 +459546222237652 +386077169302246 +397970756032276 +293719912263395 +182945118622091 +33625546930647 +376679618332089 +386397142014198 +204489940125625 +8795219904452 +345595466282333 +75780573365275 +296956170166311 +317765374578270 +398310417787027 +421626564985558 +385328945822793 +204029180122909 +307875714090595 +232245460918920 +176986047170149 +180554661630636 +67308658665802 +54893873016345 +268196038748933 +532518844236608 +329367608343527 +252138683571586 +449120449510918 +27738698326670 +13983935735944 +146033737022248 +399104712496723 +156238125117181 +488965890519981 +301583004804814 +433962507264064 +56008728344828 +185383177627019 +346566601120677 +535451568861154 +43397496403528 +538625915634607 +287103211822264 +368132234127611 +313465905299164 +85486250196541 +208304116319174 +248357454513261 +523582886280785 +288623967074308 +30224025552912 +533891723788371 +360330028420417 +134872562878597 +9559297478211 +327635266836286 +398819989330600 +84484929677413 +533897329520372 +3104914952666 +245910244969424 +207278732031751 +477713295031093 +347397886915889 +3049865169063 +248034161298508 +543482988097623 +336802930640543 +171451605899722 +268345589048822 +468179455540997 +55309917358300 +163554929789423 +112001590524213 +317893106038797 +110567758551150 +487059169299832 +124455211212918 +55927532105902 +243862850352781 +377619611954265 +162505546255461 +157944650684597 +433827374440009 +13938150683147 +339540273807934 +233616606087981 +135598171397804 +244405219476755 +248334971037843 +194378636513473 +137049243624952 +186664618265059 +405492133418893 +365152841033106 +520693416336589 +51874088312898 +283372001449233 +306198253467011 +298078429069709 +97882657013610 +554281447009012 +51497798152052 +458542077234834 +478581751408007 +50963249899679 +258931523592326 +319401420045940 +436150140458471 +287861028217727 +94885972152088 +498865775514998 +131984105414013 +345787307163582 +427303999460803 +259803336284009 +89071131152038 +217613434303318 +509234400374007 +334844221914285 +408523605749306 +428195904630618 +276171474201969 +462622529337527 +464521655261590 +445703877308552 +523952239955117 +473819365425647 +467740756244690 +375471810536078 +159198142226296 +103835569418825 +416228256616630 +479788387602152 +184575294679270 +198729163757102 +93424591096356 +161683802997467 +324827354238767 +135791365103790 +339626001450793 +488089432778714 +536513573186042 +340845293130311 +370062185428956 +479147192807899 +227994463715164 +230476733651356 +15819413853487 +479802477836798 +155236700472368 +85339179260404 +560678393707210 +406585598202885 +18077862739062 +479227902803790 +117490878259423 +363568265535130 +358235346250239 +264943941955639 +175888763636989 +478924847259988 +212186225080956 +480954071088510 +356551283527414 +195119688266544 +184749353677332 +415817076525266 +367630024604946 +48313806299552 +316283253257479 +312683575367014 +156092137583824 +532068084214112 +12557713612769 +396667330581979 +286774066505909 +368311932801801 +393436946106222 +516893940013151 +137465111609893 +293576412609617 +218481271716033 +453930941433786 +319057963292712 +135121027496644 +384521385608384 +32661181790301 +130513446441940 +227831973892152 +509126582639066 +531993850458942 +399673262010311 +542201561197805 +549351935865607 +316271577596847 +259350054487098 +45934067689052 +555718272128555 +171586129772553 +326630820526445 +546550206727407 +141887778232877 +6436593246535 +348402223002631 +309519277066063 +351544013058168 +465438518419456 +190343726403483 +508102729192823 +89761073846756 +3474748085888 +173626225137695 +186508546533316 +92364535027136 +537503755797182 +296555181591867 +166705348162920 +377943203764941 +360266179847650 +495213775096390 +343458818659174 +427238906741573 +104020564599942 +176517363049353 +178272158931192 +365044136392847 +510720718975753 +238214906106057 +79524286608212 +338083307348519 +86115388539350 +79699953677496 +266561334816191 +503219467307374 +58860794589452 +125209487334211 +110082174085732 +497590232903802 +261768244707147 +267354534394840 +430629753390134 +51669211249570 +298360892603993 +26259116181965 +172356736704165 +540705630226205 +538958563714406 +166352598279326 +121444284303920 +395606880931387 +389160739360553 +346650809974231 +454070257059120 +209318120060922 +178418779659926 +99518315048223 +27380857593897 +254365094804196 +507284437712054 +7501637240186 +198799523722795 +46488234366089 +66189856996193 +198541043306662 +264366911946984 +392866410669773 +532162986596332 +319070750019267 +109388589279750 +502598307656736 +547472005504979 +88854886794072 +260351816854237 +261344436383809 +205315797353549 +103438177667576 +297733014781995 +444332864694179 +140346875017825 +341120449181283 +433996176408014 +367899956046507 +348936716282502 +549102776287027 +508831191745523 +50934908091045 +18278348456311 +183195684004119 +280543494861645 +383783157322361 +14640576416811 +553116686726123 +184095667616522 +215256511918890 +269099684506841 +168803005161421 +541993306990629 +113490129454341 +378963905770828 +369350483367674 +332079563201492 +57413636342170 +252919421893280 +38685512214068 +132056625458240 +253070845074287 +163054093225280 +356562570843140 +443723402564875 +338584140681011 +182735738651933 +458882058032225 +210861513556049 +146812376534314 +326730991918754 +334600118026924 +136376727994119 +318400399229535 +537253965168935 +458084911436844 +497118610993585 +495837587086145 +441011778339205 +283613556574138 +302430358683671 +158389038382399 +500126537120592 +78901011113503 +204128975380777 +112751044355765 +549033031297593 +33169804023329 +338699189100310 +60241540696018 +377942331535491 +339759075768471 +309558912100894 +163169932867889 +444016581032985 +397845109169454 +318177346209738 +22077014356962 +134536571019926 +289459088056105 +275652871099395 +236232312287737 +218733017809536 +347799897526986 +378596980297437 +255683157686792 +245293230162244 +447536252907395 +103426326736657 +110065712085552 +183230833155726 +168260276944891 +550091480411547 +204511378972658 +88519209694842 +271907608391840 +297169808795226 +239927695780430 +310282022975545 +390904254380818 +93169404111657 +27264511626094 +460889806855420 +501837023077832 +208201803672701 +474790687382721 +373584635044287 +390303517458097 +560288730187905 +438147021589776 +172572677057740 +76691838923759 +359398003009141 +369418724213802 +490829202250000 +136104914189534 +115058573081721 +413097591723579 +359715441447513 +158251281494008 +198352666165460 +454915694626580 +217590004810455 +410230374531545 +269932897210389 +488158600871840 +4797058533505 +174753177068002 +14078537777935 +236664727847658 +178454978147975 +37144616033182 +278627049775917 +73592660121004 +339995682345543 +36905289831259 +66564397624172 +339045697581973 +417416064710420 +375934219573830 +510969732771840 +207620036303195 +12232844485417 +377075556611247 +73281381038767 +377489254947494 +438689690355265 +94817584122246 +448400072230203 +334926048682366 +440317266658843 +360229195400376 +182046805654113 +501004772096101 +506063772595095 +393116744873814 +395904744558856 +49966045887304 +290504586495447 +37066580893520 +395609758146481 +328061570421296 +163680327013240 +426515949260629 +528829479435042 +555139087426522 +417462264303741 +553081279472032 +120969891666364 +439075666665298 +367483403315957 +313853653651165 +534451923644440 +132294672782664 +530476967360095 +19593923495970 +371592610667794 +323582799419717 +327910453032211 +276902376250184 +98966547171047 +64304877529199 +137539143224188 +162584534258725 +104104017079681 +134623514065079 +291716022590717 +147934731852322 +558433277592688 +543053822091633 +454507873285228 +196304727001197 +288074331591088 +74341787504932 +305712803619945 +545714004669938 +415893032612968 +449064781181619 +426272564928614 +23620257099762 +275929068957709 +177231576698800 +99935125361655 +506108316122165 +219452962388874 +345040208823901 +311802257277315 +528620069180157 +114166664361174 +253436739641321 +297052559690018 +298280755577503 +540082545220284 +556901799585076 +306745841310452 +372677668172356 +98794120536254 +364891647027574 +205923657091257 +247093651571484 +320272197768303 +511071114060376 +265767836532506 +408406384525438 +234268577303015 +471237789848208 +248646121116558 +134613356046387 +231865412117052 +72125182668010 +378483422499889 +162050693048599 +364883802955022 +4417410248141 +255963568139651 +265795625042443 +556622275599224 +349359577395534 +146049113314521 +488328875907940 +499166600644121 +479627813126698 +49009196586886 +266635250494789 +220971228817244 +517939250684532 +190233687301275 +355097637454812 +404907644756370 +391672329645903 +416861491088656 +208040452368798 +301788049538750 +536655179657152 +74004391283427 +325273279859245 +199256479743786 +533770441202950 +465985021935675 +368385413863731 +415177167375237 +356263829801817 +515449902654880 +71486330667887 +55599301588462 +152189959143208 +242906371395616 +542380585044989 +187815611533327 +128978745493994 +11147770773581 diff --git a/src/day-5/part-1.zig b/src/day-5/part-1.zig new file mode 100644 index 0000000..20647d5 --- /dev/null +++ b/src/day-5/part-1.zig @@ -0,0 +1,43 @@ +const std = @import("std"); + +const range = struct { low: u64, high: u64 }; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + const path = "input.txt"; + const fp = try std.fs.cwd().openFile(path, .{.mode = std.fs.File.OpenMode.read_only}); + defer fp.close(); + var reader = fp.reader(); + + var list = std.ArrayList(range).init(alloc); + defer list.deinit(); + + var buffer: [256]u8 = undefined; + + var fresh: u64 = 0; + + while(try reader.readUntilDelimiterOrEof(&buffer, '\n')) |line| { + if(line.len == 0) continue; + + if(std.mem.indexOf(u8, line, "-")) |ind| { + const low: u64 = try std.fmt.parseInt(u64, line[0 .. ind], 10); + const high: u64 = try std.fmt.parseInt(u64, line[ind + 1 ..], 10); + + try list.append(.{.high = high, .low = low}); + } else { + const n: u64 = try std.fmt.parseInt(u64, line, 10); + + for(list.items) |r| { + if(r.low <= n and n <= r.high){ + fresh += 1; + break; + } + } + } + } + + std.debug.print("{d}\n", .{fresh}); +} diff --git a/src/day-5/part-2.zig b/src/day-5/part-2.zig new file mode 100644 index 0000000..a1ff825 --- /dev/null +++ b/src/day-5/part-2.zig @@ -0,0 +1,58 @@ +const std = @import("std"); + +const val = struct { n: u64, open: bool }; + +pub fn asc_val(_: void, a: val, b:val) bool { + return a.n < b.n; +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + const path = "input.txt"; + const fp = try std.fs.cwd().openFile(path, .{.mode = std.fs.File.OpenMode.read_only}); + defer fp.close(); + var reader = fp.reader(); + + var list = std.ArrayList(val).init(alloc); + defer list.deinit(); + + var buffer: [256]u8 = undefined; + + while(try reader.readUntilDelimiterOrEof(&buffer, '\n')) |line| { + + if(line.len == 0) break; + + if(std.mem.indexOf(u8, line, "-")) |ind| { + const low: u64 = try std.fmt.parseInt(u64, line[0 .. ind], 10); + const high: u64 = try std.fmt.parseInt(u64, line[ind + 1 ..], 10); + + //no clue why this is required, since both are inclusive it should be fine, maybe better sorting would work here but im not certain + if(low == high){ + continue; + } + + try list.append(.{.n = high, .open = false}); + try list.append(.{.n = low, .open = true}); + } + } + + std.mem.sort(val, list.items, {}, asc_val); + + var count: i64 = 0; + var last_val: u64 = 0; + var fresh: u64 = 0; + for(list.items) |v| { + if(v.open){ + count += 1; + if(count == 1) last_val = v.n; + } else { + count -= 1; + if(count == 0) fresh += v.n - last_val + 1; + } + } + + std.debug.print("{d}\n", .{fresh}); +} |
