Created
January 9, 2018 07:04
-
-
Save phpfiddle/a5ccea22bbe136cb0736b12a1ebdc38b to your computer and use it in GitHub Desktop.
[ Posted by Jai Rana ] My walking robot code.
This file contains 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 | |
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