Advertisement
Guest User

NetherRealms

a guest
Feb 21st, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 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. ksort($demons);
  27. foreach ($demons as $name => $value) {
  28.     $damageFormatted = number_format($value['points'], 2, '.', '');
  29.     echo "$name - {$value['health']} health, $damageFormatted damage" . PHP_EOL;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement