ClarkeRubber

UNSW ProgComp: Problem 4 - 2007

Jun 11th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 9
  5. 17 11
  6. 8 1
  7. 5 4
  8. 29 2
  9. 50 49
  10. 18 6
  11. 37 3
  12. 2 1
  13. 41 3
  14. END;
  15.  
  16. function generate_array($length){
  17.     for($x = 0; $x < $length; $x++){
  18.         $new_array[$x] = array($x, 0); //this is kinda silly, but it works
  19.             //value, status 0=alive 1=dead (counter intuitive but meh)
  20.     }
  21.     //print_r($new_array);
  22.     return $new_array;
  23. }
  24.  
  25. $input = explode("\n", $input);
  26. array_shift($input); //scrap the frist line
  27.  
  28. foreach($input as $key => $value){
  29.     $input[$key] = explode(" ", $value);
  30. }
  31.  
  32. foreach($input as $key => $value){
  33.     //value[0] = count of people
  34.     //value[1] = jump length
  35.  
  36.     /*
  37.     0 - 1 - kill loop 4
  38.     1 - 2 - kill loop 2
  39.     2 - 3 - kill loop 1
  40.     3 - 4 - survivor
  41.     4 - 5 - kill loop 3
  42.     5 - 6 - kill loop 1
  43.     6 - 7 - kill loop 2
  44.  
  45.     if jump is three,
  46.     start counting at zero, add 2 (k - 1) to that number (0 + k - 1 (0 + 3 - 1 = 2)), remove that number from the list
  47.     (destroy key 2), add 2 + 1 + 3 - 1 (last_pos + 1 + k - 1)%n = 5. then: last_pos = 5, n = 7 so (5+1 + 3-1)%7 = (8)%7 = 1
  48.  
  49.     wait... that system is stupid, let's do it the rudamentary way
  50.     couple of while loops
  51.     */
  52.     $array = generate_array($value[0]); //of length 'n'
  53.     $header = 0;
  54.     $kills = 0;
  55.     $jump_length = $value[1];
  56.     $total = $value[0];
  57.        
  58.     while(($total - $kills) > 1){
  59.         $jumping = $jump_length;
  60.         while($jumping != 1){
  61.             if($array[$header][1] == 0){
  62.                 //decrement jumping
  63.                 $jumped = $array[$header][0];
  64.                 $jumping--;
  65.                 //echo "Jumping: $jumped\n";
  66.             }else{
  67.                 $jumped = $array[$header][0];
  68.                 //echo "Already dead: $jumped\n";
  69.             }
  70.             $header++;
  71.             $header = $header%$total;
  72.         }
  73.         //the next value where array[x][1] == 0 gets killed
  74.         while($array[$header][1] != 0){
  75.             $header++;
  76.             $header = $header%$total;
  77.         }
  78.         $array[$header][1] = 1;
  79.         $killed = $array[$header][0];
  80.         $kills++;
  81.         //echo "killed: $killed\n";
  82.         //print_r($array);
  83.     }
  84.     foreach($array as $value2){
  85.         if($value2[1] == 0){
  86.             $survivor = $value2[0]+1;
  87.             $killed++;
  88.             echo str_pad($value[0], 2, ' ', STR_PAD_LEFT).str_pad($value[1], 3, ' ', STR_PAD_LEFT).'    '.str_pad($survivor, 3, ' ', STR_PAD_LEFT).str_pad($killed, 3, ' ', STR_PAD_LEFT)."\n";
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment