diff --git a/input/2023/day_17/input.txt b/input/2023/day_17/input.txt new file mode 100644 index 0000000..04d0721 --- /dev/null +++ b/input/2023/day_17/input.txt @@ -0,0 +1,141 @@ +111143231245512453233532231135242346643223434366453543664553534426223663625563233556655232522464453223446255523366641115221525545241115541324 +431121345255453245532513346436354562424226665435625542452543534236556535432235356366222264336444256255334564563564226352335454152333342425531 +211555234333213145543413562433332533444634432342622623343343455746676435745474465524463464332524462636562533446346254251543254415111115352424 +341511535443122253134546264233423566232562433526456565542774357646735536654673474477533643232255236632236625425666662645545552141115242422515 +341111243411143233254353542425566625642433362644332467546476655667755374563574564666444577323426235362444224643634223545235541134121215423153 +531254335432123321511666236453536252626542365563233477476436375343677375447543535337476464737335222455333555262365434346341345121543421555545 +352535514521514111243342542545234362225656644366655563365373766737565463557476566764463743673533232534423432654354532444434614515232111523231 +534444213321253145553225546453244552566243427573456456543553566474735356663557433377547454546773337365265654362565366242546625513555231514252 +323313551435534512622225362562263434433225636635664736767633535463645434646666633445555455344676663776363645426625355443264235342154354541551 +313344353445542136265245463656643456254675743474744556363764667356336334435764645543447755363434777747562523535253545462232443334115422245343 +411545355323353632256366354543644422255357345374367446347554465766367637555443653555375756736537575763375343236242446526465226534224412555332 +533131433141412662643523326542423234474465776365555735754776645376734774677646654667473753373634447774446536352326333564334433362123135411531 +512414542555163643526524526245525623733466554345447435364657576457576376563367444354735757756765547755635545232536536433564236626315454145425 +222144425355524633235543562342566454344764575745367646565574575636547773457356646456657637437547666745757773472565443652346664466335313555551 +425225555322663532264234233335563664747574765576347743444637647676737567333767356736363333744734647436646746476523664454633653546545122514442 +433432345135556643645433433353363575647663645376464776466577573557373364773353335555374554433464633534445576673653325425446624523422432115313 +331554441535436542646663554246654555367643355653745635636355676647567487764456667645455574637564434547453345476356634245324255236624234322155 +412551332642256525656556644673435575337544534765547457535765745875846744677848864748453556456475334367766655534547433255523464263563352324545 +443534155643554356242335334673736746664536333346353556577644664845484655676855576588488476647745635435535753465654355645265546422335564424541 +325113645335432555665355233555675344354467477467666744857886746774544874557756677867665777554343734443655754353756656432453432656526442431243 +435351452355523445664565747636637443475667333666457866648755647865645478687666547687547448455776553463657435344656637472443446523245236631254 +412143553424345325232454645555356743366537373758877467577647678654487674848784877787468644447756553677573465574353736576542332442522366564144 +431363252452364445356544557475337767655336557686768756474568654665465464884556855744876886546758447344734334637754557774553226363256242242651 +554256445333463534225356746736736743336763377746888675584476754588545785578747858657577787754874858864355637365443444365474546636352256656323 +425454462356554263636434334444466455765534574648745458464655777887654744458644875776467657545757755464375636667554445537457232633344456335351 +322453463334656455234546433475533746373377467777858787845857478676767845586566768464584884875677444488543533447537536376643424666645653654426 +432326546526255522353556547373665657473456445577748547665675868677547547785865454645454554774845748574876563377757437467553465624365666252336 +553526552222562635473565556657553364657548886884887656456865855666787554686645654858555566768654648745854464677634735446546732553642463664265 +664326265443345443746744735336634755665574447844755544578487644586454874856778477455686458576478578547554484667445666574665353445232224622335 +236532234245355276756476464355554554458887564887776455877644448447886868476548867546888764476866776558545547574453545467364757744326636223642 +632462455662552665443463346747745775658756746854568758745668675885889865596789855788675844755468675857556885843566367733464537666322366344223 +266666434363243576375767356665666877464755765578746575486748669688797696588866786698768646566485868545858886667435664777734463342523665555235 +443556525665656734577633335664448556655454678484454777545656666779696855688567966656964658877444685858567566545447466467443774467266252546652 +624455255224344733376335533753476658646544876564465586875955788776686876776867985856986576866568765548476654745754674775433435367623356456444 +262555462622446474437456377453868684444668468464476655987967866795876695585957798678969855857886488747765845586764675544766563666356435634332 +665624325625734563547365736536784556858567746767459867695988595777767769865658796865788965789678867667866557578674565534645577447432435556524 +333523334637534573776476636576557488766777764446687995988678876956786678875775556987997957898658565656667557575548444354657655335367544525324 +456466363423657475573575435664578688587455448765669969578697785989658879886787878568857799889578874487556756544556443334633446546536443652664 +222244452574756367757776634686545577855644575756668896567598879867997957659885755779787658957586658876446588645487856454536434636745444545545 +232636356457556536556365668745444488466474574987976697975796699686796855588666956768955895958675868455454458767755755376733663633374544556332 +332332622576377345736534646747554866678866788995569677878555787567775875779596796566579977885676656586446765845854744577365444454343372523366 +555664262474645377674577674647867577478684688557896658796655557657577765775656667969779597586797679965585456677454664556665646757366673426233 +356255527734373436547644678865777848585857997655679875687575776559878678858588867877856678657789789868858554664844468646634734744447367636636 +552366365773455644547347758554786677468775568778877797978995878598699565857955898857969565997667878757554646688678786867367546447363537255433 +234653475644663446373734847876648467867886788586887579567587799999866977967976866766799575567556897979657666685654448547664746376435346465542 +252443375445565446367565867885578575654575966679888956889877897698678786876967769856955595897679599865976488848485865647735437666446344436434 +243525237374773653566366744564464668675869676798668978579668697966669789967787689668989669967785588576658588654658478886653675446657763525423 +562444777554463656777574486488654468576556956965987679867969676669986697889777667866868559769957958968686745774765777755575354754635335676536 +264352744565456657667484888447847467598998779789689696779679766996889786776689888878996777588978666887977978585445584446655336663435446574336 +463534645476375645554684778858555786676885996565697889699888899766678879678998899799887896956567777585869788585685647484757343657653773563665 +432465634665435774545666885858845649887698657968779889688769778768696698686869888977969768555995578977568967465456666555784577674443567347654 +242537757554573534676885677655767659695959757675998797988778888678799667799866896879686987675557878578887699478448447774787663353534367754553 +524667356567664767788788786864867869567798856688887886998887976986998886686668767699776989677585877675977659745685675557555653563563733554246 +532356647776353655754665854564646466686577777586598677698878676969777768866697689778788897669979897577868888984565784456765466355374543736764 +353546744764545745656756586777658658697565657989587766697669768667797768668968778798869989666758756995866965667844757477467833575363475573665 +442363475553366336665585574875754767958869656955766897687769989798869789778887686787686676968967586798766967684684745655757855634753733757752 +663437377653673574868444644474888676596699778559668686678686966786679878697997667998668668679678775566688577957447548475848855753337766577654 +245676667643445744476584585486889868856958677757876786686678679686978889886998888999768868796876597559998755655765588557465675655453656434644 +623664534334746636755767677464487956975558566697767697896988988986888777988897867888887969667768658555778858798767857588765444464637377577456 +524435763477353755875846887578787556979997766576796978767969777889977779899788987687978888689776897659786666856748548645756774746364367376634 +252436533765577358467857485675499965699779976868778779896779686779787987877779796678799678678799677596566778799678664684774444556767456547463 +425573537536465645667877564778768875796687778666777866678866888788777997889889889698977679979667875765578958585784474754466784455733343567735 +464333643643774675646874764777889966956967776676876886799688979878878778878789889986887997868689965998667998898957544586884784556635746634644 +467634766367575447675444786885857898689798675679979967666789787898898779879998789996988787997866879557586768558865648756677648655653544434374 +343746456733555668756758766647798687656987598967767769799669777877779899879877997887788976797777786786686766555655656558576555537543536634463 +263736676335556377774776475777798578965889979797877679967769989798889889987997789799998866789986877778878665966865757677884784553474773577764 +436645433455747776854766587748677575858967897999889678767678779989877889979988889997876876767987687678559995869785556756658784533445456534364 +457333367374443446568888867558968899899755558786887989669778779799789777789789877797786878778769899759866986568874778856465478635343345547556 +653357365735437544784775777648977566996787598779866797796688878798979898888979997778969666688778767767968675685577484857666844654735677553374 +454465553543537354546566487886759755777886869789896779676678877997997979878787978898769888779699678677788756997994554564684577435566665665735 +645535635654537444576458775888989557678676898899899898879778777789998888888977799999876678677779969957869757677687688584466575575557463464574 +633446673336665785786556488457569967799656676688968667697989989999998988788999877898996869789799978989768599766594875486774675764367566653437 +365763544374353687774557856867888579757898686969676796986689878797978997979877977999799987889766778769957988885866744454548487444537747767446 +333636755475457746774775664885768689998686877878978686686998999898899898989888977798768997778786769665986876885655876855777745646444453543776 +257347366566556755557468747484878859559855958778679799887889887788997889779777777788977698668899997569799999896694654866676457747663766657374 +554664657744433558785576687586858965859676988969678669886988889998798779998998788898786688897788767697957657558568566856664764845635356777743 +437633353637643445485457545565887558959759869766978967769977997997797798898879888977777798877668879696986885979575658888674584733534445447645 +336773576566535576644748576564988895569965579877899669988897888788798979797979977997979687769689677585858655985875545766888476856753753775643 +447375436754766447678756554674875656886886797769889788989967878998987877797878888989798769897678795698885689978868887845785867854575334643545 +533774774334356485744844667888778799675896759697877967979877898888989779897787899796896869887878898896679879895846474568577556875467653377434 +526636647656677568754477458676456866878885686668989676699978797889989979777797888789689887999898765668796877998956878884644777453766767665573 +345776437655574577556784475666467865969688769689967879888899898787979977987989787869968767788798958965676985977558887786764565334636477577473 +462757555367744435776477675765555899756777555889787899796876786789979777799889999696968778897788975596967795797586768684467474533464663474556 +345335634367663334474446748788558686895898855886668998998877799988977999887787778696789977998687999799569767555576845465465587544735477547753 +352747473367547538454567786546446657585759898569988897798777688798687998988998969877688876686969789765886558558776665847888845556554375745442 +525653575563555774887556547555749798556987599756797867867689696667867966889689666776786689697968958698665787755764558846666855575573747534345 +326455665753733645448778877747875897567979969887898786977796667699978788696867677769979676996787658598765877957588784457484655764635365737434 +532467433765464655746576865775654596585859789697688867896988996977778898788669689878989769996769668658678679877544854467444775477564333674625 +335554544454457654447885484444866987787685589987766897996999779767969767678887676699997786677685669976768778997556877487444743767346766557456 +266536345774333444487446686656585797768599588595599776878699669999678767879886989896988777767965756768698777544665754856565457554453756356643 +342464363736646775655856546787754867795697769679858888796987869768899868797687697688878796789895765557688688985446585777584467466735366644363 +523237467646754656468466848574555667758689797896579777988779889977689668778896667696797676667785685755986597446645664545687753465567453354335 +323327756536477335755748475656576547887995887876985968976869696887779786776968996997867687879755599699567657755468776866885345435457655334225 +465352366554666376467456666544455546795667757799755996999978667976979767898877678768888758985566989859967794845764466677657453363336553674426 +555533553546357444765675856588657557878975766698966577867797677999988677666767968786787775798789997676876774564485686564547464355336356456634 +653433475335664753473446858774765764677666859687678977599767969767797969796977878998788799568865956576659644565846455864883376645435553556424 +565624333374353367374378478655648748848595599966858998869589999997986786677787987969558898667876766679797576857877576788664777573454645755524 +243444333564746346366668646545674655784777777596558976769676598668789667999889879766877787867797657978894768877458688756477776644647454654652 +323463545665546577475645558786854468657757569879597766865559566996799678799877666898856695986577997898647684875678677855454775654547364424426 +444345546665673463753634768476874774848587769865579789687878579978877559996587865859858789976885557859466565567686888557474764574774566345235 +463425647566774574744333678886747686865457799567576858589676667856577976986668978996686799577977886677544648876855645547444577633464636553523 +525644666573444644576675468484675575784565795957567577578956797786787796869877557895769667588675596744747766568788564546563666667763666225253 +434232334334645346434743378448585848667846577675678568857556598869978967698795766555887989895656586784848574867846777433673543643643345636354 +536245546237555656364544635858554588856888685777969769765876857989857999778897756695765797865577585676877858667674464376646773636576723344236 +563432433237543453765335674454676568784657684785679757868567556875775895977656667977959855575979944776757767668868556665477773367647645655455 +345662256237647446335653655474847777777457855878575955988896666669879768598869889768857697696556455457585858777745474363376763373677455666452 +236454255236363665663365567376855855468585857577666775857999657659868999786755866568579898667756458558654648847578447446356744353365436332433 +236656544552467767567677567448876458465585754786588555756756796978557899579678886778787886777445766574578586476464377437556375345344545245426 +662325463226374566436333654547585676688868454444785599797776865969998677956976597586658865956686866776584577484553367473533563564746536336443 +645354345344537355546665737533754846786764888785557644675696956878965869566966978688779976776866567854756646786873676654337473445524622236626 +233264254235366666736346437575764477565768544667785545578799879958588586787879788669995878745846576785646778646655574347764645533625345535563 +432433242246235746473543477553563665775475774755874587665558965695658699586897695998868747444878848474546545784334377573356763473236426645266 +243532622522636755753437535657447776765475565645845684687588454765579856966696756574665577485786546848546585774336373636476643336442355233566 +424366665443243666567346434354557355888674468846456668847655454648844584447647747545884765768788475648857654773677576566637647654653266642645 +355566553355543667644766357475663334576684855756856557474768868686854785488845464657775755684775878866867574456477457355345767654443322524456 +252226355555353432774774553656764464764766745687577646548754448444648855656476668476765857676478568588685475446655746336375532535436262246655 +124465325536222534633336746764356663557877585584548488677874547445477547864878867577864565556567445544564676546443567455743324244266455364265 +444646453636346242245546654333667346776464746865665867844474866746647865554675844644775465548557754474556375345735665646364353323526625624235 +412366252635624522466346354375765373635554864778855885678875757885648677787758444554675487467557768856743453437365564667776246434655322223525 +155662534242554334526573346673376775437747347484686774778445745547688488858484846876546748684767564753677657367475756735363326425522532252354 +111522322233565252443476336555573575463347454766857665787786477486485455577564475876657687767758584655535533636353747434645522266562523335624 +542422536235454325532645676434563747567556555574458874646877684645545674855674568675844866465685676443356746355677555555666633655263646665442 +211224626424363426643222663657775566775366634363464577678645547647756688757557454788857466767766573445336567467663545634344363446256343542532 +524353365435226556556322567464746534674443466757337368478575456847447776487687768855745786663475737763753364433366577364223462546556234245115 +351231443465264363362336352643774674346454366777533445747664448478677857785488484766644654674764373357565474433475733244565424353322356533151 +554223355636366655362352555655373675564737667663367637476378475656775876558465484467733734736556746773634646373557532336625245362343245314352 +415315224565323443623244633253477545773643635677437734464354663476745457476448456747555563574755374344343366743744432254533234544233652442452 +155321252324634365262226535534446766435373535475553354733776334766573455373756333733573667436757467643766753335642442322554323423224452212311 +554231145244425252354633334356244765663464645674467736446536735636374435547767747353673645457676564473344375654424643234566252323463342424331 +145124531215355646265623243325355765435544464565637674665536435365374465373674364755336673456445565567547775765322264364462642335223541214445 +245544533254142563235523522653435334444477535443567574737475663654776577746444777346766467745333753447653577332363336365532325543231234334451 +251214311244114545554462446423624626676363375354453335776637374775465676343453547674544736356577777464764476336235452444566243644313151353524 +251353112524455635524245344436546325534376365643536356555677555675344355745373347466346575334347466467665642344265334364324562252124525455152 +454211332313421263343625436544345634543275377555633667545444553367537367674333374344744754673653556564665546434666555266442463652431455221541 +115242354251513424524244565345324542344352356476443353667354646467467637463545337377445445357757536673532523352342633456335464421222514545231 +234551552324343221533222663526446236435634463664377555475474346673535434445744753657775466665644563245222634266653436345463353354354144115325 +325412113545232114263462446356336532466543425623356673677337363765363633556375433575757664356773345344465345555625252465465324322523535532422 +533444522324551243515564322652262342524325235554667764445446656633535736363765574763563335776423624463325533625243355442245443123534323312511 +334123531454531311142443565534562563644542436563665634765457377675756443636777543663643633363324553544365344443234345365224343523341515544422 +425513541335541443232314324425223643632666545263242622235765443475754557635364446664563322663626426463256624543536634556155552222222255121512 +443141524454451431355221565364643463663664635262463364522342652467643777543675336434624463452333536354565653344664465611411152424331223125543 \ No newline at end of file diff --git a/input/2023/day_17/input_test_small.txt b/input/2023/day_17/input_test_small.txt new file mode 100644 index 0000000..1efbd96 --- /dev/null +++ b/input/2023/day_17/input_test_small.txt @@ -0,0 +1,3 @@ +131 +253 +421 \ No newline at end of file diff --git a/src/year_2023/day_17_clumsy_crucible/common.rs b/src/year_2023/day_17_clumsy_crucible/common.rs index 59f7457..9cce569 100644 --- a/src/year_2023/day_17_clumsy_crucible/common.rs +++ b/src/year_2023/day_17_clumsy_crucible/common.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Direction { Up, Down, diff --git a/src/year_2023/day_17_clumsy_crucible/mod.rs b/src/year_2023/day_17_clumsy_crucible/mod.rs index 261c39a..7a8f10f 100644 --- a/src/year_2023/day_17_clumsy_crucible/mod.rs +++ b/src/year_2023/day_17_clumsy_crucible/mod.rs @@ -11,7 +11,7 @@ pub struct DaySolution ; impl Solution for DaySolution { fn input_path(&self) -> &'static str { - "input/2023/day_17/input_test.txt" + "input/2023/day_17/input.txt" } fn part_one(&self) -> String { diff --git a/src/year_2023/day_17_clumsy_crucible/part_one.rs b/src/year_2023/day_17_clumsy_crucible/part_one.rs index 5b45965..d6d5e0c 100644 --- a/src/year_2023/day_17_clumsy_crucible/part_one.rs +++ b/src/year_2023/day_17_clumsy_crucible/part_one.rs @@ -13,7 +13,7 @@ pub fn part_one(input_lines: Vec) -> String { map.push(row); } - let mut cache: HashMap<(i32, i32), i32> = HashMap::new(); + let mut cache: HashMap<(i32, i32, i32, Direction), i32> = HashMap::new(); let cost = min( min_cost_path(&map, &mut cache, HashSet::new(), Direction::Right, 0, 0, 0), @@ -25,20 +25,20 @@ pub fn part_one(input_lines: Vec) -> String { fn min_cost_path( map: &Vec>, - cache: &mut HashMap<(i32, i32), i32>, + cache: &mut HashMap<(i32, i32, i32, Direction), i32>, mut current_path: HashSet<(i32, i32)>, dir: Direction, step_count: i32, i: i32, j: i32) -> i32 { - println!("{}: {} {} {:?}", step_count, i, j, dir); + // println!("{}: {} {} {:?}", step_count, i, j, dir); if i < 0 || j < 0 || i >= map.len() as i32 || j >= map[0].len() as i32 { return i32::MAX; } - if step_count >= 3 { + if step_count > 2 { return i32::MAX; } @@ -49,17 +49,18 @@ fn min_cost_path( } if i == map.len() as i32 - 1 && j == map[0].len() as i32 - 1 { - println!("Final path: {:?}", current_path); return map[i as usize][j as usize]; } - if cache.contains_key(&(i,j)) { - return *cache.get(&(i,j)).unwrap(); + if cache.contains_key(&(i,j, step_count, dir)) { + // println!("Cache hit: i={} j={} step_count={} dir={:?} => {}", i, j, step_count, dir, cache.get(&(i,j, step_count, dir)).unwrap()); + return *cache.get(&(i,j, step_count, dir)).unwrap(); } let mut cost = 0; - if i != 0 && j != 0 { + if i != 0 || j != 0 { cost = map[i as usize][j as usize]; + // println!("Cost = {}", cost); } let res: i32; @@ -100,12 +101,28 @@ fn min_cost_path( cost = new_cost; } else { cost = i32::MAX; - } + } - cache.insert((i,j), cost); + if cost < i32::MAX { + cache.insert((i,j, step_count, dir), cost); + } cost } fn min(a: i32, b: i32 ,c: i32) -> i32 { cmp::min(a, cmp::min(b, c)) +} + +#[allow(dead_code)] +fn print_path(map: &Vec>, path: &HashSet<(i32, i32)>) { + for i in 0..map.len() { + for j in 0..map[0].len() { + if path.contains(&(i as i32, j as i32)) { + print!("#"); + } else { + print!("{}", map[i][j]); + } + } + println!(); + } } \ No newline at end of file