emotrend

Concert

Dec 16th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <?php
  2.  
  3. $input = readline();
  4.  
  5. $bands = [];
  6. $bandsTime = [];
  7. while ($input !== "start of concert") {
  8. list ($command, $bandName, $players) = explode("; ", $input);
  9.  
  10. if (!key_exists($bandName, $bands)) {
  11. $bands[$bandName][] = "";
  12. }
  13.  
  14. if ($command === "Add") {
  15. $currPlayers = explode(", ", $players);
  16. for ($i = 0; $i < count($currPlayers);$i++){
  17. if (!in_array($currPlayers[$i], $bands[$bandName])) {
  18. $bands[$bandName][] = $currPlayers[$i];
  19. }
  20. }
  21. } else if ($command === "Play") {
  22. if (!key_exists($bandName, $bandsTime)) {
  23. $bandsTime[$bandName] = intval($players);
  24. } else {
  25. $bandsTime[$bandName] += intval($players);
  26. }
  27. }
  28. $input = readline();
  29. }
  30. uksort($bandsTime, function($key1, $key2) use ($bandsTime) {
  31. $time1 = $bandsTime[$key1];
  32. $time2 = $bandsTime[$key2];
  33.  
  34. if ($time1 === $time2) {
  35. return $key1 <=> $key2;
  36. }
  37. return $time2 <=> $time1;
  38. });
  39. $bandNameToPrint = readline();
  40.  
  41. echo "Total time: " . array_sum($bandsTime) . PHP_EOL;
  42. foreach ($bandsTime as $band => $time) {
  43. echo "$band -> $time" . PHP_EOL;
  44. }
  45. echo $bandNameToPrint . PHP_EOL;
  46. for ($i = 1; $i < count($bands[$bandNameToPrint]);$i++){
  47. echo "=> " . $bands[$bandNameToPrint][$i] . PHP_EOL;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment