Advertisement
serpentiny

09. Kamino Factory

Oct 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2. $dnaLength = readline();
  3.  
  4. $bestDNA = [
  5.   'sample' => 0,
  6.   'index' => 'none',
  7.   'length' => 0,
  8.   'dna' => '',
  9.   'sum' => 0
  10. ];
  11.  
  12. $sample = 0;
  13.  
  14. while (true) {
  15.   $input = readline();
  16.  
  17.  
  18.   if ($input === 'Clone them!') {
  19.       echo 'Best DNA sample ' . $bestDNA['sample'] . ' with sum: ' . $bestDNA['sum'] . '.' . PHP_EOL;
  20.       echo $bestDNA['dna'] ;
  21.       break;
  22.   }
  23.  
  24.   $sample++;
  25.  
  26.   $dna = str_replace("!", "", $input);
  27.  
  28.   if (intval($dnaLength) === strlen($dna)) {
  29.       $sum = array_sum(explode('!', $input));
  30.  
  31.       $dna = str_replace("0", " 0 ", $dna);
  32.       $printDNA = trim(str_replace("!", " ", $input));
  33.       $dna = trim(str_replace("  ", " ", $dna));
  34.       $dna = explode(' ', $dna);
  35.  
  36.       $length = 0;
  37.       $index = '';
  38.       for ($i = 0; $i < count($dna); $i++) {
  39.           if ($dna[$i] > 1) {
  40.               if ($length < strlen($dna[$i])) {
  41.                   $length = strlen($dna[$i]);
  42.                   $index = strlen(implode("", array_slice($dna, 0, $i)));
  43.               }
  44.           }
  45.       }
  46.       $addToBestDNA = false;
  47.       if ($bestDNA['index'] === 'none') {
  48.           $addToBestDNA = true;
  49.       } elseif ($bestDNA['length'] < $length) {
  50.           $addToBestDNA = true;
  51.       } elseif ($bestDNA['length'] === $length && $bestDNA['index'] > $index) {
  52.           $addToBestDNA = true;
  53.       } elseif ($bestDNA['length'] === $length && $bestDNA['index'] === $index && $bestDNA['sum'] < $sum) {
  54.           $addToBestDNA = true;
  55.       }
  56.  
  57.       if ($addToBestDNA) {
  58.           $bestDNA['sample'] = $sample;
  59.           $bestDNA['index'] = $index;
  60.           $bestDNA['length'] = $length;
  61.           $bestDNA['dna'] = $printDNA;
  62.           $bestDNA['sum'] = $sum;
  63.       }
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement