Advertisement
RGeorgiev97

dwarfs

Mar 8th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | None | 0 0
  1. <?php
  2. $input = readline();
  3. $dwarfs = [];
  4. while ($input !=="Once upon a time"){
  5.     $args = explode(" <:> ", $input);
  6.     $name = $args[0];
  7.     $hatColor = $args[1];
  8.     $physics = intval($args[2]);
  9.  
  10.     if (!key_exists($hatColor,$dwarfs)){
  11.         $dwarfs[$hatColor][$name] = $physics;
  12.     }
  13.     elseif (key_exists($hatColor,$dwarfs) && key_exists($name,$dwarfs[$hatColor])){
  14.         if ($dwarfs[$hatColor][$name]<$physics){
  15.             $dwarfs[$hatColor][$name] = $physics;
  16.         }
  17.     }
  18.     else {
  19.         $dwarfs[$hatColor][$name] = $physics;
  20.     }
  21.  
  22.     $input = readline();
  23. }
  24.  
  25.  
  26.  
  27. foreach ($dwarfs as $hat=>$dwarfName){
  28.     foreach ($dwarfName as $dName=>$power){
  29.         echo "($hat) $dName <-> $power" .PHP_EOL;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement