diff --git a/README.md b/README.md index c78c3b0..1ef434a 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,10 @@ [Advent of Code](https://adventofcode.com/) problems of 2025. -| Day | Name | Part 1 | Part 2 | -| :-: | :-------------------- | :----: | :----: | -| 1 | Secret Entrance | ⭐ | ⭐ | -| 2 | Gift Shop | ⭐ | ⭐ | -| 3 | Lobby | ⭐ | ⭐ | -| 4 | Printing Department | ⭐ | ⭐ | +| Day | Name | Part 1 | Part 2 | +| :-: | :------------------ | :----: | :----: | +| 1 | Secret Entrance | ⭐ | ⭐ | +| 2 | Gift Shop | ⭐ | ⭐ | +| 3 | Lobby | ⭐ | ⭐ | +| 4 | Printing Department | ⭐ | ⭐ | +| 5 | Cafeteria | ⭐ | ⭐ | diff --git a/src/__tests__/day_5_test.ts b/src/__tests__/day_5_test.ts new file mode 100644 index 0000000..994fddb --- /dev/null +++ b/src/__tests__/day_5_test.ts @@ -0,0 +1,24 @@ +import { assertEquals } from "@std/assert"; +import { + count_all_possible_fresh_ingredients, + count_fresh_ingredients, + read_ingredient_database, +} from "../exercises/day_5.ts"; + +Deno.test("Day 5 - A", async () => { + const ingredient_db = await read_ingredient_database( + "src/exercises/assets/day_5_test_input.txt", + ); + const total_fresh = count_fresh_ingredients(ingredient_db); + + assertEquals(total_fresh, 3); +}); + +Deno.test("Day 5 - B", async () => { + const ingredient_db = await read_ingredient_database( + "src/exercises/assets/day_5_test_input.txt", + ); + const total_fresh = count_all_possible_fresh_ingredients(ingredient_db); + + assertEquals(total_fresh, 14); +}); diff --git a/src/exercises/assets/day_5_input.txt b/src/exercises/assets/day_5_input.txt new file mode 100644 index 0000000..1b7c2ab --- /dev/null +++ b/src/exercises/assets/day_5_input.txt @@ -0,0 +1,1184 @@ +292632986393425-296797126337251 +428261559408337-431275643240865 +197704206528056-198822557342819 +36791726875734-37049023408764 +134880223152389-139959748438608 +31870818340663-32138457068292 +443613579514078-447616030778273 +284142407577672-288117756872436 +553644508725126-559998363324659 +535273273294907-535756795069114 +193948250247764-195204091532044 +124354007920888-129810455318314 +51346766240284-51346766240284 +152837720997071-152837720997071 +33961436686344-34764747175521 +164681190955623-164916224872844 +353663210884032-357442176375491 +341218873508053-341406840338900 +33961436686344-34629093370919 +376266673126870-378025882372936 +167911217169913-168285872076706 +162740718598213-163392112176192 +101878332723318-109108668288254 +338990290267429-339410057428860 +198431047768818-199993956632102 +163071173976629-163328354368783 +345933383593711-350217774136653 +331774672867226-332449216414068 +193093427090710-194342053349953 +35374244459683-35880676911120 +63156601749796-67528476585644 +497776394698920-500464503891744 +37504755840416-38229907451575 +466842879180492-471618129296962 +234474297850610-240566873073788 +195758600636724-197137768602862 +503814556020214-505821902593496 +162256401863965-163071173976629 +505021445200023-506743913764214 +192123912851914-193608898258273 +314566936557400-318497911480586 +340423918583467-341218873508053 +141465993918460-149546266636235 +120980455300188-127525715793804 +394707953165745-400275053442281 +454387138843073-459356529724506 +33804228161568-33961436686344 +31331722417755-31471378480041 +475974294933757-478308178695615 +312741096143775-321267781719757 +164916224872844-165259773793177 +368054134990978-368054134990978 +432967082833628-436989263209868 +540424371768649-541277414595612 +61873080838793-69008369119090 +365495808508592-368054134990977 +553644508725126-553644508725126 +30618031161062-30761732994001 +114901534037127-118177524484042 +523263704114453-523263704114453 +196707545989242-198197663103045 +16587687786244-18488086585289 +385880631900168-387581342553739 +405135254951466-407338199650512 +412684750272800-421543388235613 +152837720997072-159181314211949 +284142407577671-284142407577671 +466842879180492-471618129296962 +215808352705165-219335046847038 +172109572403785-175343246928158 +506456561514065-507940556066050 +535756795069114-536086307758053 +517855386606767-522278672291912 +536775937809364-536864913941927 +337162709485926-337897888627205 +551595722084447-551595722084447 +82051514181396-89565496145181 +484565435754559-490202530854270 +94821984030156-99301410182453 +38229907451575-38887786677780 +1119138043168-4626546804343 +533961006639307-534081832299558 +162740718598213-163071173976629 +22382441701737-28961980444237 +541951584228598-542222619314133 +495288389464531-500464503891744 +502925701815278-504809795712771 +535415443345631-535756795069114 +305612281703354-309325838972704 +221874935879302-227308977780282 +538031726356481-538490133858732 +213089188327742-217339599625162 +10166496972041-12187556196658 +93105210134208-94821984030154 +406502700166885-408783062569030 +38047636129588-38887786677780 +161201656849936-161370867399648 +202929024869395-210322185899024 +181951435171362-184935937383834 +71075903298033-79149963547079 +545667226602897-551595722084447 +336188005508720-336306310910527 +48795586612626-48795586612626 +336306310910527-336791779754379 +181951435171362-189140232230793 +31331722417755-32138457068292 +303742181522797-309325838972704 +104670623249103-109108668288254 +533610921652560-534375261747852 +15522714286866-17095325541538 +509538573854799-511325879863473 +175343246928159-180541943712029 +17737007080792-19948084891063 +412684750272800-421543388235613 +490202530854271-490202530854271 +387581342553739-387581342553739 +191273714775775-192536163672143 +272568392667775-278098486652781 +445190842692779-450137550110441 +139959748438608-139959748438608 +535273273294907-535756795069114 +334648713052356-335036826409409 +355097026792688-361028330056557 +403731130867273-406174952965216 +248654206480872-248654206480872 +432967082833628-441453048873245 +33464879521849-33961436686344 +537153987697573-537397889472756 +338452620965483-338990290267429 +538192329299673-538959874425113 +240566873073788-240566873073788 +508478141168933-510347533438410 +510675128879732-512463171530746 +162256401863965-162569062792411 +535844704713420-536342893811955 +1119138043168-7757060220352 +474414590148921-482000020725624 +13027591302185-14738955625531 +337897888627205-338452620965483 +31870818340663-32293439984609 +541460880852978-541951584228598 +296797126337252-300331182874920 +36356103955642-36622149880189 +376266673126870-376266673126870 +202929024869395-202929024869395 +402550038147381-404874224112864 +251961472896766-260126791852199 +422738452133858-428261559408335 +541460880852978-542222619314133 +335844235762767-336188005508720 +335844235762767-336188005508720 +409751586246867-411518560172036 +221874935879302-227308977780282 +111018488233235-114901534037126 +536864913941927-537397889472756 +71075903298033-79149963547079 +255139382232577-260126791852199 +43477871861253-48795586612625 +14040167860452-16209815818872 +340719019358950-341218873508053 +338678682664859-339410057428860 +51346766240284-59512955763984 +523263704114454-531859919748109 +199368823774716-200786576848679 +533961006639307-534081832299558 +267281485307788-270923814487039 +408233936313227-410464218619469 +11398648996840-13355422512516 +143992287432675-147904842577015 +30349216670359-31039845978942 +394707953165745-398039482948233 +262423311410368-267281485307786 +242298274531866-248654206480871 +324620443918620-325802052121348 +515166413666087-517855386606766 +507213012595659-509194014807236 +194927851874118-196194101934255 +335036826409409-335844235762767 +325802052121350-331260610503009 +539412226876141-540261725553043 +540261725553043-540959920997014 +169171240481620-169569459473920 +22382441701736-22382441701736 + +92087202605588 +314304587960778 +19184152482180 +331502336245673 +104093299503920 +224082459481089 +485738859165292 +211637984020453 +32119275602637 +132343053161310 +354088630585339 +85349314948684 +347319985049518 +365139163023317 +329992624187176 +110459376101716 +427615358688850 +462236874062754 +362894126638329 +414073679324579 +63524995993582 +508263413715544 +415270817754522 +440437312367136 +423738573842345 +85900634529596 +309159795757844 +241559182902302 +395770784912912 +245664411565902 +332814854208613 +396854183189818 +237834944869227 +80363946249150 +525286478968622 +268653843932564 +454376845516233 +382773748181482 +429561732742293 +453599790177593 +484351344106928 +436777029807945 +21220827793354 +101449657425511 +442079917949332 +356763362659168 +551086436109153 +492286585079483 +99888483917901 +415417254795075 +277331235238751 +359351742184870 +469205936118582 +133625021551402 +247984995153793 +432142410158309 +343574315286686 +294555342531085 +491697942703570 +216161460231727 +467380543981301 +23242031614280 +190749142198415 +188466316632472 +265350408785042 +87817564702305 +380247719946071 +326080625028144 +430482611717099 +556616030495876 +237822134207332 +157595147282531 +514227682332005 +545236777014992 +76738990835436 +197840928373561 +50996194420681 +385872918566725 +88501332803255 +418611727109113 +319977247163493 +220861943536731 +465354919982963 +303996485418883 +418326870518912 +384027079710472 +26735201812909 +113596843004542 +110889459748173 +105164212472926 +85317246364344 +384701572747744 +287473617574727 +529090480900407 +47200397949989 +270979549843408 +340000110376755 +19097525001101 +68908009539041 +154526812087898 +18582860676910 +368555067157891 +195649704642991 +407609335966886 +387828460440284 +418722993644305 +302009353313482 +302202151167652 +407403126058349 +561739905877285 +239132904316404 +434963008241865 +165239090562576 +316310832455677 +509828957797850 +119723484535574 +407176944072200 +550294083464109 +373505879458296 +499185478958172 +110703587922976 +484773844717058 +482214670608215 +389386282773493 +164551765512059 +381072344683492 +181961021095663 +131104610192966 +503965339077519 +322200124214352 +167937387687640 +332122837758680 +140422177347127 +561235891883410 +437217358605530 +195787858374615 +283537776360610 +89764782214109 +468321375938282 +373214651587687 +166184622167974 +83917168046378 +20313242971310 +215666398955563 +441690466036209 +50652726593945 +230993095380226 +497147906055170 +546969791248470 +420695811312514 +257515932612315 +513257621236331 +240106814002346 +510075918833166 +524852013039924 +526775200904738 +519624483403758 +223761622690744 +101651505676349 +539627120919767 +547585041778101 +282418775119661 +349567812333360 +407416312083594 +389382875987596 +449775052583825 +462269724364781 +245181182227310 +352557092105366 +162037837057230 +39277612518220 +372123038973986 +560059021626757 +474434651178880 +520014727780422 +129982736291703 +75914999671899 +368324912559291 +39020410937285 +159656690374434 +422612087321627 +380996889935590 +147878475547866 +162137648017152 +311284133757248 +357969798109545 +447815017049437 +401741006849531 +151644001790815 +30095055234299 +208538695827949 +510895470816433 +312400246366093 +301405267505119 +331019058703264 +357502799613449 +339940053664157 +231165413358540 +370356402389119 +120135103313243 +391068656310844 +327477392908607 +353383847547489 +526650309633944 +194614143414267 +383813635603549 +114055311182237 +537468111250763 +135056162081761 +529915425795498 +514977110640081 +102977330738554 +469579563235375 +341835097727715 +64944828998 +246851255321996 +495985309672920 +265029687225192 +481636957703147 +92373194934560 +273515492480683 +77745113398403 +490939701798359 +423078169976750 +433750583142594 +297453390723420 +522783784822790 +302327458955656 +383718020544938 +391444308162091 +479898974870750 +546230243895697 +37056236318860 +112019819191769 +57712513070127 +109511590106242 +146507814933653 +494616249451656 +308383747738702 +359904300826850 +287378077233578 +6172140957734 +149520361443562 +265500546302145 +115511819902196 +412110199835061 +163231098925448 +269594801048479 +151610815951386 +543281669836763 +467809146648217 +293648394240318 +552062088423048 +89559896344215 +16049058011667 +231156947680616 +172216478546411 +510814022528230 +477352293877442 +259860005374953 +411540597276729 +167539887750700 +186545877319408 +178424395332899 +484269115895338 +454181775933241 +183136408328160 +436553806699823 +523471676205439 +285105004938339 +28360480672656 +558952748496587 +395190855699589 +359395446419019 +465014492906433 +240386258451319 +383128263796594 +151607133518193 +259765870126720 +517111139718660 +241388818450047 +335968734715616 +449644412791521 +434316882720417 +254101656297569 +443967186843335 +409356289703246 +314311714133277 +547834596017039 +70019272576721 +147405025992788 +155208352109445 +460292050816284 +51216042136209 +84564622686308 +179604282000565 +332673783159031 +73807054444408 +177312430816558 +73054151644037 +440327958840639 +538994421186793 +450037841418034 +535673900909944 +46278852226227 +272215342410039 +401720174776498 +49251792907898 +240288061154258 +387057565259348 +53567177153702 +406873738103572 +289959372407058 +501220180899025 +530636465683771 +271994150730473 +144609441132991 +248767208181910 +70244374950197 +37977074018663 +282632338489174 +242520991602617 +1680537827370 +497741699764034 +280283596139058 +158992252715012 +166142667687353 +363505970161442 +1607313149348 +397474718918923 +268645206404599 +445218680058012 +188781169800509 +159299439770861 +7109181438604 +267089832623547 +55177617593301 +142205883405073 +525149334889271 +493792320574301 +2044160209045 +310170894312367 +442452683240305 +464592138722664 +351126581327917 +109785799207552 +186583570411556 +442200079681129 +95763360109064 +183436585154596 +542230751986425 +17620630014462 +181377154612454 +77563012798054 +186199891715285 +432990201920759 +14507078018591 +375070042447056 +486940614186547 +282779685858095 +236045910946970 +293819540706158 +352002324656604 +74225158198040 +318569617247888 +318004373789863 +194413545660299 +521273446443625 +245044318125940 +362567182873778 +48630389359230 +435815950127807 +280518460841507 +441464416339138 +451886842967319 +341121899615244 +203859106666511 +65234539349331 +332671095065631 +212064002922726 +172359437531489 +143019800077826 +218292491964135 +328641166306928 +195388590724138 +527704628893105 +398742231799366 +5909385086703 +527155063266347 +490538349605614 +48091726220001 +214997912062829 +105750986412025 +551187981121781 +103011164144994 +308238757659431 +23343226512782 +509524947029450 +513201649349791 +305568909594743 +285500964171161 +154319699784123 +70842093737511 +370154000423346 +464823070990679 +355455943955618 +26135898766336 +377271777807137 +276373574495296 +335170274211410 +81146405249173 +195290238752400 +12632992359450 +556861239844878 +267569139964738 +282361888454670 +252640933977840 +337942890650693 +561948840171980 +385120510348707 +194079008538029 +482233155292880 +197292520695195 +118030645180671 +418805416469368 +198152307928135 +30154450998033 +516954383944221 +142235204882427 +184838567473110 +471514354688844 +79283651005769 +194822094140713 +559266871192195 +436425838945553 +114900272863505 +180517406506818 +459710011430774 +153495856740555 +36940570669061 +522287527125383 +219362621437411 +342419859251117 +97370282497500 +147190558709336 +462662596186933 +134866726946694 +366291746435274 +36590230734848 +443437313609979 +492085081090508 +179289245423106 +167040096648621 +126199522687755 +153819196471739 +75917543016901 +413702262604494 +448870730386892 +546908819921584 +237301938692035 +313409537322824 +91630953706427 +34152349327403 +236933122768901 +106926015113441 +211510670382825 +227004624353389 +534348260511251 +374935497032342 +329870390706405 +137003054370745 +554424962882869 +517637441415623 +517129332354145 +36479370807154 +11412970042485 +298313859991214 +530804152250388 +244007899093986 +112539672241169 +211967298060232 +187259420328893 +211389236272504 +180862666339131 +164333074277285 +232331231527106 +285045141131643 +350319058874059 +346779173378284 +507825576403693 +39144182915498 +503016866249202 +108746474668055 +119566114134092 +558168295798775 +111062052280517 +88646598973346 +91651167262762 +466183515160291 +475234696403021 +263562735802241 +139914108602199 +427502311709511 +296120338157657 +156192674941356 +72397848936492 +37122918205577 +195992912785040 +287442160865918 +105528260068603 +338112282662213 +194143998734702 +198136725938802 +381572337332915 +278606484459214 +405327148516414 +187067150991891 +124480075442423 +334361258358544 +897945958015 +488681800792890 +68898317891262 +58515549598295 +404992052379413 +220806981185565 +53298424958283 +105454341143438 +528868572259363 +290164509099960 +554334278854468 +254295614729060 +94287964628908 +274930911597735 +145941121100752 +248262077590442 +18109358020489 +285639406925353 +404656174427197 +318494182259430 +180999793206850 +318661765064392 +2452409809518 +292140878212078 +348789347066016 +408276471893967 +40234086887599 +104411194068376 +539649173810798 +81285585441361 +179111192935551 +101861849358800 +2708461908082 +428006998937160 +340914585692574 +275870481052657 +411531052163369 +20797169387126 +113483961437497 +367416784797103 +430484912398958 +559273776512239 +463067403400924 +7007356641451 +133394461354069 +52612282268894 +462739500910693 +138036796601839 +364938748282762 +357555606807901 +104470494789481 +53607951033119 +531582624083042 +473041680309668 +407080446404712 +359172729578883 +518862954514257 +82970729314058 +246399406510854 +457227465903868 +396487191162005 +140657586027133 +561046112868449 +222978119032864 +33573218412444 +481899142781403 +82746427781830 +70704503530082 +139649144668233 +165710737191051 +543226767299163 +351498438934707 +364788963111640 +418037278581377 +210515320373749 +147138532920942 +191413707388909 +422594343698682 +289999411488816 +104045419023797 +152534158463314 +26218546284248 +243400393324195 +269029042289492 +1614817295971 +485972768307967 +352048559156164 +493076113404885 +21976094863475 +238143924961902 +380572698200260 +292723390071899 +330143618819286 +389022164429288 +417615396689504 +277120389935329 +225419854204369 +129200294553413 +138966791993873 +516359490131010 +175840759116317 +96763486398661 +335635206085828 +170899573013730 +435513978005294 +311805378921126 +486765936610945 +387912415334272 +308666833768648 +508275999117963 +561002265643482 +446395888804328 +58605552424614 +329557626602964 +402191138432182 +544346000847270 +191224409658281 +472944439110646 +290063770213660 +340002841744740 +350903966924543 +467942025087011 +463419217493132 +159886057083926 +370180510512711 +481653208135178 +89612653630783 +443252647432464 +524355752398887 +552203486833614 +432194883764973 +55660448898473 +183812848459132 +533960907795679 +428317634446286 +10545941862882 +299881028221599 +174576183561228 +381347918489305 +474472210030779 +109781554539171 +302729982655640 +508756137857512 +380199164099378 +263633923249057 +295784940153454 +401012985132416 +237507092856913 +501009174088138 +370112752547713 +549799986381674 +556080288698576 +71443322853295 +104422442291799 +82351629172390 +390691184360728 +467637437534363 +48699170476870 +312583271841245 +107924443828093 +296847224911096 +336454130959476 +18016065581891 +217985377383818 +464105859268724 +507730329786786 +244607291048651 +116206980379522 +109967730791940 +217010796575232 +554781177049395 +154126952390113 +64148707806816 +362767127132217 +24017240914228 +310177408823401 +145841147682323 +55122045732165 +497522469352785 +304574596654540 +65637205683509 +243819933973046 +280514778513730 +345124627060051 +69971109969520 +35972397464609 +2606578083256 +96905623691253 +53649412368789 +50257106763560 +508681661313420 +521299572433575 +69087794814513 +356950291014809 +97486423492084 +143611917606805 +550778502122002 +130625724237576 +481378054182896 +304429177988692 +315925357397435 +20997663921599 +529034336767111 +364741766149455 +354604267086284 +182504243827378 +280304921008875 +523992439152045 +532178308567407 +214272563315188 +551825372419777 +308267145361863 +439280164223557 +226281159121884 +274811698273819 +263315275155098 +343476413178948 +51990040901728 +79564652890393 +386874074512802 +388413674125809 +450282514220174 +421339646720238 +98911232635632 +341823051830056 +344328927964201 +427096465823314 +39735187977251 +403144800406545 +439019855579835 +478370477102901 +90746458123721 +526960194244021 +52218289836896 +352780620318077 +227179425802508 +378843642100240 +17992013929194 +453643454395002 +238540580033141 +454083950803150 +546296873572195 +285719208159890 +120771138182184 +302807476799478 +75826661100987 +64920301312326 +339023569638381 +468260297023608 +453784608425953 +41067362242511 +511531311625159 +42922202900255 +502385546973712 +310035917639464 +149045071782801 +208432353094034 +2676571098324 +502812449659724 +292548379940943 +139740737291912 +110617716309581 +38491219621742 +104205128108795 +17511114882702 +16108706755321 +58247140741700 +255703800816489 +177819437083722 +247991503364222 +58355668821132 +294041417943719 +421673246825036 +343742777908083 +226371653021309 +371616962089538 +488852057546708 +322608288393148 +143621191080911 +103356408138737 +246932693911930 +312464679939863 +5870143289138 +536604902602674 +459737019381280 +121336929915144 +272567079260272 +72656871416249 +323560285855966 +257283776975152 +518559540752806 +147471505676435 +154923789275417 +488917939465684 +539273199515785 +529085222609024 +385380942644357 +317863543818213 +42312450148301 +488923815662625 +341113586781087 +544222093015262 +89070057969398 +196582835429095 +332150614516281 +256993803722426 +120944078119848 +260020848647969 +462247828096280 +308634206265860 +74145922778467 +451495781739852 +268904927634079 +68165966511428 +125949534045630 +339024645517601 +502842296037164 +268802011068032 +380779403324614 +374204867989082 +275085871796046 +365181815714158 +124991991253008 +415054807839655 +212933409532990 +476883573910278 +47474817060988 +102917585635199 +208203923360684 +192197796619473 +418065883333740 +182684459643533 +333974046228984 +503000646655745 +347220504094141 +177696212460438 +439689261383659 +78768857499573 +424107585795185 +62984809546910 +299305802346626 +140750883101193 +110050966604738 +420821066673305 +482516681492935 +538159373825053 +497014261816045 +59358880141626 +219550690476244 +560843832257630 +109401488486970 +461356672739124 +105054792754165 +178834616740527 +347138672851864 +300587730529520 +550456487576831 +502411341311674 +198362759997789 +185681657646524 +387178021747110 +533282651281351 +243052044490785 +141034569475200 +482132687961109 +430012151621052 +193049687618973 +79872811310943 +431897917048119 +175728399286636 +408343824273074 +518121382940352 +392300592867092 +275346933233852 +261323885974650 +282201634137640 +190813973983417 +187588800120760 +64089660995821 +532062657122554 +231027454551274 +43479991314957 +64272865971621 +475616499183610 +511463341249645 +256243371720019 +225493857674792 +300464371845332 +146171188957249 +437474637517726 +405093610060688 +39762399991668 +484465992966217 +172951079412778 +54214101190186 +495740285885991 +105566974561119 +543454149788679 +542761788533155 +265342860799513 +204105702513062 +511791409328114 +535581829258276 +426412349561626 +159605861604461 +331055861487609 +470858774927007 +246562334269056 +491988471532313 +467518225193240 +247610405336641 +14182532157150 +316664146726007 +119780861955914 +528578424012076 +152221086396750 +160922922632145 +491389040838008 +442049073803775 +163398274731646 +303644068132662 +402380606521402 +403853248034378 +295126902791003 +94922219801261 +184070627975504 +554454422169273 +467776746065473 +358068143858318 +173070349687526 +181550621798064 +158446646904490 +39961946476111 +42972913381029 +37467956621902 +342302577213533 +18391171465196 +60076191886652 +389842218623483 +65712263730840 +55125033792036 +286255560735976 +223803310736568 +283530057342450 +55095007735629 +227467960153103 +61218326037688 +453600085055882 +167698539118234 +354188077364111 +452476447174995 +492328453909513 +87872041407173 +75225064997352 +494477628502907 +224320485274659 +425556889058227 +377427590296274 +182045024206434 +65056771174943 +487731730562988 +88357776799451 +125687395918270 +405944996981925 +407882345696277 +58288769572307 +516514365921069 +535926663625953 +101251255005030 +118378054265735 +491995931437337 +470426966503610 +376799711396304 +488503193668827 +307227347589326 +478297996447873 +61178191953772 +373601511803982 +551770120686526 +74347784377561 +235148063703812 diff --git a/src/exercises/assets/day_5_test_input.txt b/src/exercises/assets/day_5_test_input.txt new file mode 100644 index 0000000..2e9078d --- /dev/null +++ b/src/exercises/assets/day_5_test_input.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/src/exercises/day_5.ts b/src/exercises/day_5.ts new file mode 100644 index 0000000..ef6f97a --- /dev/null +++ b/src/exercises/day_5.ts @@ -0,0 +1,118 @@ +interface IngredientDB { + fresh_ranges: { init: number; end: number }[]; + ingredient_list: number[]; +} + +export default async function Cafeteria() { + const ingredient_db = await read_ingredient_database( + "src/exercises/assets/day_5_input.txt", + ); + console.log(">> Total fresh: ", count_fresh_ingredients(ingredient_db)); + console.log( + ">> Total available fresh: ", + count_all_possible_fresh_ingredients(ingredient_db), + ); + // console.log( + // ">> Total available fresh brute: ", + // count_all_possible_fresh_ingredients_force(ingredient_db), + // ); +} + +// NOTE: brute force by memory +export function count_all_possible_fresh_ingredients_force( + db: IngredientDB, +): number { + const total_ingredients: Set = new Set(); + db.fresh_ranges.forEach((range) => { + for (let i = range.init; i <= range.end; i++) { + total_ingredients.add(i); + } + }); + return total_ingredients.size; +} + +export function count_all_possible_fresh_ingredients(db: IngredientDB): number { + let total_ingredients: number = 0; + const fresh_ranges_clean = merge_fresh_ranges(db.fresh_ranges); + + fresh_ranges_clean.forEach((range) => { + total_ingredients += range.end - range.init + 1; + }); + return total_ingredients; +} + +function merge_fresh_ranges( + fresh_ranges: IngredientDB["fresh_ranges"], +): IngredientDB["fresh_ranges"] { + const ranges: Array = [ + ...fresh_ranges, + ]; + ranges.forEach((_) => { + ranges.forEach((range, i) => { + for ( + let next_range_index = 0; + next_range_index < ranges.length; + next_range_index++ + ) { + const next_range = ranges[next_range_index]; + if (!range || !next_range || next_range_index === i) { + continue; + } + + // Check overlap + if ( + (next_range.init >= range.init && next_range.init <= range.end) || + (next_range.end >= range.init && next_range.end <= range.end) || + (next_range.init <= range.init && next_range.end >= range.end) + ) { + // merge + range.init = Math.min(next_range.init, range.init); + range.end = Math.max(next_range.end, range.end); + ranges[next_range_index] = undefined; + } + } + }); + }); + + return ranges.filter((range) => range !== undefined); +} + +export function count_fresh_ingredients(db: IngredientDB): number { + let total_fresh: number = 0; + db.ingredient_list.forEach((ingredient) => { + db.fresh_ranges.every((range) => { + if (range.init <= ingredient && range.end >= ingredient) { + total_fresh += 1; + return false; + } + return true; + }); + }); + + return total_fresh; +} + +export async function read_ingredient_database( + path: string, +): Promise { + const ingredient_db: IngredientDB = { fresh_ranges: [], ingredient_list: [] }; + + const input_txt = await Deno.readTextFile(path); + const input_divided = input_txt.split("\n\n"); + + for (const range of input_divided[0].split("\n")) { + const range_divided = range.split("-"); + ingredient_db.fresh_ranges.push({ + init: parseInt(range_divided[0]), + end: parseInt(range_divided[1]), + }); + } + + for (const ingredient of input_divided[1].split("\n")) { + if (ingredient?.length) { + ingredient_db.ingredient_list.push(parseInt(ingredient)); + } + } + + return ingredient_db; +}