// Solution based on: https://www.redblobgames.com/pathfinding/a-star/introduction.html// by @redblobgamestypeCoords=readonly[number,number];functionsolve(caveMap:readonlyboolean[][],miner:Coords,exit:Coords){constprevCoordsMap=calculatePrevCoordsMap(caveMap,miner);constpath=getPath(miner,exit,prevCoordsMap);returngetInstructionsFromPath(path);}functioncompare(c0:Coords,[x1,y1]:Coords){const[x0,y0]=c0;returnx0===x1&&y0===y1;}functiongetInstructionsFromPath(path:readonlyCoords[]){returnpath.reduce<string[]>((acc,cur,index,arr)=>{if(index<arr.length-1){acc.push(getDirection(cur,arr[index+1]));}returnacc;},[]);}functioncalculatePrevCoordsMap(caveMap:readonlyboolean[][],miner:Coords){constgetNeighbours=createGetNeighbours(caveMap);constfrontier:Coords[]=[miner];constcomeFrom:{[index:string]:Coords|null}={[miner.toString()]:null,};constisInComeFrom=(coords:Coords)=>comeFrom.hasOwnProperty(coords.toString());while(frontier.length>0){letcurrent=frontier.shift();getNeighbours(current!).forEach(next=>{if(!isInComeFrom(next)){frontier.unshift(next);comeFrom[next.toString()]=current!;}});}returncomeFrom;}functioncreateGetNeighbours(caveMap:readonlyboolean[][]){constheight=caveMap[0].length;constwidth=caveMap.length;constisWalkable=([x,y]:Coords)=>caveMap[x]&&caveMap[x][y];return([x,y]:Coords)=>{constn:Coords|null=y>0?[x,y-1]:null;consts:Coords|null=y<height?[x,y+1]:null;constw:Coords|null=x>0?[x-1,y]:null;conste:Coords|null=x<width?[x+1,y]:null;return([n,s,w,e].filter(c=>c!==null)asreadonlyCoords[]).filter(c=>isWalkable(c));};}functiongetPath(start:Coords,goal:Coords,prevCoordsMap:{[index:string]:Coords|null}){constpath:Coords[]=[];letcurrent:Coords=goal;letcount=0;while(compare(current,start)===false){if(++count>100)break;path.push(current);constprev=prevCoordsMap[current.toString()];if(prev)current=prev;}path.push(start);returnpath.reverse();}functiongetDirection([x0,y0]:Coords,[x1,y1]:Coords):string{if(x1>x0)return'right';if(x1<x0)return'left';if(y1>y0)return'down';if(y1<y0)return'up';return'';}//////////////////////////////// TESTS//////////////////////////////functionassertEquals(name:string,a:string[],b:string[]){if(a.length===b.length&&a.every((step,i)=>b[i]===step)){console.log(`OK: ${name}`);}else{console.error(`KO: '${name}'\n${a} !== ${b}`);}}assertEquals('A simple map. (2x2)',solve([[true,false],[true,true]],[0,0],[1,0]),['right']);assertEquals('A linear map. (1x4)',solve([[true],[true],[true],[true]],[0,0],[3,0]),['right','right','right']);assertEquals('Walk around an obstacle (3x3)',solve([[true,true,true],[false,false,true],[true,true,true]],[0,0],[2,0]),['down','down','right','right','up','up']);assertEquals('Change directions several times (5x5)',solve([[true,true,false,false,false],[false,true,true,false,false],[false,false,true,true,false],[false,false,false,true,true],[false,false,false,false,true],],[0,0],[4,4]),['down','right','down','right','down','right','down','right']);
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's a solution in TypeScript.
You can see it in the Playground
Some tests at the end.