Given an Integer array:
- Start from the first number
- Jump forward n positions where n is the value of the current position
- Delete the current position, making what was the next position the current position.
- Goto step 2 until there is one number remaining
- Print that number
Rules
The array wraps-around (the next number after the last number in the array is the first number).
A zero removes itself (Obviously).
Negative numbers are not allowed as input.
Test Cases
[1] => 1
[1,2] => 1
[1,2,3] => 3
[1,2,2] => 1
[1,2,3,4] => 1
[6,2,3,4] => 4
[1,2,3,4,5] => 5
[0,1] => 1
[0,0,2,0,0] => 0
Step-by-step example
[1,4,2,3,5]
^ start from the first position
^ jump 1 position (value of the position)
[1, 2,3,5] remove number in that position
^ take next position of the removed number (the 'new' 'current' position)
^ jump 2 positions
[1, 2,3 ] remove number in that position
^ take next position (looping on the end of the array)
^ jump 1 position
[1, 3 ] remove number in that position
^ take next position (looping)
^ jump 3 positions (looping on the end of the array)
[ 3 ] remove number in that position
print 3
Example #2
[4,3,2,1,6,3]
^ start from the first position
^ jump 4 positions
[4,3,2,1, 3] remove number in that position
^ take next position
^ jump 3 positions
[4,3, 1, 3] remove number in that position
^ take next position
^ jump 1 positions
[4,3, 1 ] remove number in that position
^ take next position
^ jump 4 positions
[4, 1 ] remove number in that position
^ take next position
^ jump 1 position
[ 1 ] remove number in that position
print 1
This is code-golf, the shortest answer in bytes wins!