Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. <?php
  2. $str = explode(",", readline());
  3. $demons = [];
  4. foreach ($str as $demon) {
  5. $demon = str_replace(' ', '', trim($demon));
  6. preg_match_all('/[^0-9+\-*\/\.]/', $demon, $latterMach);
  7. $healthSum = 0;
  8. foreach ($latterMach[0] as $demonName) {
  9. $healthSum += ord($demonName);
  10. }
  11. $demons[$demon]['health'] = $healthSum;
  12. }
  13. foreach ($demons as $key => $numbers) {
  14. preg_match_all('/([-+]?[0-9]*\.[0-9]+|[0-9]+)|([-+]?[0-9]+)/', $key, $matchNum);
  15. $damageSum = array_sum($matchNum[0]);
  16. preg_match_all('/[\/*]/', $key, $operandsMatches);
  17. foreach ($operandsMatches[0] as $operands) {
  18. if ($operands == "*") {
  19. $damageSum *= 2;
  20. } elseif ($operands == "/") {
  21. $damageSum /= 2;
  22. }
  23. }
  24. $demons[$key]['points'] = $damageSum;
  25.  
  26. }
  27. foreach ($demons as $name => $value) {
  28. printf("$name - {$value['health']} health, %.2f damage" . PHP_EOL, $value['points']);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement