diff --git a/day17.py b/day17.py new file mode 100644 index 0000000..691f328 --- /dev/null +++ b/day17.py @@ -0,0 +1,58 @@ +from heapq import heappush, heappop +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any + + +def walk(grid: Grid, start: Coordinate, min_steps: int = 0, max_steps: int = 3) -> int: + q = [(0, start, Coordinate(0, 0), 0)] + v = {} + while q: + cost, pos, last_dir, last_dir_count = heappop(q) + if (pos, last_dir, last_dir_count) in v: + continue + v[pos, last_dir, last_dir_count] = cost + if 0 < last_dir_count < min_steps: + n = pos + last_dir + if grid.isWithinBoundaries(n): + heappush(q, (cost + grid.get(n), n, last_dir, last_dir_count + 1)) + else: + for n in grid.getNeighboursOf(pos, includeDiagonal=False, includeDefault=False): + new_dir = n - pos + new_dir_count = last_dir_count + 1 if new_dir == last_dir else 1 + if new_dir.reverse() != last_dir and (new_dir != last_dir or last_dir_count < max_steps): + heappush(q, (cost + grid.get(n), n, new_dir, new_dir_count)) + + min_cost = int(1e9) + for (pos, _, _), cost in v.items(): + if pos == Coordinate(grid.maxX, grid.maxY): + min_cost = min(min_cost, cost) + + return min_cost + + +class Day(AOCDay): + inputs = [ + [ + (102, "input17_test"), + (870, "input17"), + ], + [ + (94, "input17_test"), + (1063, "input17"), + ] + ] + + def part1(self) -> Any: + grid = Grid.from_data(self.getInput(), translate={'[0-9]': int}) + return walk(grid, Coordinate(0, 0)) + + def part2(self) -> Any: + grid = Grid.from_data(self.getInput(), translate={'[0-9]': int}) + return walk(grid, Coordinate(0, 0), 4, 10) + + +if __name__ == '__main__': + day = Day(2023, 17) + day.run(verbose=True) diff --git a/inputs/input17 b/inputs/input17 new file mode 100644 index 0000000..38d400c --- /dev/null +++ b/inputs/input17 @@ -0,0 +1,141 @@ +124435341454355123215426154623633335253515632111661715126766324653755473776743151633561454172643642461446353461523652566254312231525521233551 +245511412235124443462324321415616162124634352216756543125221217153221173571625651657461264475533631254115165443563536261551653324441352432114 +535224512423433535313554265651244434414561143151655717716741541711367563117623131371214224731576421264263613622241224262344545313355133113145 +451122345542143131562452665343655214214456437171232561314471531615336626346423672726173221115165753146256525552124261234316451433251553253424 +212244332555442112314533652234631224664221354151426545553337632666712466532464431414173567552563342134626454656416126444326563644353335332543 +554535145113424254363465663641655417474247564144622161116551735351567724353336452526614124434622141464617761525246522213544526446521443231535 +215212545224542454625665561165153322654522443533125421631563622516623623235365167244627644633646436437372633622234414455513566431564115141225 +125524231454523614463312511515446667544371151265322645435154267137435342323444254575524725545142513722463525472434143431454435615433443225112 +513541224116334641664232543134373557322352355111171135721217616774473552733531621735637726577741743153156667733545143211625545625566355132523 +313443143554411342114636642542757267111424144451726232673613383732438468532653473823147545175314214734435213211126354433523523251154552514234 +411515215616461644253642646437643373527335523466455444178645732548865468422324353732628164145211167767262362723547643256132351463425435432152 +513231262543223666515523322321413466417474234274544775853454772733683653278632426456347525547742131415217216652136514254666635345231543623243 +511522351131244464435533525354272136156153517535482842778552583457354425486367282784388472652324646644467277555626432321326161156463355444524 +535144623625345161615236327662423172411325772756878885348784662672587852624622336658877655723738334741165576711546453537165231464363153625512 +511422144422621113365351737643655261233114611667428462253452664242836566272487867854477624448423263166155135666431736721416246334141454265224 +335353642265665421255562653122314613374242126355346577675724572742863584235245784277477453422273734215646537427776617533525416326122226354461 +134416116316546266131266146431657717651314343788782883557368852846337242557552676355766533427585658724753443222315476521653355522513223364235 +125426625535363462116747475221552522421653352846784563255545753225242685832776745645574565772767542373222526637777553446257414453444522122254 +225454633556141126314521164714463176642725683868446488574243683368827475374854876747385364383235683478224651612441212634712343433323543623353 +513142314665646667313372343331624415837673742263545656852368875866558755577364778562242554876488828247733626721117562272634676326261441435362 +425554466614411617542741435565244622583563257868434634638524228478783654627573845437645482368257453553388854572561247312447275553265151565261 +422553253413233143121611141317266765736455635847756427238548555323943885966899346527727836682657245262543234434577755436216717533113646221241 +516542211114415647465163177424544325287785455332435842325624743447789788993985589453223257232333332633763876766371532517524641634224342552241 +514462214435411533744326276161286283568362772852273463659355963538679983883533735969984738826732558535456557776727272431371216341751532244433 +113151652331632641147666613747878853528737634588878259735936594434785658957779684553885494357675736722833265447566413436654343737762152264636 +613425456233375545475621533786534428578347588657527387435776755445936389975936753455379455397683565285466376685777825272246144143755333625542 +645626224166276551721631731536452632387424443622578539657443836355697358797498664685764437683388643383866866846847286121324154125572646436462 +334554535443651232734472212836334473465862826564453698737893866674993684338997684569958549358797474285225846238434223156154634357213311531524 +245611613762512413366227134228664422874247627368356775754353933785746445738756744747588633864737433228336345326283522841125551513262214633442 +364123421532133647115356164622644856827454884895359954463499539345463483464833346843789838954994668876357742243338223234667751444675355421552 +145234511364727511643567337556856623834247898658653574769956348556736746368448574379368363349734944464248677245847822835655227251562427155355 +331333575537434234247453387855858783435367865353384453486433455567334947683668495354535443876633377444882753846328266246721231142227445555521 +465166347271747225113365828537327354884734546896748539338334494358566489695855339477898363795369373399644344682358876252364355633716532621211 +634115742156313531275744848482677436433576696835764533333376465387439763489486983675985798947548333347564433433483446343653637541764257455413 +542141375776472377332784725232688763538663589549853897737788358864454686597985975337689788839575486986566677387737768356455445564476262643121 +415116314566662533167836546854676883937884737953383498659588547695766876674746944974573996959478988747473883465334566386225421162321367627346 +633151165557547655274482283453487373733479584339773699484968494994586594778575958956973768889977638338975547358275357763363526613142534122633 +234634773113673731884648523248648748734967566757587389566984848776699574489996868776457776488544647773963699837675553524823377435361571467534 +644356251325563371355575527463375333466884556837478695656546748555569484677978899674899694866539543395396634738863744676725534536754533413112 +453454712144521632727442224725483755499357396493574855758459887699986945867948899865758497843593573386989765462662723264463658217356565722711 +642147141254334463678853363877876538354934589645776467974895655659564484955768875759874784858859434488735389664666327524872644224744116273521 +566531415247112475573738258248344357377765669894567775765899774756444485674499855798446646967784798578594489585983238758237326333172766566461 +642166614757761255322682833878459778869577653668766546744796646745974785988495655669748447487574444667689497644793675234785758813412225165122 +431227233757726657662625542588865654843777858359959956767867687945447949857448668475654467486677839577687834359736736462788466732563251733357 +673336433152731774532848272578455938583697698776468855656756458587579458688555549645785956597977746769575898696884466822833845836642234422325 +626323467312477676226786836893797356874886449478474458797446847497445485897777765977546868696446987369497795743799678664775467547752467525165 +366747132671328624626766362386559656754946958968547787868845798849789686689554958665449679979565578688363765373745368462387357675712366257675 +441234543123787628448888464333477338434464455895449789684797446857889697787569648585697468848668747867868675797979593433574245258334342277265 +537433714321385672423767655759397393958753556794877958889847555879856775878788897689797586688757585664598763895975937473584436347672116126213 +677465325414876743447255887463486358943737845865565558777787766885886789675958768569487864859484465989387968944866438773723623578822374514423 +576266563521725266385835385797368347573899554785757686657857569785756685578575556667776979448749667847647476536956784675647352582533154276131 +763456555647446446464374875464794889685974985485488878666879555669778966585685696887657875795948867564489344667438367632545433537676471716211 +143755661566373455756767755536739475385558874865994954476989675657796696668775857759798968577777485556659398757454898625864436434477673222514 +444474461522737668274238883795757357976997787447777467556865695857596595668765999589898859898698685454649839798984397498685333334662533133341 +371562226623425254686758385948737959955984975696788645555969779699565896885776555656876889858948779688647438564785945357656474364446465621661 +141422727348285362578485593653477744456666787875589796797585897575968678557997887657697585575449869447477845448363348566468825374437325477327 +735217423268824878436255979574934947998745478476465569769958965775677778577557765557866859894967879476989879565959764794584533862888743436144 +435314621464564536734648464563989558495854945577594675578765856797657889658669886569995596696845994566675999486477656478773465353565232731525 +663766667155453676863766734745844648454847458987769577555566799667879657575987696665959667876989976668869746599876869584582678355685777257176 +562315272185754674882355447786357954578659647597555669686785669658968677666896769755595986795665495894475857377955365883576283243237875131357 +173511251343326247846697934396343878879964768687777975665797885867898667796678678996866879885787644968685964564466694958845242745738543524564 +443131661277344874847298364534359667764566496744775955597996557697676977697677799757686785699664744496595799397658737899385274453883757143654 +467653421238774433646855388655884568674545665757778668959788589887897679889669967757766676558878457999894884599763939539422876265863563256673 +225661213277566267457668485976796447454899887557599556989887887868769979977696897997676698877565995545495646987733735599855786747778886133125 +246724633287776348282649497953388446665744894999988599775659698887888766778769677959698959887566549545476446939557675978846472875685643417322 +214526435527427583226756577634736795799555587865596977697996886788868996698966789869695985795775444998989577483475567344693625676422534221161 +477353622424775483684949763376795878465578545955985655879595766698799986977776786965889787578999465476657467754744583946787745255566255742666 +333532275835658452242455555364486549568789468878778555758766668687897969767898869899577985597779699469866578983455358758338534432633575672414 +264651165363236423645663787944863997595977685657969559769577968876678699969867799996697996876967648845785586737933454974994752574246784743372 +242173126467858574848578638563673546866668865988857687777996777769686789897986889798779868866578784487456447449873754594853583673742821413454 +361223156766754628286564669957558554565556749488776778559676697869666879698669896788876559597966579759894796883997468995763546385225351637727 +441727134245227624354579367937469854866986848458759898767956799986999789688668977669766759756778986977764846854579696877453686587557483514456 +635733133527728667475369353887677569997566475876678765959697779687786788986668998978665568855886968485945595838447746688834583785274287635211 +531541162862727526342854557939536744469856558559776767975866999966766999976986996979569965898678768769778797945549543454593858742223664252356 +637746474582528527527643893474977648859774755678675866655659979998969788689989889888866997756997588784559675558459768554753264455246876141755 +275473225442324724585947944389477746869748897548988897879559687878776766978697677699799687987876976859469545643749359734745563462624242676145 +164323275635548782736968863598847467447569676669698588885979878879977966989779696869888699897897856964754856977485889966584833462578883355651 +374211312335386325254799999845586977956684797649895779559698678896668869679878997789997796868987899678558894783583779978695538727245232531526 +112743165655786242376769549989979594658474547556869768659678579769979698887867698988966977757895569494574886593344447668948336234278367712234 +644777775274672386765837539437559938457766598484989887759877579876768676668967998697985867887887874775884985954758493534926453824833624167146 +225314375734382356827776534334383845479698486695999689889887555979798666697779977556958588858554849546897699673778768343768264842762664463333 +426337161455644246722335546935876949577944886466755789956897558867976978898677878665995699776657798577976846877789399837837485843467821724354 +671533562255777834353757734793848466686894795449757796865657987858689979897766768989597655987597649566444888964768863457537684356764337633527 +624274726163343242324489657956643483474987454457665659695956898757957799879999555566967978895585486946969793395643577849853878757738867612477 +616622461276234222637834773757364769956854744754559659866965765565887695669659996869979895985745699596864759474395855477334338374454471772622 +412167477446278853766379498346355745969868484775995679987657569576685657596777765877675885869996459454774679486677435568388332486642244717165 +433612245225355573748524455838756367685885485576977856879877759966978995869755555986895575655955878997997485865863746498325345782467411516236 +374645574563864378365722464959658576844667874855556887586679797595689665696557668669595659667877878957448968749857353494658563254383221136633 +445736332563283736226284475599486896787844878598966699896576978755889557669687687699887997494448957695744884789733433995527537748738732437257 +356475312148438673378268699683776855835467688675576747887968575975877697666695575878677686977648986849954834897489446744365478243373343637352 +463611755128462764768264373383767534645988994466555668757998895656899667695955875585567778478499675794664953344669379547833444677826766415772 +677133323432232333646333897837793633856878594658988584647679978955956869855565776879599598568676844858598557865533949866227786644373572367536 +622411671731367683732686229934643866775654769669665687659588698786888556988778596765984957874858575895555343576485654847454856378286534126741 +446272416362578456762547737749546439893458574579549457875779558686955565695959797866864678946947457597474444379897899338676382445377152226734 +526744332762662847248858532394765944856743786794874974688958688789866655876759696579884664784685645546546366336743684578545627726363634224736 +421146213565426488534574478798363748564394499467668758644495968765569759796889586577778497874566589777664933987639435332255283343827274153235 +146275717633768383448758378564565798788986344449874976567598548984689989995978765884896887865864564875364334793943967645245783243267263767775 +614553155532546243455783743435943985733577355575895449547866688646489769777758958449974456679478769479674784939833748384762253432425363161416 +377352157174245445662525865334466437753795465478675668779545978687784656677995966647979779678677887787794785574989477684256627534722157533241 +112136717222232676346333868663966397437575635387755586766464598758488889887559795589467788649858897765356765386794554562737846674757773734517 +347437462612722357578637283533364669599797898594475665896646894488999695667768589485796884754868974954439897589347228846632556876132377721277 +165736377122745337456282684845275556886393485875967994847464895596675454949874966776576867865494666958875695578736268383886364767425562512546 +651346374431243118264658886347787795769768678597979797998549899984544884996994798966566644769664557968659735477386383463263652643122454772756 +246667611732251745275328276688557769967479456667553775654466794794876895444668685779899995588874493835945853447683578562235344263634566726642 +555532542667713372734555628288343338637884767366563934479696995758956956894757966766965496367948644454748738685727843837882543553566765571326 +443125231356221521352423585646468684635876498963588437855864996645989595884765956469665446946863377596868485384244522737556731125742615517142 +161665135574557272488683347384643373975573834578946535598959664445868646988685556485686883466434867956978688643262536732722455252171552676251 +324666256774761672232623724745724837474638475959756757983633865779969757599677868856495376653946596449536947528255475543426612234251667411322 +524243444672334573314322722447636445354448554856385439537667695376458584976885964986756474687857675976337374634857835882853762777473473772324 +542324575272553352467766436578342662386485798977438957454763446777745987394953748499943458953759354987649564364548475263247246344576617412533 +223651657557534515357685738673543655228245549665593939977585556977987993756768796556946958977348534478762483686744642473232772341742736121334 +265446555222435267724144267526576846735228455566853759563387496746836864833795785599855687489475455346843357553566553365235245654353416526444 +253165652154626554371755853475222873442883547986569895376938657766936378755765884363358969664476447785246223833755232647564377463723746611511 +516244356473634777472576538484347866476843248498855483456597995537693966485873863598743878955963494427543242866625628412375144716574615662122 +663113664516763353745316152228573673437843588997737694378946479777668479753573867655848377365433934775464664788228764776445475171165474255332 +351551252235655765551233643388437232455336333276794794937844364666793967549689534457358544959789842736424624528664568664553676125722521265545 +264111262264645575415533423633625243662827824848536953379886659863585778533496695866575959365958522838675444284785271632753236453637111533111 +664124566644512434172723343644624622258223662425886954884984699874485385495775346975834854372276732668588465236625556376224361515523523434226 +546124654631142736571227764276787834363867385553636364987374498664797699737634366354743594234376273777256256732641374417455375327735134242514 +536662465632624327677457227344274454785268527762244442888886367958645476765964596374348734583738586728253534445531167546127246246562121652425 +264514636453136373246177112253725578788274746388523276263657835635638746968845859582488868424363723536434545887662251414573373655635433536141 +524264443314245632443312762713665466464756835434746245785685538743863389656337366343253724558635243756334332682777621652753547715216412151516 +453111652361221535716477416325334162557224637244578245352583825724643484574222886373752832248284856674756357515441437113546333325351661554656 +252226365561643517454357744533117314554553826426774735382372762225632644484832636578677886768247438537865457737675636372567652616421412131213 +513344433456465256431115447627314453756484826855336334883425358234447452688325774357588562258755456678632843511732763623161735525535355363362 +256153526466446651524517172172525643567748378353588654747577623325335784453583725424563782263882748456627647773471344634653166446114525245153 +333231154416353526417414421327113576272428568685222444577225446274548226433472548446484576278877355328761644464165343751625561525143546541621 +541315344521414232556236627344345413614726365446224856734353628355624426576477688584682845882552685573352275211421512515713116536645246166131 +314235121425121163661454147636376452721576321726724745885225288822536248556877243362636654524743583327532756575377766225623531621661144226115 +351253336435416221256334616262255126442624441676776622454764243872537846546222445353234236237438622516176371375647533615444255125145421655335 +433443551333536115332235213532111262666333443164162356383535673347656462256847538775483788452341373545263261636625521172512213616314365412422 +443444156142663632621614234233544142666647553646147222334457333457862548856347882352274558557621332732152237677173245313443163262254636424425 +543421441545322452563116326741151452324536364217441727218643233648853885648824436778283161477146157674175244755523332633553525266212336244255 +344242244316132165633456441461561127761276114126224411565526775236634853454775542743244652764411124475474157751464235123156316465131422242322 +353241121544651322462663341122675415632252453212637767131164525155167224527121346233413576316144456161162655161363366242651645426514123144351 +321333253326463456123455166213554332462253171575673427756416212363176643257527464434265776376733273547232247171722412652652445134513442321222 +321555153422311123335245653265652357215421135672174474547373113355116225545535554536227665673726511243457113633242312253251134351341434535134 +422324133354263433534545623146151362323455616474233456564671227241633633152346547757227121546356352752236237446244514433213144156223252321153 +133151143424533213356612226243311312645763246727241721461327275454132632476121717461254357363641155314555151355556153661246466312435523325535 +514345232331425646454454263433236223243513621773712335624344514714346321422713152443142656235255227353513336365662113556351231362142433255321 +431143344332151435234526462431462314151351142521244117557316623421346345572431154316112233573672672145126524665513634121636232411351444542321 diff --git a/inputs/input17_test b/inputs/input17_test new file mode 100644 index 0000000..3c85086 --- /dev/null +++ b/inputs/input17_test @@ -0,0 +1,13 @@ +2413432311323 +3215453535623 +3255245654254 +3446585845452 +4546657867536 +1438598798454 +4457876987766 +3637877979653 +4654967986887 +4564679986453 +1224686865563 +2546548887735 +4322674655533 \ No newline at end of file diff --git a/inputs/input17_test2 b/inputs/input17_test2 new file mode 100644 index 0000000..4a4a3cc --- /dev/null +++ b/inputs/input17_test2 @@ -0,0 +1,5 @@ +111111111111 +999999999991 +999999999991 +999999999991 +999999999991 \ No newline at end of file