Last active
April 16, 2023 15:46
-
-
Save iNewLegend/0483e11511a903849d55d571977b10ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// noinspection FallThroughInSwitchStatementJS | |
/* | |
- We have a NxN grid | |
- Turtle initial point is bottom left (1,1) looking north | |
- Turtle movement is one of: Forward one step (F), Turn left (L), Turn right (R). | |
- Grid has 4 directions: E/W/S/N. | |
_______________ | |
| | | | | | |
|___|___|___|___| | |
| | | | | | |
|___|___|___|___| | |
| | | | | | |
|___|___|___|___| | |
| ^ | | | | | |
|_O_|___|___|___| | |
W = LEFT | |
E = RIGHT | |
N = UP | |
S = DOWN | |
- Implement `moveTurtleAround(movement)` - Input is movement string: "FFFRRFLF". Output is point and direction: (2,3,E) | |
Examples: | |
input: board size is 5, movement string: "FFFRRFLF" | |
output: (2,3,E) | |
input: board size is 10, movement string: "FLLFLL" | |
output: (1,1,N) | |
*/ | |
/* | |
* Instructions: | |
* If you need more classes, simply define them inline. | |
*/ | |
function getNextPosition( currentPosition, newDirection ) { | |
let result; | |
switch ( currentPosition ) { | |
case 'UP': | |
case 'DOWN': { | |
switch ( newDirection ) { | |
case 'L': | |
result = 'LEFT'; | |
break; | |
case 'R': | |
result = 'RIGHT'; | |
break; | |
} | |
break; | |
} | |
case 'RIGHT': | |
case 'LEFT': { | |
result = 'R' === newDirection ? 'DOWN' : 'UP'; | |
break; | |
} | |
default: | |
throw new Error( `Invalid currentPosition: ${currentPosition}` ); | |
} | |
return result; | |
} | |
function getForwardInstructions( position ) { | |
const result = { orientation: '', op: undefined }; | |
switch ( position ) { | |
case 'UP': | |
result.op = '+'; | |
case 'DOWN': | |
result.op ??= '-'; | |
result.orientation = 'y'; | |
break; | |
case 'RIGHT': | |
result.op = '-'; | |
case 'LEFT': | |
result.op ??= '+'; | |
result.orientation = 'x'; | |
break; | |
default: | |
throw new Error( `Invalid current position: ${position}` ); | |
} | |
return result; | |
} | |
function moveTurtleAround( movement ) { | |
const result = { | |
x: 1, | |
y: 1, | |
pos: 'UP', | |
}; | |
movement.split( '' ).forEach( direction => { | |
if ( [ 'L', 'R' ].includes( direction ) ) { | |
return result.pos = getNextPosition( result.pos, direction ); | |
} | |
const { orientation, op } = getForwardInstructions( result.pos, direction ); | |
switch ( op ) { | |
case '+': | |
result[ orientation ] += 1; | |
break; | |
case '-': | |
result[ orientation ] -= 1; | |
break; | |
default: | |
new Error( `Invalid math operation ${ op }`) | |
} | |
} ); | |
return result; | |
} | |
console.log( 'FLLFLL', moveTurtleAround( 'FLLFLL' ) ); | |
console.log( 'FFFRRFLF', moveTurtleAround( 'FFFRRFLF' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment