Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if($_POST['part'] == '1')
- {
- $answer = 0;
- foreach(explode("\n", $_POST['input']) as $line)
- {
- $parts = explode('-', $line);
- $last = array_pop($parts);
- $name = implode('', $parts);
- $id = intval(preg_replace('/[^0-9]/', '', $last));
- $checksum = trim(preg_replace('/'.$id.'\[(.+?)\]/i', '\\1', $last));
- // keep count of characters in name
- $stats = array();
- foreach(str_split($name) as $char)
- {
- if(isset($stats[$char]))
- {
- $stats[$char]['count'] += 1;
- }
- else
- {
- $stats[$char] = array(
- 'count' => 1,
- 'order' => ord($char)
- );
- }
- }
- // sort stats by count descending, order ascending
- $count = array();
- $order = array();
- foreach($stats as $key => $row) {
- $count[$key] = $row['count'];
- $order[$key] = $row['order'];
- }
- array_multisort($count, SORT_DESC, $order, SORT_ASC, $stats);
- // recreate checksum
- $generatedChecksum = substr(implode(array_keys($stats)), 0, 5);
- if($checksum == $generatedChecksum)
- {
- // echo implode('-', $parts).'-'.$id.'<br>';
- $answer += $id;
- }
- }
- }
- else
- {
- foreach(explode("\n", $_POST['input']) as $line)
- {
- $parts = explode('-', $line);
- $last = array_pop($parts);
- $name = implode(' ', $parts);
- $id = intval(preg_replace('/[^0-9]/', '', $last));
- // decrypt name
- $decryptedName = '';
- foreach(str_split($name) as $char)
- {
- if($char == ' ')
- {
- $decryptedName .= $char;
- }
- else
- {
- $index = ord($char) + ($id % 26);
- if($index > 122)
- {
- $index -= 26;
- }
- $decryptedName .= chr($index);
- }
- }
- echo $decryptedName.': <strong>'.$id.'</strong><br><br>';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement