Skip to content

Commit

Permalink
update robot
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyi Nyi Lwin committed May 5, 2022
1 parent 41e6306 commit bc11ed0
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions romam.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,52 @@

function finalPosition($move)
{
$path = $move;
preg_match_all("/[A-Z]+|\d+/", $move, $path);

// Current position of the robot
$x = 0;
$y = 0;
$directionNumber = 1;

$direction = [
1 => 'TOP',
2 => 'RIGHT',
3 => 'BOTTOM',
$direction = [
1 => 'TOP',
2 => 'RIGHT',
3 => 'BOTTOM',
4 => 'LEFT'
];

for($i = 0; $i < strlen($path); $i++)
{
switch ($path[$i]) {

case 'R':
if($directionNumber == 4){
$directionNumber = 1;
} else {
$directionNumber++;
}
break;

case 'L':
if($directionNumber == 1){
$directionNumber = 4;
} else {
$directionNumber--;
}
break;

case 'F':
if( !($directionNumber % 2) ){
$x += $path[$i+1] * constant("MULTIPLIER_".$directionNumber);
} else {
$y += $path[$i+1] * constant("MULTIPLIER_".$directionNumber);
}
break;

foreach ($path[0] as $key => $value) {
for($i = 0; $i < strlen($value); $i++)
{
switch ($value[$i]) {
case 'R':
if($directionNumber == 4){
$directionNumber = 1;
} else {
$directionNumber++;
}
break;

case 'L':
if($directionNumber == 1){
$directionNumber = 4;
} else {
$directionNumber--;
}
break;

case 'F':
if( !($directionNumber % 2) ){
$x += $path[0][$key+1] * constant("MULTIPLIER_".$directionNumber);
} else {
$y += $path[0][$key+1] * constant("MULTIPLIER_".$directionNumber);
}
break;
}
}
}

echo "X:".$x. "; Y:".$y."; D:". $direction[$directionNumber];
}

finalPosition($argv[1]);
finalPosition($argv[1]);

0 comments on commit bc11ed0

Please sign in to comment.