Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created January 9, 2018 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/a5ccea22bbe136cb0736b12a1ebdc38b to your computer and use it in GitHub Desktop.
Save phpfiddle/a5ccea22bbe136cb0736b12a1ebdc38b to your computer and use it in GitHub Desktop.
[ Posted by Jai Rana ] My walking robot code.
<?php
function diddyMyRobot($x, $y, $d, $sp) {
$stringArray = str_split($sp);
foreach( $stringArray as $key => $value ){
if( $value == 'W' ) { continue; }
switch ( strtoupper($d) ) {
case 'EAST':
if( $value == 'L' ){
$d = 'NORTH';
} elseif( $value == 'R' ) {
$d = 'SOUTH';
} elseif( true == is_numeric( $value ) ) {
$x = $x + $value;
}
break;
case 'WEST':
if( $value == 'L' ){
$d = 'SOUTH';
} elseif( $value == 'R' ) {
$d = 'NORTH';
} elseif( true == is_numeric( $value ) ) {
$x = $x - $value;
}
break;
case 'NORTH':
if( $value == 'L' ){
$d = 'WEST';
} elseif( $value == 'R' ) {
$d = 'EAST';
} elseif( true == is_numeric( $value ) ) {
$y = $y + $value;
}
break;
case 'SOUTH':
if( $value == 'L' ){
$d = 'EAST';
} elseif( $value == 'R' ) {
$d = 'WEST';
} elseif( true == is_numeric( $value ) ) {
$y = $y - $value;
}
break;
default:
echo 'Please provide proper direction (EAST, WEST, NORTH or SOUTH)';
}
}
return [ 'X' => $x, 'Y' => $y, 'D' => $d ];
}
print_r( diddyMyRobot( 5, 2, 'SOUTH', 'RW2LW4R') );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment