Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2. $array = preg_replace('/\s+/', '', explode(",", readline()));
  3. $regex = '/[^a-zA-Z\s]/';
  4. $regexInt = '/((?:-|\+|)\d+(?:\.*?\d+)*)/';
  5.  
  6. $demons = [];
  7. for ($i = 0; $i < count($array); $i++) {
  8. $health = 0;
  9. $point = 0;
  10. $demon = $array[$i];
  11. $health = addHealth($regex, $health, $demon);
  12. $point = addPoints($regexInt, $point, $demon);
  13. $demons[$demon]['health'] = $health;
  14. $demons[$demon]['point'] = $point;
  15. }
  16. function addHealth($regex, $health, $demon)
  17. {
  18. $demon = preg_replace($regex, "", $demon);
  19. for ($i = 0; $i < strlen($demon); $i++) {
  20. $health += ord($demon[$i]);
  21. }
  22. return $health;
  23. }
  24.  
  25. function addPoints($regexInt, $point, $demon)
  26. {
  27. preg_match_all($regexInt, $demon, $matches);
  28. for ($w = 0; $w < count($matches[0]); $w++) {
  29. $point += $matches[0][$w];
  30. }
  31. $str = $demon;
  32. $str = preg_replace("/[^\/\/*]/", "", $str);
  33. for ($d = 0; $d < strlen($str); $d++) {
  34. if ($str[$d] == "*") {
  35. $point *= 2;
  36. } elseif ($str[$d] == "/") {
  37. $point /= 2;
  38. }
  39. }
  40. return $point;
  41. }
  42. uksort($demons, function ($a, $b) use ($demons) {
  43. if ($demons[$a]['health'] === $demons[$b]['health']) return strcasecmp($a, $b);
  44. return $demons[$b]['health'] - $demons[$a]['health'];
  45. });
  46. foreach ($demons as $name => $value) {
  47. printf("$name - {$value['health']} health, %.2f damage" . PHP_EOL, $value['point']);
  48. }
  49. //print_r($demons);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement