Advertisement
Guest User

Untitled

a guest
Aug 16th, 2019
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. <?php
  2. $arr = explode("|", readline());
  3. $sum = 0;
  4. $input = readline();
  5. while ($input != "Yohoho!") {
  6. $comand = explode(" ", $input);
  7. switch ($comand[0]) {
  8. case"Loot":
  9. for ($i = 1; $i < count($comand); $i++) {
  10. if (!in_array($comand[$i], $arr)) {
  11. array_unshift($arr, $comand[$i]);
  12. }
  13. }
  14. break;
  15. case"Drop":
  16. $index = $comand[1];
  17. if ($index < 0) {
  18. break;
  19. }
  20. if ($index >= 0 && $index <= count($arr)) {
  21. $element = $arr[$index];
  22. array_splice($arr, $index, 1);
  23. array_push($arr, $element);
  24. }
  25. break;
  26. case"Steal":
  27. $index = $comand[1];
  28. if ($index <= count($arr)) {
  29. $stolen = array_slice($arr, -$index, $index);
  30. for ($i = 0; $i < $index; $i++) {
  31. array_pop($arr);
  32. }
  33. echo implode(", ", $stolen), PHP_EOL;
  34. }else if ($index >= count($arr)) {
  35. $stolen = array_slice($arr, -$index, $index);
  36. for ($i = 0; $i < count($arr); $i++) {
  37. array_pop($arr);
  38. }
  39. echo implode(", ", $stolen), PHP_EOL;
  40. }
  41. break;
  42. }
  43. $input = readline();
  44. }
  45. if (count($arr) == 0) {
  46. echo "Failed treasure hunt.";
  47. } else {
  48. foreach ($arr as $item) {
  49. $sum += strlen($item);
  50. }
  51. printf("Average treasure gain: %.2f pirate credits.", $sum / count($arr));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement