Advertisement
bgdragoslav

Santa's gifts

May 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2. $number=readline();
  3. $arr=explode(" ",readline());
  4. $position=0;
  5. for($i=1;$i<=$number;$i++)
  6. {
  7.     $command=explode(" ",readline());
  8.     switch ($command[0])
  9.     {
  10.         case "Forward":
  11.             if($command[1]+$position>=0&&$command[1]+$position<count($arr)&&$command[1]>=0&&$command[1]<count($arr))
  12.             {
  13.                 $position += $command[1];
  14.                 array_splice($arr, $position, 1);
  15.             }
  16.             break;
  17.         case "Back":
  18.             if($position-$command[1]>=0&&$position-$command[1]<count($arr)&&$command[1]>=0&&$command[1]<count($arr))
  19.             {
  20.                 $position -= $command[1];
  21.                 array_splice($arr, $position, 1);
  22.             }
  23.             break;
  24.         case "Gift":
  25.             if($command[1]>=0&&$command[1]<count($arr))
  26.             {
  27.                 $position = $command[1];
  28.                 array_splice($arr, $position, 0, $command[2]);
  29.             }
  30.             break;
  31.         case "Swap":
  32.             $index1=array_search($command[1],$arr);
  33.             $index2=array_search($command[2],$arr);
  34.             if($index1!==false && $index2!==false)
  35.             {
  36.                 $temp = $arr[$index1];
  37.                 $arr[$index1] = $arr[$index2];
  38.                 $arr[$index2] = $temp;
  39.             }
  40.             break;
  41.     }
  42. }
  43. echo "Position: {$position}".PHP_EOL;
  44. echo implode(", ",$arr);
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement