diff --git a/17.1.ts b/17.1.ts new file mode 100644 index 0000000..56c5abb --- /dev/null +++ b/17.1.ts @@ -0,0 +1,117 @@ +import { write } from 'node:fs'; +import { open, writeFile } from 'node:fs/promises'; + +const path = './inputs/17.example' +const file = (await open(path)) +const input = (await file.readFile()).toString().split('\n').map((row, y) => row.split("").map((e, x) => ({ score: Infinity, weight: Number(e), x, y, combo: 0 }))) + +const file2 = (await open(path)) +const input2 = (await file2.readFile()).toString().split('\n').map(e => e.split('')) + +type Direction = { x: number, y: number } + +type Coords = { x: number, y: number, weight: number, score: number, combo: number, direction?: Direction } + +const openSet: Coords[] = [input[0][1]] +const goal = input.at(-1)!.at(-1)! +const cameFrom = new Map() + +input[0][1].score = 0 +input[0][1].direction = { x: 1, y: 0 } + + +function astar() { + while (openSet.length > 0) { + const current = openSet.shift()! + if (current.combo == 3) { throw new Error() } + printOutputTo(current) + console + if (current === goal) { + return true + } + + + function processNeighbour(n: Coords) { + if (n === undefined) { return false } + const tentative_score = current.score + n.weight + if (tentative_score > n.score) { return false } + cameFrom.set(n, current) + n.score = tentative_score + if (openSet.includes(n)) { + let index = openSet.indexOf(n) + openSet.splice(index, 1) + } + + let index = openSet.findIndex(e => e.score > n.score) + if (index == -1) { + openSet.push(n) + } else { + openSet.splice(index, 0, n) + } + + return true + } + + if (input[current.y + current.direction!.x] !== undefined) { + //Right + const n = input[current.y + current.direction!.x][current.x + current.direction!.y] + if (processNeighbour(n) === true) { + n.combo = 0 + n.direction = { y: current.direction!.x, x: current.direction!.y } + } + + } + + if (input[current.y - current.direction!.x] !== undefined) { + //Left + const n = input[current.y - current.direction!.x][current.x - current.direction!.y] + if (processNeighbour(n) === true) { + n.combo == 0 + n.direction = { y: -current.direction!.x, x: -current.direction!.y } + } + } + + if (current.combo < 2 && input[current.y + current.direction!.y] !== undefined) { + //Forward + const n = input[current.y + current.direction!.y][current.x + current.direction!.x] + if (processNeighbour(n) === true) { + n.combo = current.combo + 1 + n.direction = { y: current.direction!.y, x: current.direction!.x } + } + } + + } +} + + + +function printOutputTo(dest: Coords) { + const input3 = JSON.parse(JSON.stringify(input2)) + let current = dest + input3[current.y][current.x] = '#' + while (cameFrom.has(current)) { + current = cameFrom.get(current)! + let character = '' + if (current.direction!.x == 1) { + character = '>' + } + if (current.direction!.x == -1) { + character = '<' + } + if (current.direction!.y == -1) { + character = '^' + } + if (current.direction!.y == 1) { + character = 'v' + } + + input3[current.y][current.x] = character + } + console.clear() + console.log(input3.map((d: string[]) => d.join('')).join('\n')) +} +astar() +console.log(goal.score) + +await file.close() +await file2.close() \ No newline at end of file diff --git a/inputs/17 b/inputs/17 new file mode 100644 index 0000000..ae372f0 --- /dev/null +++ b/inputs/17 @@ -0,0 +1,141 @@ +122432413514514224244555535635323326624553553663656334554364635433356564366445465776535555535366263456336252245622346225565543545134554512112 +324132123225523666336255236264436632434462342547637735775353537573763745674756745435373635563763542656666323565452635246562526245251234442513 +441232244311136666465364336563663525622363457637473556773537743444554636566374347436537733374557743665434556625253453232565643523541553553212 +211234322154343442552633555444535234564544764466766476635747554563755334764734467357664465576555446463644622225555466646544643555514453153213 +334152142422346242643446445434533355545566535743364737644564663353734776533636777764575353344647365473554666556252264262536563425445455243151 +244355233244423635326522244535665323375467646356377573635573463336676774453365645333337753547633737336453356545524445524265325334252535525542 +411522534446242236323636264554632667373664335467763347444636364673433645457646433774633556566766575766536564636334644433233224522234332312154 +445545523524652635342236653226466674743573364736545753557637457335636376763343353336334677744666443344335533345566635463544542342422536254532 +424124536443264532325442532625455566465746534657445347357573443537553453645475544377537665464433437337433655666746225564445235645262646513432 +412132544634453435465626453636476334663365467473533373563645777346566347454754665533434536375655374537434556637573522546665422654236366251421 +433214454455444535264426336447433457455333365637743344456473454344374663555776357556666453463443453553454336465746423252364535422323324522515 +122554622333643362343222353634575544756644553357344767347454463756636536473635557355644637637535376373533343743356454625363335422234246355352 +343122645256525536455565476366633573645447467763756373636465588447675486667576567646446573666564645334436737576373335436262256663432653562344 +131335463322266555224566537347333465437765647433435545454467674667867585447888446786888436664767773753763665674647435573526653352533235626251 +146222664355662455555537647766477664365754655375357534767857577844654844665858787456568588663555553566474363766334566546642563554436525532664 +553336324333252234532456345534353434657567663334458847674775755844554564846764787676546546856733436556577337655657656454465346523254265442426 +456434542333642322336376765756447775455756667476658884878458678657464584856784745666476576747773475545336545666655454653652333626365463435524 +544333624444435465353335663534534367364643343388646674564756758487668878687654587655776575745557657455777543736447445756467534264642645256434 +352624462462546356377345435756335447335646547774567686867764576476555655576665585664647564686658676673655736363767573553674524535335326336443 +444646564345325435377733353466433454637756747546768787786857486587858656864648744767877545885577777854365674764743646434434443236353353636626 +542256542446564557756746677346374356744667565568677445587464448846585556478588588657584567454864576868865366745345354573575736466662435622635 +626455554262335654635763633345355445775865864488646555768684667588756568864877555574777687456565685888445745445663376765435337635632565262644 +522455325542665363644364446337366637375686465756547578465755588475767488485655664864468758886846766448844633575745535734567673542434552666656 +653623226546623733545435433677365756677685475446786655575755654854486785765566654465776646868876648646468776656543756646334366347633466245323 +334224253345564764753534465743753345487677745444758644585855455444846574874687456858855787768578864778886684654557634543545755654535424235263 +554626446424777365445744677355664858647866457884855685487484664454484784556464867848678574884568545566474765447674334676554457647432623523464 +654433252523765474667345764663438854868466464448657686748455654657684576854748446558868648444575656555888478774465334764477773755574426644253 +436462223464747463643463464753356557574474674568786674557848679975897776595586976887887654457564557755655484844456556334644674367334354343362 +233666653355443464653444447434744566767747456675567754854578599975777586759879776958646576487848467568544754668653335433437554446335753435452 +366262444356573553564676555537685457756844886548748465879876867875776759767586855566556746755878665867686465775487364334553537546637466424453 +225322533364754573465435446375657757545867556767576577776588559565795588579996765595959989667757548447655475665465543564633656344474765636243 +536543253775774373436666443755647448448644568584876996885655667665698778786558677866855857578548468874847785848474776744646375644365673556525 +424442627667554653673444576467778545558577476875577599886676855995675698989577788999557775866556658648664478755545687633346764744364565643245 +465354266733645666766374676867854758844875768678565656868585677789885965598897999767778797588697784765676454456647755746355343474766636542654 +644246765746476333744434854647485667567844784757677868997966898657687857857776956889966857768978684476554577478867656475665635777765465743364 +442332554656336473676353744644756558654848565778586557698558978679987559957588786655586868975967955746546555644644465585467334565763757343643 +433553664646373347366747655775866465776485486776969587766779898589659657999795976968665998699789678577644886544777587856745447553553547757565 +525637457466537643645567476744647484788545658598869787798885566875858965889659689875875665757766756784566464856586588446634745353345364335636 +552674644557566763667475647887578885866749688585989787687688999598959555565669785667697795568567679965788555875765547846885544375554546475625 +655376735757644465543757456844777767686485986698896986769675677656899675675556967685956696559589977998767468757548577764777535754554465346525 +543666354766367336346488755565776485665655779867778866868798697687988556879779898965956569765958675879656455487774755474754536636776337336552 +566737435567453667778876485576865584859858888957865579868999888777857958875788959567755986679566795867798584554447544586864747745576433535534 +546634473457544776344754785876876586686688778688878656687576858898798866968898868676859667977865857689687848758448578568656544753343477547335 +565477753353544664578884846858684586776776567689658586579769579777669677679977986776967996668698779789858558747854446446547475754755333437534 +456674654456775734844565548746885746557778889566787588686986979696896677987766666867698569759556869767577655874578564644877456437477647347556 +264645656347744563454554877555656789867775659578779689859676777989687868979879978768968965789786699955698757574744484564657547566577633647657 +375745364367464378747457857867457786686669857978875959779886866977868789778866686787886985659997966786755776777474744686675684347373644357336 +655365366733464635467447455778865678785978678567567867976899689989998697877687666977798797855655578865567575978856554667878464344573437743534 +563546677453664474656688457766857575798558857759577598797977677666689777996877968687867678865789669669765688957847758478458486473753573475744 +477374453364674488487744777477459959586655797887776969999976788767866799878688676698688979666577895567999556769687656777575847876675445637657 +633464766376664544587568886454678996576879696875966688797979668978687779678768789677766778976659879997578798697776754675788448467736564676565 +774534457373633467878847866885899557968758769598987899879886679896998878968686988798888768667969659555996597595678575674454667585774534336656 +356654357765437468568677785658465679589989756869889666697778879996988898798988687799678687999799559695966788986686758788865786787654344333453 +577366353566576845687465676885866899696578888579987866877886967768786889969687699786788877776765686757755686888766474754865688858765644556556 +773647345673447856548644775484569765987788679988686696786798976797767697798766699669887667879997559976585585696655557467764746444436534357463 +753734635764665567685664586774577897967956876797868898776868986998777889666968769869868999979678885575865588657657767448444588668577733436347 +535534633674364444777478464788779897579965659688899867978897976896789796767898786977876997689799759797857779955689686576446584687855373673337 +437773436457476755664675466645665786978665865769887987697769788796987797779899687996886686987669998877988859869569845568478566687746355657766 +435635737453455765456446845479677779769676787967966777879896678878997777997989899768878698876676988767688799758677765884568766468564455554377 +337466747557564868555855856485677769798977969698977668997999978987789989778777998889668999977766667656996788995578764645854448485767343675446 +563663673677448854876884778669976865796768699967687789986896988987888898998999798798679788896778776987659676996775878666758775548854377676355 +477634777366586448844684487665899595679998688778879789696769888778897988979897887878677686799686867765596997755857984688446484564753556655453 +733365367676466477584476848588788696899778698689866969977996977878797977789787887878888768777668968686567757658595756884667758776747564737643 +635435436666554585768475588557665786559675696976789867997768979798788978978978877897896986787778867685979978798697786747744847477673457656637 +453673476475646868878454868997866799976866986987698797798779877979977779789877879988778668798768778986879657695856587584588858477455675356637 +675475434453478747684584475856698765975568996776679777699878888989777799897887977887988869779689689786898757888869686575486684486566355333757 +347473476453664766446547686576975695676786779968867799799878787889887879887997797777798767678777967679889586755658878574855867577685756653747 +677336674635875455476674874876867899996787679999976986799988878999998789888987978777778777777698967699875575566866996865845557657777556735453 +443767775435784768788645477688785557779596687787896766696798978898997979779798797999997887977686886997885598975598858757765878777473444473335 +446657743376567876475544455669985679987695689889799788879897789797898777889779798988888979777786679766557668977867877756857568478853737763367 +546566556344568455575875477888569599687995879999677877786997799999979999879977799788997679878997976685586558976775687576545865678864744543353 +433474455753465874784567448976886695886665968688679988896888779977788788798789797879797886998967897679665559579658574654846765767587666344376 +775475557733546864854558485596679868885967798776666896669998798777979779879778798889896797867798886687769697576877694486874577556555365767357 +655433576637878666788577485585669968585595699968978768787988999877978979979787879999988868668888668777878788969969678468558757676686457775677 +345366376635845567458445788875988998765575787868968969698999778878997898899798879977786678988769697968959989956588998567674484775457336443464 +374336453554877856768678646868659779785766969688799666969999877977797897789978879878787777986976898987997675977965755567567557555473744766446 +553567477434554456548846647696656778567785666668676869698788988999988789879897997899798789768697867677789598558666668568547586677546445666564 +456664556676577846686756745596668759678868997889987687686897998999997999887987889878888786868798897665995676875869586567754788557473633766464 +755773375666876874467778664968887789698877789867997677676897989889779787777878777887777886989666986785866969888765687676574546548747533456656 +344744657567887475676886775577657897967577898996867768779777989799987778779779987998798989899788669968666597969679857485468654457584466376775 +347546444347674848665875876888999557679966787969897978887766798889989979978788779786987698879968988688757895959769667775747648845684737347374 +745463473763767664654876445686696558666557968898777679677876797778778879899999898986987989798697668957986855565999574557664867857865743764753 +747675644376755887667684558568796758968875686787888668776978677898799777998978898996968766767889866989578979676566544866886766858863766533743 +734633333465448454584687478787887588989676589789767787878889879899888779888898898887886777877779869756687896899965784855774677644844457667674 +634337376747367585568648458756668657796668578778686699967996886697897997979999777697669778977786998976586989995688586787756558745675447537334 +473756443465358456676577687875967768888597899969767988967799878697799788777776878877666678688777887559696696856695466486764575848775546765777 +537537736677757446445645567485576999866598778866967966969898869766777976679888899679776876886978997855997587988795568578464567757844667455646 +664474556543434554545645776864858569877787589596668969677798798777978967778897867798896667696696677776877998697856686654645887568536755373376 +346763543553446858664566874848658758859777776897998767888989779789996878687986888896986767788886596969788579976966466446575456778656765446457 +374737544477775846757446887885795555879566895999879767888989688778897898668979679687989866666797776569688997779968674568566485856334334647767 +335767747667345754688447875868669785599586989657869989798668989776869896996677777998897886868986976798997759585657847455554485577374463354675 +563334573347366665855858745667865688655866958678878767798969997768698696967799688966776768997769555585558557878875875664557876464577633566356 +454475744346774764844575854574556967856565766757666987667667879899678879876997788789788899976869987778566779899654588668765745657764757737744 +756466343556354686587454565576785865795785797967775786786997978766966868986986776686678887979897689788896789766588456578865646574745447335555 +655347477333545645854667866765774898565558667857596887789876777989769868779877878778879796858557659677799665786445876748575476556664435443537 +357555775545566774558446667868876565588658687669687685766878668799796766687866996879688769866867976885997678548774445456755447444677666666654 +234535466776444738565447747786448598696589966685787786689686888788679966879898688768979798755668876775867766856646776548886854543543366675347 +656535667557476455844876657656685855957756565886988577978978798667669879676998797668787955697756956857899877476878555768656863333336454457473 +655373776555673476584547447748657884999656785786896886768866766969976878769778666688788555588586879777755757475778568888865655543357733765737 +667465655566767747444666876747688456579798998669966565778698688866789977767969788958777598889795886965858588477587658566747545636553343773457 +235365646647653676576464857746567578569999979978679677575577955896686676996896759996998967855566758978898975647684576685864847773354546734766 +635635547465433353565488754464848856649669877668957896859555976868979877898599799888565898665675666996599847846768658546777346367556565646674 +526454746644354536337787575746866576855775568896888858586696965766688978766578567588877565999968567996867556448586868884646537574754344573474 +323374445736343736374555754885548546668887989859556685758757687969565786956585665956989875579889579859548577486848447888886347456655675745762 +536554733547343477747448576666884778668448678799666756568575589666795777755556795778756558957866679578684674455747647785744644455653643664664 +455354763745566556647366865744587584677676568586788658795759599856758765976555998996777967795988885885468576578654647574777375476665436543524 +636563454676744333337337566458658878875446575889899997859876655859659766859959888789589897565895857984857444466556574778575357775447775557644 +656245547347643653546355754487687875587475787879578959696966565569755587975779868575779765666559757676787775484456456756674753645763535356546 +445456745567757375735363787485764544845775864789759799597555658688956895688988865578799976579778555567644645456644768874353444453654667362363 +624362634355755757475437557587584557568466866444975767658559888968887965559596886587596699578668576856446468766778584833354763474537545446456 +642622263576653735767776337768888675558678448856556888588898778956887798979957767997997995579656746776554657787787664554576567473434646353262 +556644436537637475644644565488844667875554588784785655889568897998856666966957777568889758897877754577666576467454484557773675463454654256432 +662623425343435547563436635746866566848474476768588579675758767986997988895578877585765999458876567576558857568645545476534767436754543542326 +544636633436463446545643463346865765687456875664656664776578879769899995598869857989999454775684776886677455544886437556656673667745536552556 +252266436535756553656573573637458666677786885548768775578459667566859966986595799575968788844458545587887458665453445333564573435473464623254 +524423465264664534765735535337667765854485685467768584554645578596986757865999678785845447785888845468768787874757747635356375674545544644353 +664244242533656753447474765734558466856477774657647667886577687668874777667565688765576587865574747844476587857565474353345344473632325346626 +233244255434566363566336537654535888644584676584486774455578647488865475666555476446544677776756847465545565863665754766636375776372255264256 +243455665554245536334336455363467465888678787875775867558768685848577588477464766567846568467465858858667566535677666737643674535435656334554 +266563564342634475355565776356777445665546558777446558865576676764578778676677684655877877877564485588664867556555634535665666363645322245332 +356526442255322637776377367733656733674458847576655566648864575746848746477558485487558558758784845845478775346756557764366477452554544364666 +566452425354233476754576444577447456337475674876576577778687666466756588478774855454564485864686454767464733556777434373447376532466545526643 +455424646423654456533466567463464643776344775784766488545565756657845555886544644767686548485676888668767644435566656735543365235324656625543 +355353255565366452763566635474465543535364568888876765466488678557647557765677866476685648564646555666465535443655765553333655426244434443465 +666224524463563553374737556456446354453737667865874644668847587584746754844774547455745555676667478357536376474374674346474645232563265432465 +533522353524653246255536735656733577335635443748878778764785556876768487586877555466876754876478477564664463467356676777745232555562426223422 +442642656566545644322767636444445364774447675546558747547666848546447654544854857686876455666466773444445364533464466436654455326352652326245 +565236665642353363326436475664435734777555765533568774674677468445846568674587577878868476458337353655347377346356547375464323362423442556256 +214263545422452633463535477373365446754447733546344434587588578775547555856688678744754878373646677454454346745477635655443324353523266454425 +335246224255454234224345666756473737454537435645343753454587458466575786786666465756568347776575567667635777757544546635446436346262555643344 +453125625533235526534344356743735575573363655544733357375664448655787675788846564476636533354375643436775455766773535656423264432634225353445 +155315635455534356225554423573766765366455543676334353443535547767376353677567555656655446366336346673557356355775572344465444655632222336314 +241441555345225656562425264256553547463376745766454367375375565575454357555736544576353674357343373336477533775677452563355553222525446551311 +522212652332456363266236556364444733757455766456735534563777737764743347675754456654373366437746554743334735457367543243542324646335635462445 +254241433225445225623343522253355474554553653334647754664757344636535445345377346775767677334647643555744753375725236322262366364526256615551 +435312433425443425622236233633663545474636535753473466373645734344354565357364653474636557565476656453576456575244622333424255335455445531215 +513111522332545253336362644632446354555743735756655765344557334635746654644376435563365635365376446547545463745633334322622433322524563341433 +124123551424455633244454564465624443633344375363673664733374767347333544775567677467636765466643734665464333456623344545263445424653543242441 +255345213322452255335262552556442624463635444754335736437566356356436654567333356447646377666675344435744465465632564356546333424535152521144 +244412444522342465566644644554563445246235375746674773577536757366643633745344634674644375345344767535625643345624535353323233424533535233251 +143143253334524544262644435544644226252336756545454653564474677346455647365673563663665677644657777572644642422533456524354546222124312412134 diff --git a/inputs/17.example b/inputs/17.example new file mode 100644 index 0000000..3c85086 --- /dev/null +++ b/inputs/17.example @@ -0,0 +1,13 @@ +2413432311323 +3215453535623 +3255245654254 +3446585845452 +4546657867536 +1438598798454 +4457876987766 +3637877979653 +4654967986887 +4564679986453 +1224686865563 +2546548887735 +4322674655533 \ No newline at end of file