diff --git a/day3/CHANGELOG.md b/day3/CHANGELOG.md new file mode 100644 index 0000000..4c17aef --- /dev/null +++ b/day3/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for day3 + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/day3/LICENSE b/day3/LICENSE new file mode 100644 index 0000000..87cbd05 --- /dev/null +++ b/day3/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2026 radumaco + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/day3/app/Main.hs b/day3/app/Main.hs new file mode 100644 index 0000000..2e83943 --- /dev/null +++ b/day3/app/Main.hs @@ -0,0 +1,45 @@ +module Main (main) where + +import System.Environment +import Data.Char (digitToInt) + +main :: IO () +main = do + args <- getArgs + input <- readFile $ head args + let banks = lines input + let values = map (getDigits 12) banks + let numberValues = map digitsToNumber values + print numberValues + print $ sum numberValues + +digitsToNumber :: [Int] -> Int +digitsToNumber [] = 0 +digitsToNumber [x] = x +digitsToNumber (x:xs) = x * 10 ^ (length xs) + digitsToNumber xs +-- +-- highestDigits :: (Int, Int) -> [Char] -> (Int, Int) +-- highestDigits (d1, d2) [] = (d1, d2) +-- highestDigits (d1, d2) [h] = let digit = digitToInt h in (d1, if digit > d2 then digit else d2) +-- highestDigits (d1, d2) (h:t) +-- | digitToInt h > d1 = highestDigits (digitToInt h, 0) t +-- | digitToInt h > d2 = highestDigits (d1, digitToInt h) t +-- | otherwise = highestDigits (d1, d2) t +-- + +getDigits :: Int -> [Char] -> [Int] +getDigits n str = getDigitsInternal [] n $ map digitToInt str + +getDigitsInternal :: [Int] -> Int -> [Int] -> [Int] +getDigitsInternal acc 0 _ = acc +getDigitsInternal acc n lst = let nxt = highestDigitBeforeN (0, 0) (1, length lst - n + 1) lst in + getDigitsInternal (acc ++ [fst nxt]) (n - 1) (drop (snd nxt) lst) + + +highestDigitBeforeN :: (Int, Int) -> (Int, Int) -> [Int] -> (Int, Int) +highestDigitBeforeN acc _ [] = acc +highestDigitBeforeN (maxDigit, maxPos) (pos, n) (x:xs) + | pos <= n = if x > maxDigit + then highestDigitBeforeN (x, pos) (pos + 1, n) xs + else highestDigitBeforeN (maxDigit, maxPos) (pos + 1, n) xs + | otherwise = (maxDigit, maxPos) diff --git a/day3/app/input1.txt b/day3/app/input1.txt new file mode 100644 index 0000000..bfe09cc --- /dev/null +++ b/day3/app/input1.txt @@ -0,0 +1,200 @@ +4942223224223134312221222433336324234433314222333723222642441142184541322622221421243432273241422334 +4353554354133444633336743474444343443423455322434443443733352437452343433334433532333444533333344543 +6494235656557395762237355445345462754746129544525363443552889473535435433434766333663533585486544578 +4575437413355455333663534375455245465427458234151534536655743664553344545864333436556593466693743454 +3333223332222323333233323322334332333223782633323333232353333333383143463332233623333137134223562532 +3233214222227326232141314262243323243318437262233343312222332333243314323233332233231138344212222413 +4322512724132232224424222213235322527453312212422232122252122223612232222222211222221512252123722443 +6663342662463463428161854326454827243228634722353126355123657466364286544722233461473231447262766431 +2322312443334215423341142422644412433353362122224363242329222224222352422222321422462421582414224122 +3312232612432728242712333422233232222312394332423442613231243249335432242433332434512433194142243132 +3122632263153322262222252222662253253259222562211212232432555522454236241422123212212223254322215552 +6866242533424343343246514351436567632345348595253633543334552265414327342632443334413223244523114443 +4222246263444442233444363332323342313745633259434423463433642234332252263212123353333313331612563234 +3233232235322232133352122353221443223241132522654223333932232132551322323632222352232227221223153531 +8444447346444842754874478452553683435486734244433244454544842543434436683424234444435584472954443232 +7113431772572633751667657216244623772551222115664154571164617664753675772562646765442652241621323589 +8467462841736658844223542625678266837542222464326215912221544733351254312684424525247541764517493643 +4956345859977964655747659599766663569494975587463877555757868462282967447587999968557265784564748956 +3256326435533467226337367273223926532882867174555873646539441455182593213218623246436762422426443523 +4814644567415343385352751692333443546565348417354424443446433364463368833454344396675565826333336637 +2223223242112422222222342222222121322132222212532122353222122213222222222222212212221522242222221122 +3322224415221212324244223251421222422222274222222223242235313233321228123242442222441431214232333224 +2323322233234332252223122222322121224233232242112231233233232222222223152223213222232215122336422326 +4333523134351236232633233335433342342333333333525323334332334133235232444234322423453533344423233223 +2322332412222622314121222323671235533233233231652725272614761215335221322622213242222123122422977513 +2365152914522418424242445651292623223524126743732283446125222353243322373621142553754223256247439173 +4232243235313442123523226222224561233273223222223122742254222452114574122142446242412432242444315241 +6435251444334532243334523443545366243223333243753434413432424224232425334423343244532626322556235322 +1323222423122232232122232222413322221725243111322214232222315221313232522232222426233358321212522442 +4132532123332422212272222212333127231442534222322512332225212222313223142283242222322213223251123521 +4342341414241244472423254532212352424424452273353446444424253534443333332255425434243444347544523254 +1223234433244322722123322224225221425633233124122122532123123523225231324152331523242215252122454134 +3599554833745933644614898657666948936754563296574554346795837542883363355343654544544356542483617999 +3432223322222221243232196882521224121215222242232523221222236361412133127128423222434351912425871242 +3223242336345666346333352563336343463333644632543365834353541332325239333653243333723335632358765873 +3222732612292236112322312253223332232234312521162122233142213312333122123333124213642123312124335233 +3342355342234323222563363523121242232331434323225243222423423332123323352434313433234442234321422333 +3136332323213333224132332411524322363534321534236334213323263235333333336222533323232233533323321122 +3332223333423331332322223223234333342232133323323332422213332324432313453322333322221422233325234323 +3222242252343222223461433353224556254553344326362465237524624324323355252333354412323461342414254214 +3425231791232292243643163322633213222445224223543195222364229933222227322242512323122314327322233176 +1222521221222221322326123222722212345221252323222212222122212211122122232222882212212212254273222237 +3333434344441332211423341322224245362424544341333432122253232322242321423234436234131243245341242533 +8773367538234633355857568498895934885793367477872856685722887759764765569498434346383839335751783364 +4324234312592433323343832443643252422647346356245641414142634224913323423512475753347723352372242645 +3628224362452232344531422261253365232354212335246321322222932232243632484215322323823212221842251356 +6455435432535352343343345553224552539374331314436674545385353545279432543373154436336372343561255489 +1222226234221223252422232622222232222221212232212227222272272422222214421271332324342213223222224423 +6574844427476484883465697645445558548368657446474426567563667555648484467467537545563673656335367554 +2523526312223333215322125221222242326222322275232223222222565565252443223161232332632125222232222224 +2222243222323336132221231332223322233222242322222222231223512132132122212224312124222243223222262311 +7252254225663538948335523352256435222262443573455632224445554341334233359344245454552234442333433232 +7323472232536122525324353425251265164522421233445344126353254445439433394324242647232234334352221254 +4372424343244244342534443443344432234443344333445831434334735322334332242344414232444333243444434447 +2123212222222272122322232331231222112822221322242121221221311322242222228212222212321222222222221622 +2341554222584575666614356123154273356432223244433643543373667153252423375562115245362596512425525621 +9677948566937576223387838478967696534888395693876558829635596238699945467586796577947525742296678546 +4422337743543636262523249236315463411232472434433323323872223399342466515743873457321923233324334552 +4322223812334323133222332122421212224332235222262232339442111224293222236322241523322122323322922572 +6456447733177243544436545355213325244563444245253724415355823333585432453532334425342575433375635365 +1212228232323322242222223221222222223222122121212121226122123127223222223212413232222162121212522242 +2333333333343334323435344423343332423432233433234343333382443533444333533343452334343431344333543323 +2322414243624222422273533243343242256123346327525433222364722522243342222146222235464822442425242532 +2522221422235521443522812322243261232445452862229822124236152232232227252121221244142432544226321253 +2222112242232222222231213122222433213212323222252223322121213222222133222332422222232121223111242352 +2354624665225524536152535324342213274334352326652535225225324255556713332754453427125645534226612464 +4323223334342423212332112212122132222112234244124363232225152454413128613232222111325223222332552118 +2232131333332413366323323233328364437482132311313352233236323333123343322233343284322353266533343324 +3671121743224223229232456331114583421251355342232322324414223422355627257262624722232343223325433472 +3374222222456445422563532344243442442321133241142546215613422326354324251432323232312462612264215364 +2332122222122423331632242532323223213234534522324332312123211241345111323224222232223223334224523323 +2884338766656952395673622777657487357677165566325555825333465838367572982698847776762976777582857554 +2144533142553353237346221314236344222114325223324342344313333212333456142353434132724334412145458413 +1212221226222452222222222223223322222311124422322222233312222323523222123133221126322211211322234212 +6332232432422468422234244447222121213228645528644237183536425443157244135441342315132632633247135622 +5344232333124521221433134132222722313122333322323421423432223222422233432332234252422332243232225432 +7734476576267589774637653765654553848574759553535672826365552767737428663465755785753555567583366257 +4212324222232222232325434232251843124342337721112344423346242444422222331222236214222121344122424247 +2224132218282264224222223332222123223242222222212412124228522241223221292223223523223542542222221122 +4223243223233324234262244422393122222722222222142322435332222444431322341233322322423232322424222324 +2472346212825254252112222453342122213325126454463143432432131633244424524511233674324227176222217332 +5555535946545445445566554455528544445554954545354353335565335354645565554755546457554354545557545444 +7264129377547747266643646413665145632372333447371422257444494672643462362372786744226392212852434672 +5754732212398743335532333562563143526575383135455332313172358253522532652333333443224243322425425356 +3333223123333333233213232331333313232214233232433252323333333333523231322333333313243233324363424334 +3146452535342336142353424536134543334231333252432513341132433353241224223434525332443343322352214565 +3777747776874575778767385666338255582757868445786775748767554669595755785756465676396837666745768666 +3234223413221322242322122323211324432323232232133222333232334231323312222332232233332331323332333332 +1123221324514261241245328232532242712222132225221321229232221225222212224872212222222222243223124222 +5351743332544443445343552534454535144333646553423456246229455334753547943637538464544537343464343654 +2256652355556544452236637343235435542245255585524524435458515242632747465265334341452524544373525664 +7636855664543564542364255645336535556524434466754565645543544455645652254414335654533575535667626464 +3437324334332332253242123234233446226132232364322122231223232213323432252322235634343222116126326222 +1513132221123233332323332223422432213134433232612412233223332222422312222212322432211422373454612337 +8344434758885387547546594664665883837466374758685845757563543654832558365558455945843446534344349777 +4236756877333559563345628253763963549254523745655836635753588329637639561765348442533323632563923635 +5325417433441844424343423344464353433824453443744423354655433243225543143334143345323444234432344424 +4344341421232226264443443443143234524433342334342434434624244444323334442243323443134521223434452334 +4568954558461468465752446724444486475643685859563452558764458338848524655965884655664755395547656745 +3222311322222312121222222222232222722121222222122222132231222221221721533422222122242238622122131222 +4167464636737436643832443268653385223834282253333511167453566972445436553562723746762555346647615554 +2224433315344334443333234235323433643233343444333334433422455341333444337343333333354443434542244663 +4121322342232322232122262322312222253336232345232335222231232222212223223322223222322425223222346422 +4666341446372463465666633654464341436314647743244626662365233444735233464534436433626434654464245562 +4324484116243444123443444364643224334354426342384321354423446461653334344444633332324413432244543444 +4549659661263732333345391335457745364312685424364334225465427463633535236333344433453584336678644863 +5352224223622234344313522132313123656235123354411552392232352332366243342221122222332653256233453245 +2223232214145221323322223243322462334325312112422241312421212732222242222222222232244241612134523331 +1122612222223222223222214322321222332222212221211232222222127148322222144242421262131222523112321235 +4253443432255325334432314431412323332353524423332313222431434322353335554233244312331444624225244323 +2214213224333244343235222336222346344332234344343522223232243323324235322233213332133442624131331315 +5223635694344352843525469335335653364645943443366358358459433496853342133425437239464565747493477338 +2322333851212552324352932111225331134532232526333227122237155321443622522232322246211335215224252223 +9549477888567857851249829217837676598557853828234267286115457436898613533659678228637789216487763913 +1222332122432122334332322433434343233533313222223222321235221332341233343342214324222132323223422232 +7429547227239483322857422455767132695536457267963547554434491764459245134473274226254347473467284726 +4623312322223423343324221243333314333352423145423342332343334227322233222232232312222233632321432242 +2221331322821322124322227222332233322121222823622372219335332323222225243333131233253523312333132322 +6134211522422236252888223151222314258332146213238641136142281362255525113725265546633336222251374442 +2222221271122413714222144222225222122531422512461212212312211222312233332222522213224423221371552812 +5524357523537235632333353433336235446623232326483552246363236356463354437363122634352353343243633435 +2423212222462231231322322222222122122232343223224122222522656423245232322233252225422242323222228423 +5222344422543245342524243643156321323122321222252223223144332244331324243221411242222222271114245324 +6135225333345324665356564343335643146552534336454321344633423343244732232556447636646553653669265463 +3362532334372543373983448392425333336245841444333355532344221843565153335313325323332435542846445867 +3725365745614557545455235562474555435253328245555323165648534234553445526523336385585444476124655425 +3233273624228682233433633362342542421324212244431643228223433224343537423253322287233224321224124333 +2445443486745462452384734774646733765766467364449283666735236672654851355467463674564652473269525275 +2222241421312123562422268224322233223211421264132242222222324222272322221211132222332375225224341223 +4543134453537551554175542443462523243383734365255554314254432334533435333242335755634354242333624433 +6665772724263363246346461423359345334777482343522256772521424423266252443135564773437462975665443384 +4682698688357774856925593673865933866863532399633468639144498577635512524484595697395734533733443244 +3626443444444242433434433645443634224432144464435434942434334442446337241443434435444432643643344424 +3241344266334326533514665432232434433454693326467642235426633365663465474366162254335236453458458265 +1122452433322224522132322333623323233122352312322322122251222232343314223323133332113232223133422242 +6644444424433427534631343334445252224553434344245573433555357434333241244532353142443351463333541532 +1533242352127443864424344623363634232232433533232433131226242233323323331241421332343213543733452433 +3353324434234221612312332362235211524212322322223245431423471231226223223341231547252252225721232146 +3651323123333212332232123333232212134522221322233513322122222222423232332243222331323136333332123222 +2242133525512222282712142225222223323231132233332423323123223224221313122135332212433313422543435235 +2211122225232222232222222222252122232523232222222212112222615122212212222221322222221212237221222122 +3232456465363322454244452522435534241514555554244415655473144423237374426244423745623335643334245424 +2233124213222222332212222222212222222332221122322522221412212222212223221322122424422412623222222223 +8885586974497976564868556679874676725886798544777587986859786877845858874369869775787861875756878862 +1222245443544433224433242133533424264743344497345324141425323442644227246633242324445424423244254244 +7364644615656851376435537622356923457226276466537737377337677927364437565724773651276277666752482716 +4338342334942145452233223433343393343434352242574433832751566355935433135421784725713834375362623936 +2432222142422222242222222222224221225122222212322912132231443221321322322241222322322322122222221232 +2888313145855285653525744565532735484664728456434362162876335775816323536465677228212124331576667569 +5224443337435622312133421443341422433332325123432323433233323224243234243322532214234123333622321352 +3352333335624234132134222323322333822332125352334312467363231322353451333212326323324234222412327262 +4424547244322321328344444345564325445427485463457522347323333642372244422248133286523524574356664847 +2232544234134443346363355444341335334312445212454223252353532532614444144642324566153535452346444542 +5527433437335374433857213333642734748353443585393383663333741373742583333538536646437837233263764753 +4344334453433341443333432434356445232324333233233435522633233341434532433532344533753574333243334553 +2433323573233323395323355355513337234442532333334523643332333323338243435331523334323334395633343343 +3115112246125241224232227341252145542231133332244454312123222239221354262212424521555113212134423422 +2461126254564126265545425436556416621545563431212661353565432552531324413265341663225111314436224789 +2974666557555662322659553643546462465262455257456525725365382455658674624352466233364525526436446454 +2344237733343336433941333333342333323332422135324312246324223234244335334232433231192433427943433333 +2232321425224332323333333222323322513222223131222323432123312112221525222223132622221232212333122222 +5373343363343434434692343344144334339434323433131233293383366213434224333442443233454363362476333274 +3443241332324423232344412422444343224312423421321143311424221413442211334133322441424111332421356789 +3334255622615165224346626533452565342553524435453654635245442572423527563424243254245222132444444554 +7344333553435387735334333446345342553456536665635555644845653553444765484553372334737164436777441376 +2944544742663539824324329447882624742684257684519944239668832224754655495458257351244333445465622273 +1722223321322223322332233122222332322323222132322322823332122223232123222332332113323322124232222217 +7212122223122321241212222142192216322212321221522321144225112222223221252136122241221342121211261332 +2433621235126342424512323424222212432231132244242522333253222522222244333223242425223223332216222233 +7331223333232323223773233322732221333632633333272733433223233253344372532312332243332225335332223223 +7153291222122122232723321174353223335232521622252322223223435522422221242532234242226211195125332411 +3452623643344532154554547455535644454643462554345559444423323545432225534551353344435444442353325534 +2333323321333312344332322444333242333234232224222643332211222332242333334332234433322233412233252332 +3252534251524444454442242525326334245154544342332733324444434433453334348341543233452654442353442532 +3231671466523253723224426133243333722224322247532623432514414229442442332232243464642222644242497346 +2434243344425422334522643433342445434354454454543353335344232343432353434334332334533445333534443434 +1322621222222334933232742333252323623443723246431222926275222222122453312131123432222132221184222225 +3344345285445542232352624244524243422533212334243712425842232322843235475443125522253433334144423166 +3445353122435435333253433233335566312333334313434333224342324443454547434434342354364333463353343455 +4476525542314133314431445525145343354556512243454173543254521215564253576525644445666236444366434454 +7566768977477565546384757565766657756578556975868768756466166526858647684755968668644665648576699665 +2412334322122223313322446232232423522222625324123423132121232324333324342242432163232212243254421326 +7222232212316122222223228212541225112214222422234522222423152222532428122321213133436122124512321272 +1543523223667613178472662226224232226325325223222243531852222356732622726461174414252232861662528234 +2542242226324552542229234712131447543122224281322624282379447411142275238522325122522443317914152124 +5326935751212284264422242131563454844223644141344299463841944323341426213625642623365364241247424225 +1415315325441432433425454244244134335225413351231431513343421312143122315125535455424153425331156789 +7333232333314343334363633333334243323434333222332332133334633263334533322333433433425533333636333243 +6444434346442343445244364234454423324243434577433247743642446466344464164434444447322433363344424454 +2321363227632455614267472344256866763526272222252317332374727226746634722465247222512626512262439842 +2393333223449343424233333763932124434124323336333295243234214313272242236233323334323343333442233442 +3333633632353353326332523326333249633333233331333133337333438233233633233333333332332332223343334333 +5355215454545343454443466652533337554635444333337364313434253343645554234735446454365442655445243354 +5292882428649236756448672454333232153432357784212765664476232424321552449391444274244924755534564473 +3542373153466322583543436434634253323433333324331353852543344246334232123365543534343443542343332235 +3244234232241254234352224433234533313222323443321222313231324534234322233234331223334314434134342134 +1213221111432155322122222121222222222222132111121221222121222222132221221222235232222521113221432221 +5242311324332722116243322922224232246222221222131212213122131212223322332414232242223221222124332224 +7454132511454322626252235385242574942547256245122541227462453323521243242398322842423455542762642213 +3422625438535318655835453462742236454559673365563575346236243542284435538452836574856662875273434662 diff --git a/day3/app/test-input1.txt b/day3/app/test-input1.txt new file mode 100644 index 0000000..7255fca --- /dev/null +++ b/day3/app/test-input1.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 diff --git a/day3/day3.cabal b/day3/day3.cabal new file mode 100644 index 0000000..c9c1d29 --- /dev/null +++ b/day3/day3.cabal @@ -0,0 +1,76 @@ +cabal-version: 3.14 +-- The cabal-version field refers to the version of the .cabal specification, +-- and can be different from the cabal-install (the tool) version and the +-- Cabal (the library) version you are using. As such, the Cabal (the library) +-- version used must be equal or greater than the version stated in this field. +-- Starting from the specification version 2.2, the cabal-version field must be +-- the first thing in the cabal file. + +-- Initial package description 'day3' generated by +-- 'cabal init'. For further documentation, see: +-- http://haskell.org/cabal/users-guide/ +-- +-- The name of the package. +name: day3 + +-- The package version. +-- See the Haskell package versioning policy (PVP) for standards +-- guiding when and how versions should be incremented. +-- https://pvp.haskell.org +-- PVP summary: +-+------- breaking API changes +-- | | +----- non-breaking API additions +-- | | | +--- code changes with no API change +version: 0.1.0.0 + +-- A short (one-line) description of the package. +-- synopsis: + +-- A longer description of the package. +-- description: + +-- The license under which the package is released. +license: MIT + +-- The file containing the license text. +license-file: LICENSE + +-- The package author(s). +author: radumaco + +-- An email address to which users can send suggestions, bug reports, and patches. +maintainer: rmacocian@yahoo.com + +-- A copyright notice. +-- copyright: +build-type: Simple + +-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README. +extra-doc-files: CHANGELOG.md + +-- Extra source files to be distributed with the package, such as examples, or a tutorial module. +-- extra-source-files: + +common warnings + ghc-options: -Wall + +executable day3 + -- Import common warning flags. + import: warnings + + -- .hs or .lhs file containing the Main module. + main-is: Main.hs + + -- Modules included in this executable, other than Main. + -- other-modules: + + -- LANGUAGE extensions used by modules in this package. + -- other-extensions: + + -- Other library packages from which modules are imported. + build-depends: base ^>=4.22.0.0 + + -- Directories containing source files. + hs-source-dirs: app + + -- Base language which the package is written in. + default-language: GHC2024