Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = <<<END
- 9
- 17 11
- 8 1
- 5 4
- 29 2
- 50 49
- 18 6
- 37 3
- 2 1
- 41 3
- END;
- function generate_array($length){
- for($x = 0; $x < $length; $x++){
- $new_array[$x] = array($x, 0); //this is kinda silly, but it works
- //value, status 0=alive 1=dead (counter intuitive but meh)
- }
- //print_r($new_array);
- return $new_array;
- }
- $input = explode("\n", $input);
- array_shift($input); //scrap the frist line
- foreach($input as $key => $value){
- $input[$key] = explode(" ", $value);
- }
- foreach($input as $key => $value){
- //value[0] = count of people
- //value[1] = jump length
- /*
- 0 - 1 - kill loop 4
- 1 - 2 - kill loop 2
- 2 - 3 - kill loop 1
- 3 - 4 - survivor
- 4 - 5 - kill loop 3
- 5 - 6 - kill loop 1
- 6 - 7 - kill loop 2
- if jump is three,
- start counting at zero, add 2 (k - 1) to that number (0 + k - 1 (0 + 3 - 1 = 2)), remove that number from the list
- (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
- wait... that system is stupid, let's do it the rudamentary way
- couple of while loops
- */
- $array = generate_array($value[0]); //of length 'n'
- $header = 0;
- $kills = 0;
- $jump_length = $value[1];
- $total = $value[0];
- while(($total - $kills) > 1){
- $jumping = $jump_length;
- while($jumping != 1){
- if($array[$header][1] == 0){
- //decrement jumping
- $jumped = $array[$header][0];
- $jumping--;
- //echo "Jumping: $jumped\n";
- }else{
- $jumped = $array[$header][0];
- //echo "Already dead: $jumped\n";
- }
- $header++;
- $header = $header%$total;
- }
- //the next value where array[x][1] == 0 gets killed
- while($array[$header][1] != 0){
- $header++;
- $header = $header%$total;
- }
- $array[$header][1] = 1;
- $killed = $array[$header][0];
- $kills++;
- //echo "killed: $killed\n";
- //print_r($array);
- }
- foreach($array as $value2){
- if($value2[1] == 0){
- $survivor = $value2[0]+1;
- $killed++;
- 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";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment