From 1154c1489d1c3258d2d4d5126a10459f129658ca Mon Sep 17 00:00:00 2001 From: April Eaton Date: Wed, 3 Dec 2025 19:59:54 +0100 Subject: [PATCH] Giving of on Day 3 Part 2 (for now at least) --- day_3_part_2/.github/workflows/test.yml | 23 +++ day_3_part_2/.gitignore | 4 + day_3_part_2/02.txt | 200 ++++++++++++++++++++++ day_3_part_2/README.md | 24 +++ day_3_part_2/gleam.toml | 20 +++ day_3_part_2/manifest.toml | 14 ++ day_3_part_2/src/day_3_part_2.gleam | 169 ++++++++++++++++++ day_3_part_2/test/day_3_part_2_test.gleam | 140 +++++++++++++++ 8 files changed, 594 insertions(+) create mode 100644 day_3_part_2/.github/workflows/test.yml create mode 100644 day_3_part_2/.gitignore create mode 100644 day_3_part_2/02.txt create mode 100644 day_3_part_2/README.md create mode 100644 day_3_part_2/gleam.toml create mode 100644 day_3_part_2/manifest.toml create mode 100644 day_3_part_2/src/day_3_part_2.gleam create mode 100644 day_3_part_2/test/day_3_part_2_test.gleam diff --git a/day_3_part_2/.github/workflows/test.yml b/day_3_part_2/.github/workflows/test.yml new file mode 100644 index 0000000..f6e42b5 --- /dev/null +++ b/day_3_part_2/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: erlef/setup-beam@v1 + with: + otp-version: "28" + gleam-version: "1.13.0" + rebar3-version: "3" + # elixir-version: "1" + - run: gleam deps download + - run: gleam test + - run: gleam format --check src test diff --git a/day_3_part_2/.gitignore b/day_3_part_2/.gitignore new file mode 100644 index 0000000..599be4e --- /dev/null +++ b/day_3_part_2/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/day_3_part_2/02.txt b/day_3_part_2/02.txt new file mode 100644 index 0000000..c8ab41c --- /dev/null +++ b/day_3_part_2/02.txt @@ -0,0 +1,200 @@ +3243342952212532333831142251933144323222213232122222323333226253325232222324622352337352222123223314 +3353443623344444442533323673445444565758516456445436445644454644454566344444733444643563445546444734 +4544644463574842545428466934446366933968639333234477948644946619455756539631276986475444645465433321 +5122327242335622223222555422232732251315442341373221283825224222546743266216244223535433521222225915 +4323232342322325345234234332631432121332323337324232343442422224344324423293233233323233452312432433 +4872493454583666466657687369628966455649545813595473734576687664495785675633876663486248448865446343 +7262312253414524453322432322224323221342622433127321333272223322233246332223233524372325213234733225 +3543323353424523331332322442334221216342522452442322233372321523131243622323832333233525723222123233 +4343232333332233334242633436632423374433334531212123372334433214333433333443642323125374533383364343 +2412145285934415562531335123751222243462522222134222241232536263343432825362223362321631232522132242 +2864811433432334472224242423433454414633234228246252548637584358332233712425432736526542479564748392 +1331233334322132333333438332332333633342332313332624343433633323833323333332313434323243322342323333 +4222222122231422313212223214142212252222222122223213121325212223312223252232132124211232221422112722 +3597464736467757425647554555736547566357467448657553773674456566766467545675764775544554546664596663 +4334222222222232222433333323322221342121222334212632422343432231212283211233342343313323254233223233 +5435643441664733426365743225253246533243626385234676562563339366555542654312256634296433546672424453 +4255235342132644234212221762524355311424353644342225453232353325233225423233652335212235422223222512 +2212222322523222132222232222222122322221222222212212522222112222221222121223131232223322223223221221 +9344745395656528759446474762443744278376528888828482673645384685666773577547695964287348363336794598 +3224423232444325634115622541322256543424366243244245474255324342245426444663344365422512455743446623 +3223454436253472352424144242543693124234523332434343422414143443334381444474445623442944334434424563 +3433744345944353536244226444237577546434343424657485537436546472457337466637234554565863456743837654 +5426333333323422243322331622321232531412323224327255625621243323242232267422232865363224333123625323 +3622373343246355422245134456645162233454222244422332242645536552542645325235423334532222253136422232 +3134332222332123434323323321123222333122222232332233333112435323322333223123212332322331332223222342 +1242362435348113325228512323126435245255332152242733226259322255221324224364532215262252535127225622 +1121382322522243343154224122222252221114422222221242422222123321224421123222122512234214224722322513 +2313223323452232443335535333233474413345342242333533333142443321663321323333133423142332323234222322 +5454396555445776447347564845545354544955545533775546555445563546545555644945455555455516575554474545 +4658453654546429446265443454655643665684533326474388744367574863484463443545354466844464723945548437 +4544447462844657154551314548755445222775423333322755737574477382635442333553554254554675554554485835 +8464357246935655445644345343634255424446534444454146444573445424432521452444145434755445615435844454 +2232323333342236213211342521322233223311326232142332622212222223233233432632323321323126121222233322 +5223333343153323312332473223323144432323453134414243122153922232542334244322144223245523134264424222 +3333129524232222232341222232322227122242222232222252332222242321224321222243412421236121222333223222 +6754556372372554522223323343313425324242222334453433343347235232522514533445427257295422225927728643 +4433332143428342723444225122628424422224255264122444534282531322432254343442325365464522454674432523 +2494312179242323172344832733132253651522434323244423229423228512264623394451222345233121522664522222 +6323362463156443426754634637423234464363525462626325446454535333734546664444374741546346257444353362 +2242211324423247231122222521312253232222232215412255322242222511132222324232222235221222134312142612 +3422233334433433332324373343223343444343233333433414343442333542442433313343433322338213443333434334 +2222121122123252522212322222222252222222224211322142212222322111211223221322221222224112124212721231 +6646665675266656566534442566663653466664355324635566564464436636663667544265655554436642456534353655 +2224122246246152255426222222613234136435252325241112452521273222344212312231212444123221212225222412 +3672222262533221222222442515412522242222232223152412122242344222225622263222214224163312726322422222 +2423672593736712554339283535832492567579324354338268236223433393778593339723332394976743369464556353 +1244343452133334322235334241213242538232324143125232142451222322231323328332325453423233331112233232 +3122122212222222332222222323421221211222222213212222223212211223233232322212233132232215222221212221 +5865825462652524644434554377636576582336724652613587463252545575373434578425565867245342444757436361 +1512122422532252321122212221223221522212322132332322324322233232233233222333526222223123231222231223 +2433333221336335626417722221334773353133434213523636623323362333332533333222553322122131123223356134 +7535512233234222333232353222133565335313335233334445114732332232212232534543133342214733133315243133 +4324224122452323663233332342653627233546363232432656322554344213425432143433233433334132441642335233 +4343323484235574214315233273462235423532554443514736255554343911242442244565644343474623723459375423 +3222923113352342232152362425312293222442231223234232222322223218922221225221221132212228246252226372 +2334832222443323252224132142333422121323323433222224232826412133242632245221522121662323223222222241 +2222331242222512212222221221222121222422222222222212222211221314242222222112423241332262127342252262 +2415262622552255346264636433113423231323633743156212256384534441621143725246332523454323223424542956 +3225313333535542223237541543332343543154547323545554453372164513335425445364333635334157235444346463 +1217216222221322233311242311242212222224212621442212233212247222132422222212223132222222233222125214 +2243622334444222242152422424229222421231352121214244312222234412425422241322222522322232432341212222 +2343654345533333332343454334232423334361147223736227254223443322237311633334355234243442232345433434 +2425533328234421351492127693223222212321842334252545223233423223228211422372534422646272324325316244 +3333393233322333323322233322113332334522342323332363311444224235353323242422235235225343232321335344 +2333323334353213133922344322332323121234324333333333333323324423333222213333321233225332333332344323 +3344423431313423333133232232423241352344433243234324241313313421135213433422313412322231332324442343 +2374223331325122223233122231222332423422128311233322242222213222152523233222166222323663312223222122 +3224213234834524512322323224232732262133522443242731223222322211331122222332332213122212214224821214 +3446325684543686527771818352554478526563585272267631871518232252667243861482371354346615272735662829 +2443447233351223453231122524343242524275344732433355633421324333422564344243452435415353443244344247 +4313325255323233431122455355521342914244554444244153344443923434353353472122254435344414341563435433 +2443313413233344323344353344334233232534343423143633433436433342443443332334333243422334243342433442 +2233332122216333212225143333333352453333233333324632341325336233122363333333223342233333332322145312 +3333232346233623233333644333343633334532433512323333323332443546231133632333363271133425321133323133 +4444374343434434244544345444347521344543434443244444244443434344245353334344444734638349383534444244 +5846694454745176466483363334858365854345446448443263376255414651534687843624444463157453115474447655 +7355221344424533425345345445463336453535455456725234244443235425544233441433353335313124344346343355 +6755476626574656663763466674465672657556895357156544567547566563776547659517754439367166345594473585 +4424224242341222233362122432426122223272246522623233133533224553434322356431232243352753522543246424 +2343487322374225873675273229553263314234254347245137662221243823345342582524572925737766423355426228 +3295232254123543554248323362512386442313333341253325434132525633532435424323534325313333532222323336 +5712225545123233125642322342224454222224645125523323615263312227513524572513425221254532342253325537 +6463546269253474666364577551333675352336643746264343454544333548672562815537375526516854525142757661 +4358424667685444465547558466556446924444845445795664647754445335458554544554444443644553645555335486 +3211122433412434124343132222241222412414141313112233431211422134111232142222232233211211111223256789 +4432253836322534474773467627624474454444437825447427442345431554472263475446664473167662516372434437 +2222253423222222422222234142325285225222121223112244232245227221222211212123322212221212222213322222 +1334333132333525463365522322332333534333332322242233323221335233432323273123226323254434222352225332 +2222121222251213222122121222215322222224412321222252223222224372224232222112222242224224321522124222 +3323423843355435332354434436475345472842233647637434363412533332847334444463553443746334637657443334 +4151154413234535222543154221222523545441214542112132122151232313544115214432311332121424534543226789 +3233721352132231326323332122232332132411542334221322232212321242222727213823323323323633323322257522 +3623425223321433353533333227233463343532235383232234342324279231233232745132115251233331335343363332 +2225631325232222331132223242631222523232612243122542212722344132222222332422212227242262221252223222 +4245141234538445442315532243236354226222255243655442435523233433352326533525453516455252344556254453 +5223524523221422122255224152222222212322243233123224121322224213222229222212232143622322335224122222 +3324124145315152534142323441353223433345442472354334225433265322353255433535242512334535354453514445 +2224222322122222322212323222322212212213227223222432223211213222223223423112343222226232222212222233 +2223222221231124214322222343432622212221222211112212321221211212322245221242423322222322422252424342 +1112422113343622632322123123323623223337212334133125433312323621252227121252123122243212142222125332 +4435425334341425648453435333333444537394454335433453424242343344442464954254531632344332653464444341 +5853344266446552325362233536633133324333663253323384423942661424233613342346284563345363435343336642 +7654476357344448773453835623571233542247352543541453461334334383474455342384364345434336474668333333 +2433123335256417183232642625574474447745672724772623432235622635482473464611573746254422244215253238 +4544634574533356639436356296494443535453466653766435464894666549444674547258624265765575545764585492 +4244433443243396375483342354444342414344393493444644494248334444467644443344335454424449863345358643 +7588396364755748744334565764413567477586673523446885544538764844444886886732424786567827746476678773 +2225312224332111224221122422214223223822215422331442322222251426321242455126342324232114342422122212 +6477647577377357764529147427567376355446425664556575745365496575654237767763255674765557673712665156 +4843646486774657873455344743645364473375874449457543534453475454547383326246636344446593767673645493 +5546325544544433354343245322488535443555433235546748452344584865331745234375525554353266339434534552 +6569458535458577718836655584635645955357445443775756694475565345447777935755495567485453566645575555 +2225422722412224751512442372233222233243323223152442211311233261233242363312321232231622342223433535 +3122232221222342241213233244322232241243222212233222312222223213143222222232422323312422213223312324 +2743399462563317465862234644563444357852568235448645465844363747876465434346598649558353668555535745 +7527743356761115771527465546477427767227443322635624473412672215476744364313312165216412751634172189 +3343363343332443733342433433234433373488616233283313232333333323133333333273622233337373342353333343 +1228229312222432643222422422221235426324334324334142853321241234432224222244422242353243472322332915 +1322113222232341222134524232222222222324312121222122222212222257222233222532222222242541222232222232 +5233325442237122332324223222227235244421233222354223432218234425848222435432121122221245153274216721 +2434833868385432466424354653334522635836132479474618334173421163238335244392425446458879434656528522 +5323543533433434222243473383352535335333435455833554365332333535343543333333535351443335352454533533 +3222212324232122353322224245252242524222323322222222133334321124222112214232222325312523359222225322 +1117435224226142737923212423122422123342212132222323223223422343263345346224341423212442613223332212 +3643434343444424424232134434323524164335345424444431424267424237314364364244442538244423334175644244 +3633246453443332124424643374334334434464432444443143227423422644343234354437464443413344383474334244 +3556558432556333478555334846432431464251566468456845586852657365943267553746367157543532735852716546 +5522234323355224252322251342252254323324113252125232545543543243612413224342333334254552331423423324 +6554954444464539478638739656534448665476564455954532175579756359346743166689833568488374854856975532 +2482467639573357674464357445667845613676464635723653645754635553427468728347557324676675665737444667 +6655795545471353366652252545335442533235643333383565653233325353243543654643636552263524364456357535 +4354645554445533446445461443434525453234425244566532243857434453523424543448451544333422533231243353 +2243242222222343722242423327313412135246224243322282322123422212112722224223532347122223413134831224 +3323231224426343212222333333335232252123322132231232242222332232212221333232123152232123314153333112 +2812524241232222676666461464416426163822386448712326523472247225327142784222685254258253124724167521 +3362321132334542342232222532254124232316234223112221222344223123243222534413232442232323234324322242 +5353535535447365524531553464573355535424453557445345735255454545354451355444551535675555538354545454 +2282212222222324224221111212212211223121522222112221222322332223222322162322222122332232138221222232 +1124222422311342132113414222222243222215112312212922145252322352522224924222322312222622342224212222 +5426554244452442246645554544444324442446242424444446556626464343413552344241355345444554586354545454 +4424424243233243432362434444322335335333345433124344413133433439415333443443433424444312233334424234 +4335342342233331534523235422433247334322343333423331424313312344423334234333342533334354332312143434 +3754968746847553256575435394354443553443364634585435285772344552492454544143352364822369715655463365 +2342333313632313122233333234342243323343232233432334232314233323332123414223321322332373334234142323 +2523652332232424522232123542452222324233112122234252524121352512323212546122422532232143523452222233 +2531623222132337525364243355336536334636323326332353433243354216463332222262223833152312236522156925 +3423233733463143323394255333752433434231523233333365333124766633332343542633343333473333334344863333 +3533243333324255553533532235433738259315537534353523543375523431332323225245314442135533333333332234 +4454333334534433343434343211335344434433334123331343374444373333433331332444223323437336444332433242 +4373233633333339233223323272333333333433333333333333333442334333733433332333332323363233333234333332 +2222123222242232222424522222241222221222223531223213512222323212113213321411142142222422112223272212 +5655254624457577376632776658635557236275677343167556857353336457553432473666327443433172937694454398 +3343221435333642524355413734133233313342224324353137243452333233242615333233723853654432384334528282 +1344534125256233212223254232835324614232335393313326645432695582522333233361222233262555533622513542 +4242231523342252582426776253242522424783223422829264632632453333123525327122231522635622324763422235 +2154243564444445214243555446463452464444122326426465644665531443463333437352153354439243317134434232 +2432352843523214655335423222632333323324642232623564325364522384332325331346623734238553633613623621 +4325344333255553555655585424426214465344546643537355357413263546158614525343645543554135245528444456 +3443463525644443433432263354212321411534838628522233231293434222445424144432129941722452225244222358 +3243144245522344524327457532455445453434354528475332437287445363245364333534839344462625376638332372 +5233233222353233823313353344422333463335235732153333421422351336333331353333733221334233423333332313 +6213252222222221224221622422222822322223222121221123132232324113452622141211922222222823212428232222 +2222322422312215212122731312714321424224233433226324249222632422232422613335722242282342253517321111 +3336567632421356326255824343244453745352333635566558752523475477551261474619454553575455534444456346 +3831446655231642232122235224422224133328222221296324271222422222242212422225424213221222123312222223 +2232322222532222222223324245222222321326222122223432322222223233212222213222221324522321233223121113 +1346614453165762863222372324236351261545653423656124732345375213355256445216252434672432644124666225 +3553644535434344356533545446544453464441625533525455253564566444435447355645444544445467466536443586 +6536234454684563574844652254253844836254414564565325437364242524573523452663635634254547647783433436 +2223234225221222142222341222232221221363224322223222321222223232221332522223151222132222222132212322 +2122224222222225324321312327253232312332732133115323212252223532331132223332362122222126214222422242 +5867565339867736556338655683877876636557523534565335368324354664348584578276663383333466324782634574 +4445664333343526338548644363344445543164755477543844841933331545578846374637763457632573546917243328 +4331548313525353235364214443342454544433635315263322635353333146433444555526344424458522453644546254 +4334553144154334644441123233435112331363343674333443343443262533322614533224443245336333433444626234 +4372442222453314951342645554474521342254344364622443432327226444652523522953252434342443844324667322 +6353347443433286737337268366534254682445656137364644647586634333453749273338534342348335356654433547 +4335332333423332233343435333133363336212233433343333333633333333333233514333333323223323333333222334 +2212222422332114212222223222222222223154223241221212414231322124232232222422228233222252222322422522 +4533534544432344494224523154431321334323333324119535142526323453432434314425542553343332346233425235 +3267436143336187224525162365256224726618232311235336168223372253456226563256922513335363966488538263 +2121212234212212122332212221715123221223222231222623223332212222222222212221222221262432213235222113 +5932234455324444344255155254323545235325242551325233423552355468555552334223554554552423453414434445 +7345367668464745346554634726444484826838565466435838266665525535335135557465734545563587534375365547 +7634482867626734874643773879945743338382678256379395597655473653154775765465744835765537386552275787 +6446474755248776658565839565789474845643547289685584434428855894754888895585465497298644785284345799 +7742313144324434444544524232322234532434443243244342645322863444132245314622434444444273242343422443 +3114762242244746273232326244162235336434126254468223274442434344272223322535292574312324516444624261 +3447435563254434725735445544537355335542253446435754544433345443343364442344343747334273644456634234 +5655366666657858634756466747694666666677575666656568497665665355564266666766465564666656635436456666 +7212641526136323543321423642642222552271266553333322345139223352334313333275532952332433362374424233 +8529622449734236374645747753463382252992252612798415287246649464222537876876273562225352827946942515 +2442233343534331242544424334331541342324233431122254344342342324441332242244424453342243423323434411 +2624532412325434646616626132334555542433422511263244614316563115152666246122151346535115446335464789 +2232434343243332443333432413434332214742313331354433342445374324525424243313315522375433447444344325 +4563722636652515521631327222524422246555832453222644256353865523543523264633445537355355611452542336 +4722554565532634217569382635464746646645973363542643674126563646256336644654634254535655642147676684 +6243633632433518434434466732224545423224435333421633544243313334242363432423335432343524533135632335 +3215335632333332312222322242322321333262232523332122343334323342525332334253421262212364422132322124 +5555645555556232356365365454554363546565564543655535456554645645455463465445435553644446443555555746 diff --git a/day_3_part_2/README.md b/day_3_part_2/README.md new file mode 100644 index 0000000..38dfb48 --- /dev/null +++ b/day_3_part_2/README.md @@ -0,0 +1,24 @@ +# day_3_part_2 + +[![Package Version](https://img.shields.io/hexpm/v/day_3_part_2)](https://hex.pm/packages/day_3_part_2) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/day_3_part_2/) + +```sh +gleam add day_3_part_2@1 +``` +```gleam +import day_3_part_2 + +pub fn main() -> Nil { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at . + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +``` diff --git a/day_3_part_2/gleam.toml b/day_3_part_2/gleam.toml new file mode 100644 index 0000000..cfa5a97 --- /dev/null +++ b/day_3_part_2/gleam.toml @@ -0,0 +1,20 @@ +name = "day_3_part_2" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.44.0 and < 2.0.0" +simplifile = ">= 2.3.1 and < 3.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/day_3_part_2/manifest.toml b/day_3_part_2/manifest.toml new file mode 100644 index 0000000..6139ca9 --- /dev/null +++ b/day_3_part_2/manifest.toml @@ -0,0 +1,14 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" }, + { name = "gleam_stdlib", version = "0.67.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6368313DB35963DC02F677A513BB0D95D58A34ED0A9436C8116820BF94BE3511" }, + { name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" }, + { name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +simplifile = { version = ">= 2.3.1 and < 3.0.0" } diff --git a/day_3_part_2/src/day_3_part_2.gleam b/day_3_part_2/src/day_3_part_2.gleam new file mode 100644 index 0000000..7603a29 --- /dev/null +++ b/day_3_part_2/src/day_3_part_2.gleam @@ -0,0 +1,169 @@ +import gleam/int +import gleam/io +import gleam/list +import gleam/order +import gleam/pair +import gleam/result +import gleam/string +import simplifile + +pub type Bank { + Bank(List(Digit)) +} + +pub type Digit { + Digit(Int) +} + +pub fn bank_parse(s: String) -> Bank { + string.trim(s) + |> int.parse + |> result.map(digits(_, 10)) + |> result.flatten + |> result.unwrap([]) + |> list.map(Digit) + |> Bank +} + +pub fn file_to_banks(f: String) -> List(Bank) { + simplifile.read(f) + |> result.unwrap("") + |> string.trim + |> string.split("\n") + |> list.map(bank_parse) +} + +fn digits(x: Int, base: Int) -> Result(List(Int), Nil) { + case base < 2 { + True -> Error(Nil) + False -> Ok(digits_loop(x, base, [])) + } +} + +fn digits_loop(x: Int, base: Int, acc: List(Int)) -> List(Int) { + case int.absolute_value(x) < base { + True -> [x, ..acc] + False -> digits_loop(x / base, base, [x % base, ..acc]) + } +} + +fn digit_outof(d: Digit) -> Int { + let Digit(d) = d + d +} + +pub fn undigits(numbers: List(Int), base: Int) -> Result(Int, Nil) { + case base < 2 { + True -> Error(Nil) + False -> undigits_loop(numbers, base, 0) + } +} + +fn undigits_loop(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) { + case numbers { + [] -> Ok(acc) + [digit, ..] if digit >= base -> Error(Nil) + [digit, ..rest] -> undigits_loop(rest, base, acc * base + digit) + } +} + +pub fn int_list_to_bank(l: List(Int)) -> Bank { + Bank(list.map(l, Digit)) +} + +pub fn prepend_bank_with_max_joltage( + new_digit: Digit, + curr: #(Bank, List(Digit)), + length: Int, +) -> #(Bank, List(Digit)) { + let #(old_bank, old_max) = curr + case new_digit, old_bank, old_max, length { + _, Bank([]), [_, ..], _ -> panic as "old_bank should be longer than old_max" + Digit(n), Bank(b), max, length -> + case list.length(max), length { + m, l if m > l -> + panic as "length of max should never exceed target lenght" + m, l if m < l -> #( + Bank(list.append([Digit(n)], b)), + list.append([Digit(n)], max), + ) + m, l if m == l -> { + case n, max { + n, [] -> #(Bank(list.append([Digit(n)], b)), [Digit(n)]) + n, [Digit(h), ..t] if n >= h -> { + let #(_, inner) = + prepend_bank_with_max_joltage( + Digit(h), + #( + Bank(list.drop(b, 1)), + t, + // |> list.map(digit_outof) + // |> drop_first_min + // |> list.map(Digit), + ), + length, + ) + #( + Bank(list.append([Digit(n)], b)), + list.prepend( + inner + |> list.map(digit_outof) + |> drop_first_min + |> list.map(Digit), + Digit(n), + ), + ) + } + n, m -> { + #(Bank(list.append([Digit(n)], b)), m) + } + } + } + _, _ -> panic as "Kris, where the fuck are we?" + } + |> pair.map_second(fn(l) { + list.map(l, digit_outof) + // |> echo + |> list.map(Digit) + }) + } +} + +pub fn drop_first_min(l: List(Int)) -> List(Int) { + let min = list.max(l, int.compare |> order.reverse) |> result.unwrap(-1) + let #(left, right) = + l + |> list.split_while(fn(n) { n > min }) + |> pair.map_second(fn(s) { + case s { + [] -> [] + [_] -> [] + [_, ..t] -> t + } + }) + list.append(left, right) +} + +pub fn max_joltage_from_bank(b: Bank) { + let Bank(b) = b + let #(_, max) = + b + |> list.fold_right(#(Bank([]), []), fn(p, d) { + prepend_bank_with_max_joltage(d, p, 12) + }) + max + |> list.map(digit_outof) + |> undigits(10) + |> result.unwrap(0) +} + +pub fn file_to_max_joltage(f: String) { + file_to_banks(f) + |> list.map(max_joltage_from_bank) + |> echo + |> list.fold(0, int.add) +} + +pub fn main() -> Nil { + file_to_max_joltage("02.txt") |> int.to_string |> io.println +} diff --git a/day_3_part_2/test/day_3_part_2_test.gleam b/day_3_part_2/test/day_3_part_2_test.gleam new file mode 100644 index 0000000..0f38926 --- /dev/null +++ b/day_3_part_2/test/day_3_part_2_test.gleam @@ -0,0 +1,140 @@ +import day_3_part_2.{Bank, Digit} +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} + +pub fn prepend_bank_base_test() { + assert day_3_part_2.prepend_bank_with_max_joltage( + Digit(1), + #(Bank([]), []), + 2, + ) + == #(Bank([Digit(1)]), [Digit(1)]) +} + +pub fn ignored_prepend_test() { + day_3_part_2.prepend_bank_with_max_joltage( + Digit(1), + #(day_3_part_2.int_list_to_bank([3, 4, 1, 1]), [ + Digit(3), + Digit(4), + Digit(1), + ]), + 3, + ) + == #(day_3_part_2.int_list_to_bank([1, 3, 4, 1, 1]), [ + Digit(3), + Digit(4), + Digit(1), + ]) +} + +pub fn prepend_anway_test() { + assert day_3_part_2.prepend_bank_with_max_joltage( + Digit(1), + #(day_3_part_2.int_list_to_bank([3, 4, 1, 1]), [Digit(3), Digit(4)]), + 3, + ) + == #(day_3_part_2.int_list_to_bank([1, 3, 4, 1, 1]), [ + Digit(1), + Digit(3), + Digit(4), + ]) +} + +pub fn dont_prepend_simple_test() { + assert day_3_part_2.prepend_bank_with_max_joltage( + Digit(1), + #(day_3_part_2.int_list_to_bank([3, 4, 1, 1]), [ + Digit(3), + Digit(4), + Digit(1), + ]), + 3, + ) + == #(day_3_part_2.int_list_to_bank([1, 3, 4, 1, 1]), [ + Digit(3), + Digit(4), + Digit(1), + ]) +} + +pub fn do_prepend_simple_test() { + assert day_3_part_2.prepend_bank_with_max_joltage( + Digit(5), + #( + day_3_part_2.int_list_to_bank([ + 4, + 3, + 2, + 1, + ]), + [Digit(4), Digit(3)], + ), + 2, + ) + == #( + day_3_part_2.int_list_to_bank([ + 5, + 4, + 3, + 2, + 1, + ]), + [Digit(5), Digit(4)], + ) +} + +pub fn calibration_line_1_test() { + assert { + day_3_part_2.int_list_to_bank([ + 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, + ]) + |> day_3_part_2.max_joltage_from_bank + } + == 987_654_321_111 +} + +pub fn calibration_line_2_test() { + assert { + day_3_part_2.int_list_to_bank([ + 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, + ]) + |> day_3_part_2.max_joltage_from_bank + } + == 811_111_111_119 +} + +pub fn calibration_line_3_test() { + assert { + day_3_part_2.int_list_to_bank([ + 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8, + ]) + |> day_3_part_2.max_joltage_from_bank + } + == 434_234_234_278 +} + +pub fn calibration_line_4_test() { + assert { + day_3_part_2.int_list_to_bank([ + 8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1, + ]) + |> day_3_part_2.max_joltage_from_bank + } + == 888_911_112_111 +} + +pub fn drop_at_front_test() { + assert day_3_part_2.drop_first_min([1, 2, 3]) == [2, 3] +} + +pub fn drop_with_one_at_front_test() { + assert day_3_part_2.drop_first_min([1, 2, 1, 3]) == [2, 1, 3] +} + +pub fn drop_at_end_test() { + assert day_3_part_2.drop_first_min([3, 2, 1]) == [3, 2] +}