Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. if($_POST['part'] == '1')
  2. {
  3.   $answer = 0;
  4.  
  5.   foreach(explode("\n", $_POST['input']) as $line)
  6.   {
  7.     $parts = explode('-', $line);
  8.     $last = array_pop($parts);
  9.  
  10.     $name = implode('', $parts);
  11.     $id = intval(preg_replace('/[^0-9]/', '', $last));
  12.     $checksum = trim(preg_replace('/'.$id.'\[(.+?)\]/i', '\\1', $last));
  13.  
  14.     // keep count of characters in name
  15.     $stats = array();
  16.  
  17.     foreach(str_split($name) as $char)
  18.     {
  19.       if(isset($stats[$char]))
  20.       {
  21.         $stats[$char]['count'] += 1;
  22.       }
  23.       else
  24.       {
  25.         $stats[$char] = array(
  26.           'count' => 1,
  27.           'order' => ord($char)
  28.         );
  29.       }
  30.     }
  31.  
  32.     // sort stats by count descending, order ascending
  33.     $count = array();
  34.     $order = array();
  35.     foreach($stats as $key => $row) {
  36.       $count[$key] = $row['count'];
  37.       $order[$key] = $row['order'];
  38.     }
  39.     array_multisort($count, SORT_DESC, $order, SORT_ASC, $stats);
  40.  
  41.     // recreate checksum
  42.     $generatedChecksum = substr(implode(array_keys($stats)), 0, 5);
  43.  
  44.     if($checksum == $generatedChecksum)
  45.     {
  46.       // echo implode('-', $parts).'-'.$id.'<br>';
  47.       $answer += $id;
  48.     }
  49.   }
  50. }
  51. else
  52. {
  53.   foreach(explode("\n", $_POST['input']) as $line)
  54.   {
  55.     $parts = explode('-', $line);
  56.     $last = array_pop($parts);
  57.  
  58.     $name = implode(' ', $parts);
  59.     $id = intval(preg_replace('/[^0-9]/', '', $last));
  60.  
  61.     // decrypt name
  62.     $decryptedName = '';
  63.  
  64.     foreach(str_split($name) as $char)
  65.     {
  66.       if($char == ' ')
  67.       {
  68.         $decryptedName .= $char;
  69.       }
  70.       else
  71.       {
  72.         $index = ord($char) + ($id % 26);
  73.         if($index > 122)
  74.         {
  75.           $index -= 26;
  76.         }
  77.  
  78.         $decryptedName .= chr($index);
  79.       }
  80.     }
  81.  
  82.     echo $decryptedName.': <strong>'.$id.'</strong><br><br>';
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement