Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
// InputtypeMaze=MazeSquare[][];typeDirections=Direction[];// OutputtypeFinishState='Finish'|'Dead'|'Lost';// component typesenumMazeSquare{space=0,wall=1,start=2,end=3,}typeDirection='N'|'E'|'W'|'S';typeCoor={row:number;col:number};// Functions to move a coor aroundconstcoorUpdaterByDirection={N:(coor:Coor)=>{coor.row--;},E:(coor:Coor)=>{coor.col++;},W:(coor:Coor)=>{coor.col--;},S:(coor:Coor)=>{coor.row++;},};functionmazeRunner(maze:Maze,directions:Directions):FinishState{// Find starting pointconstcoor_row=maze.findIndex(v=>v.indexOf(MazeSquare.start)!==-1);if(coor_row===-1)thrownewError('Maze has no starting point');constcoor:Coor={row:coor_row,col:maze[coor_row].indexOf(MazeSquare.start),};letfoundEnd=false;// The closure will set this if it finds the end// Iterate through the directions array until we finish or hit a wall/boundary// I'll use `findIndex` since it can early-stop as neededreturndirections.findIndex(direction=>{// move the coor based on directioncoorUpdaterByDirection[direction](coor);constsquare=maze[coor.row]?.[coor.col];// (note that square will be undefined if coor is off the map)foundEnd=square===MazeSquare.end;// return true if we want to keep going (i.e. are on a clear square)returnsquare!==MazeSquare.space&&square!==MazeSquare.start;})===-1?'Lost':foundEnd?'Finish':'Dead';}
Some sanity checking
consttestMaze:Maze=[[1,1,1,1,1,1,1],[1,0,0,0,0,0,3],[1,0,1,0,1,0,1],[0,0,1,0,0,0,1],[1,0,1,0,1,0,1],[1,0,0,0,0,0,1],[1,2,1,0,1,0,1],],tests:[Directions,FinishState][]=[[['N','N','N','N','N','E','E','E','E','E'],'Finish'],[['N','N','N','N','N','E','E','S','S','E','E','N','N','E'],'Finish'],[['N','N','N','N','N','E','E','E','E','E','W','W'],'Finish'],[['N','N','N','W','W'],'Dead'],[['N','N','N','N','N','E','E','S','S','S','S','S','S'],'Dead'],[['N','E','E','E','E'],'Lost'],];tests.forEach(([directions,expectation],testi)=>{constactual=mazeRunner(testMaze,directions);console.log(`Test #${testi}: ${directions.join(', ')}
Result: ${actual}, expected ${expectation}, which is a ${expectation===actual?'pass! woop':'FAILURE rofl'})
`);});/* -->
Test #0: N, N, N, N, N, E, E, E, E, E
Result: Finish, expected Finish, which is a pass! woop)
a.ts:83
Test #1: N, N, N, N, N, E, E, S, S, E, E, N, N, E
Result: Finish, expected Finish, which is a pass! woop)
a.ts:83
Test #2: N, N, N, N, N, E, E, E, E, E, W, W
Result: Finish, expected Finish, which is a pass! woop)
a.ts:83
Test #3: N, N, N, W, W
Result: Dead, expected Dead, which is a pass! woop)
a.ts:83
Test #4: N, N, N, N, N, E, E, S, S, S, S, S, S
Result: Dead, expected Dead, which is a pass! woop)
woop πΊπ
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.
One with full typing info in typescript...
Some sanity checking
woop πΊπ