From f264d498ca177fd35894b080720b8ae5999d686f Mon Sep 17 00:00:00 2001 From: Philipp Polterauer Date: Wed, 20 Dec 2023 20:38:35 +0100 Subject: [PATCH] part1 done --- data/day17/input1.txt | 141 +++++++++++++++++++++++++ data/day17/test_input1.txt | 13 +++ src/days/day17.rs | 207 ++++++++++++++++++++++++++++++++++++- 3 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 data/day17/input1.txt create mode 100644 data/day17/test_input1.txt diff --git a/data/day17/input1.txt b/data/day17/input1.txt new file mode 100644 index 0000000..f843ab5 --- /dev/null +++ b/data/day17/input1.txt @@ -0,0 +1,141 @@ +444545255141453153543115556561643516151423532337412774414441721317213343733152621461673175544545524222563411416342322366541444535522233121243 +421421524314443512253422332664241651364257561551214721455576166524431171274175317372653477517577241673232143224551263322452143223424442512133 +122414233232324335126412114454165555522653265376763523457517545241435263374646313344726475377762477531444132653413635232615665445155454213553 +433342425512533145514543263246344433265717575222411273572357122314777427242561745627716532655224672371451244145132241152215143132412111225315 +112335155225545126251436514455563227144243745612415115466546727447475744536733441241464113572313235175243742551234644545146333661532122432111 +455333521453515232233431516525346222351165422271276355726727772363125254626244724144664336776363437767462654311441536563635211242114453511243 +522233125122466131234442443333374216624137736132361212111446747686624426364373514243652224422433716326443344723244656331312615351223143524255 +144221134113365652322123146425441422351632667313645112315547122442677584866587352325583572477352677647436246151676634366442566246624524421422 +332455512312546324345332555473633546763274324411136732624435351134216434447557632252283813256757466657515145361547526661445645563242522144443 +224335242521162466612611166456214127351247551163328461657262845348263518622265376117142325337561446713741174634357715564164545325144434552541 +444121144645262451415555627111712222165227631514371274777512584647853535741352272153131425376151732141577453673772741113555432416643222252223 +254314111565345255662642154554624114241332765112543586527438222754445281242265785728152741375225583723761136622161736433325325452614553124255 +355425224454133112116232455645723555754667321757278718318411835745272858713571418184458243266461483437216624332347321353122324226624666234523 +311125422114465124226314263544576677714653883275856555241428263714827568285551138351435628776322528811452444316547221423441266344411365223444 +224424553146265311453137673571342553436177546363728772336557241684786361667817582186225126372118624528616226132746227356226325325316145512235 +124533441425546266356476314541613554542535246324634554322541538222281546624887535142172232623388322268826735365423614513161154422665545316415 +553263133133341131514774742457657242168585541216687267486763251443121785736466786716743446785841476682443277111566663363137536535254311536221 +532125355316261261237311264453657417347883158481815714228643377934167897438439162585272111262882674331327136845147155256466727312651543446233 +225421313265425445335245115537215588483414464368245118628782824714775381238196349651385561145267813755537228157446615665573422721412365363465 +531453563434654712424136333352742123366187556217713513578772883138441111127138532318432634565486141568363884845263154333451423741246612134521 +245616156511146442553144725371743624133135267328282322651632723954867387774641259857439484721833382848523371783184625377615212171344412133311 +426522664456141241646534326661586182872585453554128366949894532966338969913382994353971228985423281683678247246321734532171771713363614644352 +634332356412571443217452614387342887437741621276786347887144227232822787721342254649496424334264754177147651587286154765233437761733131631234 +232332223645355241437245523578144282581668384828452267423594351371232214384917414792156219921211584484485245732816511657723346145641231523646 +236541624222521254253671571853366454887248375891679981742519765834646658246816286831282758856254124326427568351182228376612375375337155261626 +141312532541371163636776541866536536687453218756113743364554327717688369514117591373998695371435298111781655162185714227231423667675322543544 +165354626147221223414722888668683747163485481714443185582661978555128114922685841726345483882112769479356727867565416851634122553652636651643 +432156464137132577234667855832676688887248355268488234123417164622566881332671191843581546592849575725275244782785858366414755773656324264133 +551464516464511554422385521365384541381434859833793596874996291628979754892985734686634596323198682152247886234563546786411567151524623534122 +366112116455517225155874247221226333335451623281973364111534395555972889358474285948774655924329698715148417148648632737485141431766625164454 +662544232277377442263282444643327369337171689533282192695379324776937993248887632979557512139316592313455345648511367616424546116645355355313 +511256423112223675165887384438287873257635381458133948845766724475995789865372428732635939632181875822342451666283164571363123774576712526222 +666315646667772654733466328213682513169781545626164443952596342522878743266485893346849822824333823931789954623781875282773156211747474761635 +343125314751561756724843428274745464735561748524342378477478993899682842782756962544936379579361494173617321521237763254873684232725377421623 +111312517362426268554741765164354535649924693765944393273229944379753392772332227864787582278277573429688248565235872766123457635611136664216 +464371741656346762452447864442264721273229235793625257848545973842855685594658859728758594354877334837137768492928247264842455644133216252144 +526615176447633641423236684433938249961623397993492326853543398474695936275535999386295382548645326527283871698255753323471281762757476234542 +512754766337517755857642718158147551579842857266723889447925527293369573984665246395246374736644824899715254552766577824376414686312766227174 +461426356613113512483683656651258562367946759333957779976794773465679795539493878736997824956975628772236673141344423237327163422733166352572 +146172652764747487812618173763723135644994698593828263869833386888565933359483953337685894299587386222518729294761233738553532852154475721272 +252244477262262885626824216472653577485858934783279347874699989893937744948645834639252846379284679365168667751693584536824641534267315314214 +222344114655357182316865678339419696697948854542359456594847667654555694888694898789763522684483267832768131336914537823738554112634664135151 +254234772552433284667548436621794134521458467992843567725979868546939687657777673756653994685978335248973145446649185867275113488363174736173 +477561514444126854348374881334354215239293275669824824799669357437899694573863876667889394926223597652929734262882729825631631653277571231113 +331335736541262652323717416471248516773693377634556397894794784873788783649978435338887856538265466624596489834462319364533111435363434363422 +135547626452256587164553957899147682472695693579368497697737953344369976639397659676355875854795957954957671763772693255271823573471563636765 +341672522133638487148413533818731149396496475545957775894977445975656854669844364593639793986782632756845765486685139573717773428257514371742 +376454756441448682383667515287688249585965585569766356633469974854876553374863486445847475446392562993477965652449458794667241327541777176135 +126437354275153736225692821872111898433385247673543488635437889454896637493967454758934457447943563385393877317286244331264788624422366441563 +352266446175478864568129886616427725599339338994479885467735849556564688739449734483964433568935522663972548752148789779548771462855852233144 +332252436232334761542889666164164257428765778635947387777866867738485448598467758446854869785657883567742253228498667452126822622254563472771 +143336542612575453571985915297855865924243837899584443566677659496857498989577496753736694469479996552589358237722578599521473731137786316364 +445675353363252831634462238899254375528353983699477683786793555956575599485584655585884388357367765599857346964147813179845486255866134445244 +175351635785364673582695267794642962644889786679976587768776465854886786746749856656738693653593379259388522243538315735458658543564335573127 +433775565246713348431194884714834235377854595383395933593569764567894879874896685646668589545574956382326729869399326643888557678175426343616 +217314578225252744148367429884277863723848754439567497463489847867484678466466794477864334436686683664745275343535614266126638283641532224462 +346113415457237373632116422873469877384962549463983966396588969687548949455479755878776759989779684566353863863676357161481285522821615462646 +736243688551124422611443287582842925573282883459877687869884695968845675698795647589749589935669998432547863426667668683273577135771545527667 +337457762233425753792916514781975296675887356537688599775859897697497767845964964698465448538873585849294484462597371659376853517846724654145 +537433736458225551726111358459638783993864546997449377497466958446695954947696756767499466986379974444979842796225655288914241631485567763452 +476264544833173727992234231695579369764925378878674788695856455794977745894878765686686878544447953354833388835924789666578558314518621217546 +125551752643666378668347653556694685958373337446349698486869895798459686658565478544855756877486958447977629893722471735234376724857776671533 +156676118316148122428368343647244982479694938479835645954656646565876986697878694497694746768539537897633424676822366235963475365775775472642 +314172724851613751429743759568779467466796954856857677465497746675865575556967988694799859465483536875952729644669944367655928172117851515173 +432614655575152358115612243676954979784866596494974789946578646585657756677878794794675559488637396854554892559367232113284896672675366323242 +721266366556252629417821447273699254827379363849584444657998756767595985575877889785655966654635784487764649956448988643733466424452561333251 +532131343136768372193561954965849884976983655464546758757489656677696769695587789849655689494339858539577245748642682772462778435217624165257 +456377314221423743986527351297979426554356659365358849669658867885879656757657768877999959459597449789532785288367862985852416638343832215162 +625212648454463858878515955182746772263675386343678456459575859987785759979555995847787549448336884648678283856423839533381734612685211353364 +316715532318635777982694226877492976544978699874999564984746785656978657567885976679554556898564463645989364988576346691922731171633138214555 +266323615713651758818343678134723976633665959453647775875444989578566786788685968885466695597775747895589448224334235445911734552527517436447 +463644411178561756471999416439732683592564684878956566556978965896786678776957777959695665845433775553745727563634917239571898668463478362466 +125216515625612734788956456292636763443374583398734958788567558866655597977699598549489485897677493689553633943737876794412663724756712356627 +211726378444314579525264839798793393737864355784437686486568685688898966878769965748946446665345653475834888799377486419525188536377474463477 +714644367137732257812377262939282576872775886648337666564986875776868699777777697659795867678957353979649973765387514173131222254534684824477 +124254588781214559385121843666689968245479683545598466759787577888998956877985857584559644566754386964578476269233333675341754513745128757564 +325527283123283888925883499439537678574874884798867455755668899857978659868977987997566779769445398839474447672329437195343769334612832657411 +115325286666357155393767195664229239429865344396745995696649579566576596589758657585589899497775489659499272393385762512413188722438435861631 +266765615613787285373822143849959394853994996489859786797888449585778769687896669497558958458984699448432844252765532424485723456154325154227 +413141122433683449827869663949679655728637396896349855746659787565588897656567886555467575479339876557549425567462542865796513283275114717457 +314461356788587354977217697146276329548397776874777865897559484455569786688855778845455798759436888477528564457672431579857482114678682471445 +452272118464727436939368516888689222945689384779488868546984875884769958669567665977964544894778466875633339464283915713383978145552713323737 +727166248777431734786935819755928768676247795456664797768779449959444789444446558968884895736533397485396332445956377434963496273654427666345 +765571726188365267656626658973543258877466683988873649674466984746698656687474768886667447968867369535725938658395727221758958838315524437613 +767251256786151882427722817317444878245498538399396379784446668957984559474674957775645949585553636373557942889695493336633597153632213257721 +472344412434575557617997248746476638765348547538893775897656899986465477676577675655449987833437784676363599249584847623655883478534786135145 +637324723453318286542817513599349864923655957848868567686898555448944798854645888855749997387854895535685925467465975174153976282354735633361 +557364251887623288315612696445479636294536679756888536438467669554675687574964885774767489575555663467638253858693767856394164335442317437571 +725447571483715356728584786985489285673933264789477533999455648744664765646745756687649569643574545886728293968454656195183115816872767652527 +545721642186384855118373397244844633863883248988384795986494649995896655568956588997433379438945964375354232434466146181961635151113745271513 +461334265642484316135171839848499474524822258957476645863546948967949687669598467669475866974684799985952667264365967937581216417765356244126 +661271631265165828221257459723518772695599275558734688454689478959554555769456686594795749443658762644842349979728235782321385632253543275647 +557223441583612435156243285383737998332259694396487535879937676768956858497577885445835338973399794887345568635272553125848311812118327446733 +562166311342685173632442147274753947683237568965964566497793897989675375596683958993564449593465257449978434267975222845158774571285755377225 +364512322162212682662798311721339988898824355993967736768753439734644565834879336645739783837495848394954356537745486639351753468288524771556 +475636631244737273336542424689595872499569323956397544398564573485377355587994667574585766934653829736956298955458949199472225887656172761421 +214713577472285724313827918528167959777426225698669596955787988993657497897658775953864758364294727963597644295854649151487561277273353611722 +361235466635216765667535981982545894828855853924569347983766844536788364753843595734849836329833532773895278662676334125833366726186561224775 +364633114576377245185517737446389861298584686434452789689867883733663993845934664577333896524726727955837771813662327193516781232282421273723 +762345311547556272523887673827256811338924623672424437843847973884539597695597789843544842973755266555583943322161719472775818854641171435134 +434232544624326627143511634742949297552953765373489838526586399699333986474939568336894594585387966872525848429134241261845782473116314523223 +422553517567143481334242437145672843234888659344287879757296363768499759976697457637433996499968842747367769717842584865136421662456353165665 +153747727531272356838152172726738473167948296458763389893592476839839965578976859384646222287784646386152158424588447327154648583447611361567 +146214356514126712517511473275134446943743882793668393733525379677374634376336735939536476765889824244986245519429547472247283763443545772766 +111467415546417566711143568223363131549248424393897595374556862643636824786637222926836527624339546449584731878331184281737318787244522142516 +135163732627773752882482868126882293492742318568984683865965994879592743766764898494698299652667756421671192422446517668271118616475561625632 +255473623141455423168686742748137541699882382627666347476529746997874277875755423637556862679343875778952344177961352255488675845545317164532 +434121311176142368267741258338348639743925427714449264396782274983727372582772483365384259435464742343261878999532441847482723133772311157164 +345336734261565557581428538544475924459734491472667587625323942455332824934664747952865823696936998975132493694588258721858361357777126157415 +523225163212351767244743521381759693375172143337586234999859979646442226622458743377389823356114176754461394235468568564643365166352155577346 +653642535762537614352167416811834317352439137971182797242953685872846282425952973579676833999238328799137448633746817834472245724753343375436 +444613332175234447174376732887274485138452277285217598939968554946872577877725865354978267262421455927212348466736854357731341132467151662433 +555115512761657341615252511168718871697379348434719861249574649868574423662582599342244479353927573994966352435216735713564545773523233145415 +611342632474322655753581541718552633836197762642249924291437943497282265686283887457148384146857823526165223663684661363311146356724517423555 +231433646442753166727156668251361167273823453624663222267379817465749234249775972474657873316389932263893684835236264264157374237457455232566 +226661534224516441714427161671645212736357453636741664299784328115811994146277326678765232584957911842778622287511242157243512215355776615135 +411215633422155425651727874381237654541531633143125234382643493646441922486465783641348819821676858522527853666653244567641243721547122444424 +263165131242513547361321184625265873852765347361871811222529242111957493623215239526699242954151311172288126511785655177761332245746354511643 +153144336414226122542651764684868281756452232468183559359897134872721719862141497387654787945881568683427544513142366445466244536333264614631 +543361253316352431226553437436863833772874464449594162447597484692619846952369629391232523586241346621514444824218172253212146556656356615662 +645123513541646142625266156424122862852365181544671959537985179929834833159238355415652463235625783883818434736753132423254747446122325256361 +232536623531326263246371221757375233784551717321446661422358442318319874417265257195113585736731553758518558328453766472632537134331512514614 +245641113521152313164743222756332253816434767886728574887145995928319137245636773947134612877161636588468555663346656776417572641516415514212 +255563453135355775765312324513616658532268717323835836285883163698937293554337587613652735515121356673121458248235345474733753625455442566263 +456112456115133576642377464534222517844327624118623677843275373344483514728922583367738755387828323523317755723111252273157764522316554225531 +543335116626214565331372542212574237423642262328836438374676127112314881671334547825737248117532547817735153327412572257361413425546211242364 +434434635665312446254252574753324175474142254565235127685831756817864417767467743144816625788837553462685134367247545524275473246543244221341 +123522216224264124251662531534112656416885342128615744115565442848137137652126848351272638321163578248671842261655134424235734525254136653652 +241453441561114431452324766432273214361672814412422654651245286551746543237477622776214344588747246112583732564376273626764623156643226524413 +231335326164664124634563676551613317347771887772464643221742682841335143536223284717657373714175316138566312312374513267146332451334311611442 +125111146241131446456142654443625177757112185562643375262252238216873563415577754658463766251212817151323614177267737153614134544255411331352 +453155425465245263316413213774353547464516164661672135516834511181125583841351265286817134771666863757556742134566257521155536234243341352532 +211445111432334461266555453513261143167312323327131161652863484415171444284662863644767781862685137533222552611573536464314352355445363321424 +221333254353315115252362251214735355761437161561145235878186536844348268741375246518652276636616131655456323652617625645215563343666333445331 +454332335123241424616656534213743626512415445316742427536277368261734584253281716456717754372442537612643321274322516131515525612213343124215 +133451151243456152345221115126216775171476163657575636312747258165744263885248247534114312611142455672414541753724122331343646153511312255553 +155441255441121324155415121224222733577343362277623333313622235723711752132772127121612715111533424723425426114353531116136623265534443121421 +514322332324333166114131462312425546272146531533267551574413647535461464571272355252255365174517324454111427433514154362356124641121441421125 +321325352114532246162546322361652553651142445344357435753155732552465643172166325751155655331561157457656635125436651461151643121551115311122 +423422134554143253655235661356254463413345544435431564612437222446444472647274546546232755367527716771616463354625535626222115543255414544253 +112515335221413545143126313163342254542323473516714755675771426135573376356345211326153347725226233733645632223253642241635145515252554145124 diff --git a/data/day17/test_input1.txt b/data/day17/test_input1.txt new file mode 100644 index 0000000..f400d6e --- /dev/null +++ b/data/day17/test_input1.txt @@ -0,0 +1,13 @@ +2413432311323 +3215453535623 +3255245654254 +3446585845452 +4546657867536 +1438598798454 +4457876987766 +3637877979653 +4654967986887 +4564679986453 +1224686865563 +2546548887735 +4322674655533 diff --git a/src/days/day17.rs b/src/days/day17.rs index d832586..7bf032f 100644 --- a/src/days/day17.rs +++ b/src/days/day17.rs @@ -1,5 +1,208 @@ -pub fn part1(_input: &str) -> i64 { - 0 +use std::iter; +use std::{collections::HashMap, ops::Index}; +#[derive(PartialEq, Hash, Eq, Clone, Debug)] +struct Board { + data: Vec, + width: usize, + cost_map: Vec, +} +impl Index<&usize> for Board { + type Output = u8; + + fn index(&self, idx: &usize) -> &u8 { + &self.data[*idx] + } +} + +impl Board { + fn from_string(input: &str) -> Self { + let mut data = Vec::new(); + let mut width = 0; + for line in input.lines() { + width = line.len(); + for c in line.chars() { + data.push(c as u8 - '0' as u8); + } + } + let cost_map = vec![u64::MAX; data.len()]; + Self { + data, + width, + cost_map, + } + } + fn height(&self) -> usize { + self.data.len() / self.width + } + fn prev(&self, idx: &usize, dir: &Direction) -> usize { + let m = self.width; + match dir { + Direction::Up => idx + m, + Direction::Down => idx - m, + Direction::Left => idx + 1, + Direction::Right => idx - 1, + } + } + fn next(&self, state: &State, direction: &Direction) -> Option { + let State { + mut idx, + direction: last, + mut repetition, + } = state; + let m = self.width; + + if self.is_possible(state, direction) { + if repetition == 255 { + repetition = 0; + } else { + repetition = if last == direction { repetition + 1 } else { 0 }; + } + match direction { + Direction::Up => idx -= m, + Direction::Down => idx += m, + Direction::Left => idx -= 1, + Direction::Right => idx += 1, + } + + Some(State { + idx, + direction: *direction, + repetition, + }) + } else { + None + } + } + fn forbidden_dir(&self, idx: &usize, last: &Direction) -> Vec { + let mut forbidden = Vec::new(); + let n = self.height() - 1; + let m = self.width - 1; + let col = idx % self.width; + let row = idx / self.width; + match last { + Direction::Up => forbidden.push(Direction::Down), + Direction::Down => forbidden.push(Direction::Up), + Direction::Left => forbidden.push(Direction::Right), + Direction::Right => forbidden.push(Direction::Left), + }; + if col == 0 { + forbidden.push(Direction::Left); + } + if col == m { + forbidden.push(Direction::Right); + } + if row == 0 { + forbidden.push(Direction::Up); + } + if row == n { + forbidden.push(Direction::Down); + } + + forbidden + } + fn is_possible(&self, state: &State, dir: &Direction) -> bool { + let last = state.direction; + let num = state.repetition; + + if dir == &last && num == 2 { + return false; + } + + if self.forbidden_dir(&state.idx, &last).contains(dir) { + return false; + } else { + return true; + } + } +} +#[derive(PartialEq, Clone, Copy, Hash, Eq, Debug)] +enum Direction { + Up, + Right, + Down, + Left, +} +impl From for Direction { + fn from(value: usize) -> Self { + match value { + 0 => Direction::Up, + 1 => Direction::Right, + 2 => Direction::Down, + 3 => Direction::Left, + _ => panic!("should not happen!"), + } + } +} +const ALL_DIR: [Direction; 4] = [ + Direction::Up, + Direction::Right, + Direction::Down, + Direction::Left, +]; +#[derive(Hash, Debug, PartialEq, Eq, Clone, Copy)] +struct State { + idx: usize, + direction: Direction, + repetition: u8, +} + +pub fn part1(input: &str) -> i64 { + let board = Board::from_string(input); + // for each x in xg we compute the min as + let start = State { + idx: 0, + direction: Direction::Up, + repetition: 255, + }; + let goal = board.data.len() - 1; + let mut front = vec![start]; + let mut cost = vec![[[i64::MAX; 3]; 4]; board.data.len()]; + while !front.is_empty() { + let mut next_front = Vec::new(); + for current in front.iter() { + for dir in ALL_DIR { + if let Some(next) = board.next(current, &dir) { + let accum_cost = if current.repetition == 255 { + 0 + } else { + cost[current.idx][current.direction as usize][current.repetition as usize] + }; + let next_cost = accum_cost + board[&next.idx] as i64; + let current_cost = + cost[next.idx][next.direction as usize][next.repetition as usize]; + if next_cost < current_cost { + cost[next.idx][next.direction as usize][next.repetition as usize] = + next_cost; + next_front.push(next); + } + } + } + } + front = next_front; + } + // dbg!(&cost); + // find minimum in last index + + let minimum = cost[goal] + .iter() + .enumerate() + .flat_map(|(direction, slice)| iter::repeat(direction).zip(slice.iter().enumerate())) + .min_by_key(|(_, (_, cost))| *cost).unwrap(); + // + + // let mut current = goal; + // // backtrack and print index in map + // while current != 0 { + // let minimum = cost[current] + // .iter() + // .enumerate() + // .flat_map(|(direction, slice)| iter::repeat(direction).zip(slice.iter().enumerate())) + // .min_by_key(|(_, (_, cost))| *cost).unwrap(); + // let dir: Direction = minimum.0.into(); + // dbg!(¤t, &dir, board[¤t], minimum.1.1); + // current = board.prev(¤t, &dir); + // } + *minimum.1.1 } pub fn part2(_input: &str) -> i64 { 0