diff --git a/app/Day3.hs b/app/Day3.hs new file mode 100644 index 0000000..64730ac --- /dev/null +++ b/app/Day3.hs @@ -0,0 +1,50 @@ +module Main where + +import Data.Char (digitToInt) +import Data.Either (rights) +import Debug.Trace +import System.Environment (getArgs) + +-- | A candidate is either a left-most number we've just selected, or +-- a complete candidate represented as a pair. +type Candidate = Either Int (Int, Int) + +main :: IO () +main = do + input <- readFile . head =<< getArgs + putStrLn $ "Part 1: " ++ show (part1 input) + putStrLn $ "Part 2: " ++ show (part2 input) + +-- | Maintain a list of candidates (either partial, or complete) and +-- then select the greatest one for each line. +-- +-- Insight: By keeping a list of possible candidates we can always +-- backtrack out of an accidental corner (like making a partial +-- candidate of the last digit). +part1 :: String -> Int +part1 = sum . map (maximum . map combine . rights . foldl go [] . map digitToInt) . lines + where + go :: [Candidate] -> Int -> [Candidate] + go [] x = pure $ Left x + go (Left a : candidates') x + | x > a = Left x : Right (a, x) : candidates' + | otherwise = Right (a, x) : candidates' + go candidates@(Right (a, b) : candidates') x + | x > a = Left x : Right (a, x) : candidates + | x > b = Right (a, x) : candidates' + | otherwise = candidates + + combine :: (Int, Int) -> Int + combine (a, b) = a * 10 + b + +part2 :: String -> Int +part2 = error "Not implemented" + +testInput :: String +testInput = + unlines + [ "987654321111111", + "811111111111119", + "234234234234278", + "818181911112111" + ] diff --git a/input/day3.txt b/input/day3.txt new file mode 100644 index 0000000..47bd927 --- /dev/null +++ b/input/day3.txt @@ -0,0 +1,200 @@ +3646122246265233144266436235253422621132626544356324544665325242262222212765227332424562134252555523 +7245465384344644334334444342433545356366344332495446345542464454541266464244443274454544425424873164 +2236252122122625244252232213543322314224432231222222232254232723224251242314122214122276232232242242 +6422222157312424653221232214722212222221222542522312242522223215412252622233222125123213231352428222 +5442253214354122234335455412232233424454239863362144311545224542243331245555523345523325255631352535 +4344142444124445544444484344324232434554224624442432554214348344444432242472242246642334134331516623 +7453643126653263862225322424476275744474755647441484416343344424445769444633852332346533273234424534 +5535335555549452745545533556554445345341457675534455695554442635335563453332535655456495344535453455 +3313343445332242323112332323352335323142343323233233423313442215345334352342533232435223533334323324 +5343122434654415724443254434443342245254318222524323424222262262351433613432354285434344316342262232 +1241735334332542511135533294154335421424324485245232724223223425412532247343244444222821213222342722 +2232242221222222212221243432212232322421223222242232344222221113322232325232232413242233224214242322 +3326335824544454446482445644661534255555225433846512433452536533324543265485524536345555535563565446 +7225322212242225222321222322522221126112212434624521167612322511222222122512342242111222722242322412 +3442443535333343323455241444434443444344342444444443443353456334444334224643547443433143443244443444 +8323542454422433355323355742564126223432827336623274822346635468945663533334436331244343381322371163 +1122211222122222232621122121322112222121223222222221212282312221222221112222232112222232132232112222 +3243322343242434492129333132432551532545325226222422522233292193132524233336521132524831432325236531 +2272216236251721224222412432222252222234422412234142113527114112423123223553325242251244722132791112 +2223232221219222132252732725232332122225512222232122241222212222222532322222322222443233422223211222 +8353655835433486374646457445464264426254665745466334533753645626544646464253616456964434547566476335 +5443632333433335454335333642754442333354331515434444431323444536334535236423534423144433334433433334 +2232333332233223352423133333333222533232332122332413323232124232332313134232333351333224231321432333 +4222519183363625366267578377743556652213232326521735574755646566281779474437272255352454552775254555 +3232332233422333321222333323232222331232133123222132323323352312222232222321323232212522321352222322 +3332536163534633222275325354533243326351422245466355386395735223134633323326426364516123531433856826 +2433973323424331524442221454224323214943624321222335224422222422234244312523444324224273465222244412 +3323322222222122223221332232322324311332222332233332222421232432313243422323232263222232322222212422 +7425636336354653955756556574465264526555645623944655424345555453555357644843254353264755467351434834 +3533924317962323353347232351632324334265435453244238334434133324622454422265225434234332247335345323 +6545424464256535352323534364732922544565533235766465636452347425546366181315153524354227444923235756 +3674433442442349342333332221454324346332333447232532332257331331443316224333235464346233535245433391 +4412421233152453212211224333233212112142444242412241352224122324133652242422431124531314222512432422 +1231322232122213242242223332122232232213131312232322334231234122342322313333211225253142238213333974 +4645431425447448584759334345543865444445542696122464564333835434968434455535654633553527444447355543 +2454222625142244234226352634445144262344246222223632336242145423424214622232432534142222263242434247 +2122221232412242432137562242244235324239413223223431143223333422122433421332232233543722332752822413 +2221541421321212332222322121221222423211124154242213222212231211237222322232233231224124322222222221 +2213122234612622322222122222232221232411242213432224212241542322334123144223322221241225627342322428 +5455464544463834551275554355352255442555444445435444435454544445543553635436554642444566455455325455 +3222232223322223332222321222322422223223231223233212332222222222222323213331212293233332123211222233 +9655787145244444325422545511633338326554444442444143636362421324442832225344422522243433225537232234 +2224244245435452339322312238435131483146763244321343254423425433332342422316444513237362241341251262 +2222455245121122222141311222126142232342421326211225234233216753414121322422211323422142444322224221 +5384544555555145642543653745535544514349554443548443564451959535545565534545544555446965444356584572 +3243441435354222141253432419522341432241337235331223235445323313342323243332524353744234232514324222 +3382334363363233122423222376325233333333533295437445413226534242843234631347231732387433344226333423 +2273595222226367325224339833231231231312323769667236543222312133213424235262423337596421223514742293 +2735213672432965252435269963256331688364974722643962759132242422425172523227443366637417857263216228 +3525378144634347333577231424343334641334272349766184613275838333335313383254834893623633334353334484 +1313233422272321222233332252222233333322133212223131541322223223342242323332423132223333433233224433 +2213322532325322264133345511222233523322242263411272833143456226253234642321232532224255224227233332 +4246232455671434645371233376455264253235564196621362465254252315362645254244531545224432362623432321 +4535255222531435243322152341455354123552531133411443332421441235113351414144423541413321434151136789 +3233322257422222561226622222622422214225166364613232442653123262343232332624232422333331272372222243 +3323233332342234323333313133773312333225221332233222323322222223533253333322332132335222424321323731 +6387326116521224227222424122422222112324125122122422122252211626343332323282522322222221232221262122 +2322341242512252242232322424422234224334243532324331222213522222321334335454223132234312232324223333 +5462348798973855855749647676586955687757637396478794757447665747756997473284727478877784444674579787 +3737573224329423565443434443541435853225364623652843315322533332236544643533444555343726825333335447 +4738268464335436554496241563343353547364773566565371343567163736366245723444348368367552444426764652 +1212223132225322221221332121222223222424322232212142212212222222223223241311424231313223232211228334 +5414612453344552372252233452552534512432362245172343235153222423446244324444254654444425524443344445 +6526246253244722455326445343156422213424555335565355566366252264544254562336633425552523663447643564 +7523513542322525222323428333323222223423352112335372321233323332123322322357242332223322532283432214 +3422122222312212252232233222221222222425222152412222323121131222351122312222223212222212222531241212 +3331323333321333333424233323331133433331333344335235322332335333233433243333233232436343245132333232 +4212224222222322312222422494331112233223112212942313323321225223242224272423132122232233331647242232 +3313321223212314323414522132331232223233322421333243325323435322331432132334242224433238322424333422 +1323333652343334343343343323431334423434443313353133422833233333332323443424113423363334424533233343 +4268234773233672242432325288462532512956159331233133982264623842233329232334222432833953784332473244 +2122115234522414232322313322321322332353142331523522262332231213232232112222332335132332243432311225 +7243766478364448863299157833884463537263675642232568647647541444442217343518326648876724432763328936 +2246242352242234453212223333426141323211242422243314442632332241413224344432345222131172444231233142 +4344443244445534533434344223442424353443222534342354211545342642432324424144454245224335542443434123 +3322433343523823233532323335344324733633343337152233235323145364233336322325231323532332533312333344 +5695866346666555733655655578656531534647465568657345666335556333566656343613633156764565468376346367 +5345346635355823516352686964729622885696395482847758862683475622655628383356461615716677867263915389 +3336275314373324132232226335332623336213523362533843345333573322333322337253223712335329223322234322 +5648554645545557351355445544565453535855457464465544455453433257655287531235554373648536554484233484 +1222212322222222221222121422222222211212221323223232111222121222222222212121222233252222322122222222 +7624466883652316715226623452322352362165331383445266212564246522622965663142642216723528533261262226 +3332333333332333822324332244324423323333233322343333323623633333333332133332333233332333227232233332 +4455345554445355454544554446433464545544144256333553545565332364566565544244455425543443444556553443 +2122122252231223223222114222223212253122222922222522252312232322232342222223423133212221632412222242 +3543232332232332246332333332233312342325532223321334423242314634331243243322263223333333323323133331 +2231222222332212122231423212232222232222222222222222931322233222462112332222332334211223222131722422 +3343366332445247632348638743363335938513333626228448643895236333673432886335533664334134356644678183 +6363763833627434827314675533326653545793648764455353721332983922336712673745336244634435375377436475 +1231223626422222927112555156122353142222222396121211312422235581262157284272223511326256227262783322 +2232222422227441214441442442341433122334221215523442142242224342232242246212232442223223332423242424 +3222523322224224732212542226251121254252211222224224222742221977112222524222281222223141422512523222 +7226674585681665678872986726536752224582889349253576886637426367472455966247623181266826796786544536 +2213122232222122122242223131422212222227225212222221222411113242222522222222214242224221222221362221 +3343343223223234363235442343233432322551462545346252334234434353346243322313337434232333343731434433 +1871254336531524746232444332235351854437211524257832283262262323123652321252252222212648452212274212 +4226632823322223242226212224222122582122345222542275245152622252124252132341225131654235322411345253 +4443546337333645723634444334327355653245375424624244344476634543364333344454444433353444453743833453 +2212412427222322322222242212232112222422321222422114222112343222411222222222222222212152452212272122 +2534521274222335227522454234343524443112222242223454211522445512334322424531212251323247424321341242 +4315511223332533323442132421432214343221323322222133335333453221422223231313233113233333432342343322 +2215322322217322332447165122425353223724433172322223323413522232476232376225521413323513322332234242 +1622264212212225125242222422942212152222852222252242322142121232352222132222222221124223423212221626 +2433372322112363333322234122461113432232352321254332414232422224235421322121322334222413222212432212 +3154553513335625445544555525456555235546342324524554542522542575345545355336552652634485532653363235 +2632215321232173122223532332325322127133593323232236823225321726312327243373344222322632232332632336 +6881989755655859847455497565795987688368894887859448958835556545697674633847677938766443387748453697 +2512264222112353823542413231531212243126322121232222122531132331522331244364232321222432523284423342 +4515454321443435432154432569341333414434432213532344143243332444332442334424473233333524424523543245 +3336933736844256544777445463464344352735465454545633336444443643433444354334145344584484844547832443 +5454732555348459649647453927956647545455253685541515556456259445542428254484453345437436694553545225 +2213222222251223334212626221227242241221224424333222222272212221253232221372223325252513222253243712 +5245251522556226344463456254352223514616123872552538766736748734942816443585445632285344445526291232 +2331711444221234452236412525224412233123723223512422323123172232227232227325429243355242231422221547 +3242222223242222156312334322223323252225242142221222422232452222522212312222628221222122382482412422 +2221133322222222233223132553231322343322222223387321823331333533232132332445322322223223213323223352 +3441242223233232211312351132312447242223292322332242226665331133213322233332333123354223222312433523 +1433534323333254442323442314532344333313414332335426432444532273334222554334363345333332335553444134 +7635572256212934228134252636244321126646392442164654251584442625264254334532316434251244766332242375 +4242433224232332332332443435363413256534322224143242222142234222232232222233222232311343333232152526 +2473214445543444244244734334444324233426244333344744342241562211461447343242244444224265141442234354 +4843466643574275335734436422524373467767423556654837855452342574473756433668622483437666446446635336 +3435352335551457575434445355637432433427455535542252455474155545455273426541454435233455455354443244 +5415253344353245233634214242425242532551361643321325622252212322312214222255224232334222544525533151 +3421521422214443263444235523122353244232124338252933244242584532832432255435423631743121433342537464 +5443222522241223252222222222212322322223228722222112212221232222142225442228272223225521432324424223 +7342643552222445843213232137243454423334335323325246423523326243122542243292243323335641344224346532 +1255422246236154365556366341444526366243444256311664624522616216426131543532246536312515351546664789 +2541922533427522195552993223272362424161354252232453533941352351252632522322976233513222992253414436 +7536436694548337444437347464543443444333345761845592334747744434465548369452766964436744838364444455 +4523333444435363325544333735633456356544265534322342372334313221543364532133356535236552123233253241 +4323523231342315354133512634332333363632353524432652423548152431553323332336424426133362323544533242 +2333523224222323233221133219128242626333222223223222727262232722234244533762432344614324522444233214 +4531434444343343334436244334453343333533353343344345323443234344433432333434554144433323534335335386 +6784295938898793458876558774676456956786879497617976968497687764575959998889695958568646596356667866 +2423322322327423412312253323334123313221223333374323331334336561232332223334334236322533223343316333 +2521626623222622332166132762226612244226262423413556612215236555314233222242125452522632522362224268 +5322721453236221342425151334332132354323133143643435523534344132135253243833323351338323574233432324 +5455525558564433624235614521342615652356465263557555352475456265542254434333435245565244254664545555 +2243212142232244533223224532222221233423322242122242451622234314222322342212122354222233222545246442 +3232395336242962412533235346323237433354352323334435235214313325422213434322335333323335343455152312 +6162617632737766658562682537667369364425542826572893638515686156284664354485233525434587226546746532 +1224531552414922252452213552162352254235422223542353532222455622111431225594123234291333223242121514 +8588278793587652475836547558372676263754233667423169847534792666978778673775225773775864653485447847 +3732336834534374433353963477624333653323343243934334233142833833447334947333613443433233623643236373 +5524443342322134833124342133469243224212322351443322343836344433333433223232422263454332644732432543 +5767467746552774655677477874564766768264755717746667465666545674665386564754763665676777675476657566 +9522522314242255626251264222832545324258523844272422275413534522212255354557255375224333542415255425 +2332222222223222232222212222133211222253222324215222222232112221212111222212213222222232222312322222 +4123233333233322221133232133423223344253323143443332222332333334424231212322223322338422334352322323 +2238242722832843522224234223236462233224417367322264514252272831243222735944274137434425954943234667 +3753747356655644256552434742556478329253517622364559222127227224443585174532215545464443623787256424 +1121292229112222822262251242232222322253222222212225122231211822214228322122623221232221922326222223 +3133333325324552224233232326424142435233123314424353124246332213232522343333533434332336333334422333 +2132161234246212345113433325432422322243312322462121332332224634221463232222422212433222233413334443 +6232224727242231233652223117325256351222612324222422621531435421434222222227226572212411522624342262 +3456314344243443834445463344893543455384536444423143463348195345434444853244443445457684535244544184 +4443545454444443454524442455545344444345444445444444443544534443452446544945444545444548344445473434 +2212222124222422322513215252222222522225224222221222526122252222422423232422321113222224242222223253 +2322444475465167432357434167113336321773236734175744354422157737425635371133215562254747515144315589 +3456536452566663365665574465446432646657646766544645346544684652525556675224658266455623275522256643 +3424425444354423324434453524333533444644454435564343354533543454446444484454549535248564444327453644 +6564413944339655555533234335766565382433264773667573674552535435494553563284734565734545834544536557 +3354837544583375485653644654336576328374264634382886945443354315316556747454545367472749483432336373 +3232322253245253521546243243532545232961522253532234532233822551323625132353454522622535255632322956 +4223242222462822531322214322242222242312222222312272222523212223222124152821412122242223322312741342 +2336443324544535135563554525243555434465253538334142614344244442432425453346352222542424424324522252 +4243245355334444462442343544434444434434554454344454331554235434444343434431432354444444543434454234 +2233223334322222324132323133322545324212235442334622273633324422333633342234343234331233332232331443 +2426252244232264222422333232334224242222223122467243232334222212422123224322434122221226142232121221 +3333336333344723253354323534333433117923334834333643433423333137632377443422333323433433677661363572 +5465344735754423934754735516326625395343835357543463358344448745835354377155335654147335244443444523 +3633334253335444435573693454335323593364425326563234322813253443443485344642424134655935375452522447 +6334647859967376546453554838485443465573446567485378674327445756653858486848448796552546784453667945 +5856633543356844451565563443654548545555535543555443553535753455554525454543355853774255575555545557 +8458391345554446232358458584445554592375742127828554647579635843594966548645657445626227454972655352 +6366735464285569899233463455776567442644822242289621463467945643675887526966596499379672673737354962 +5263334535143432345532244554342665443323354334532444453432244355254553534634355552555552535335453525 +2232231222212222222222232222224212222121233222211212223222222221222222221222222231222222252212222221 +2244224211432324122112223312223321512141223213212222234122222124422222422222224222142222224425314232 +2211121423224122122212221221321421212222422212112122232222322212322232422222222223222324222122123213 +8278537343213611873685453575747314865241836564571474348545672433813526827371468446368583745612211719 +1432334333534331333743623543773527333433422353113331733531243252633132233335833523332133632313343424 +9246126532972954525756755431353364422225222221345242442333525257422366743523342336529564347246624319 +3332322553322233234234222232232232333122223233322332213332232322363222131213233332322221222413221233 +3433152223344373145323442333423342533434731314332441632565733223166363431463323533554324323324445345 +2236251423222233342223326232122332142212222215642421543522231122342221414352212432211424733248212432 +2442431442142434432321323231342323411321234114314411221343433242444243233331144344211324434414156789 +2474443432214244344324444424334432324442443217365443425444352434144144344244442324412334564744524443 +5332223932224485329312124484442334522222144213444542332235434442522226374343872244542242244245244423 +4248652554532526444339432346445553566554957474445436744434515449464455246463343345344533734253595512 +2343432539734443375323342335333343333333339242223312367633534823342129345333343334333333332324334335 +4434313521544244534453333545454243334534445357234333254444144425433443234433445324343244543435333544 +4632333625433273333333634322323311231313431533223155423332324424543333335433351244353422333345333162 +2313585222173232863329364326737449354839373123835342263122212332125992322368216327273296218272224231 +3152522253552445445633425362544523133426643334633342252262152323425532364522416214665456323724445256 +2112222222212223223222322212232223221222321212122331222526232222122122222222322232271224223531422222 +3738314365536557837624537143344633426455454458576532327616233524376111374519332364281726543258353832 +3122222732335633356313344332333232342265332334346143233823315463343332262237334333332223233333316343 +2311122132123143222522221331121212224221322122222223325222332231123122322231132422222223134222323124