I honestly don't know what the point of this sort of calculation would be, but it should make fun golfing. Shortest code wins.
Input (in no particular order)
- An array of integers,
a
(All elements will be integers less than the length of the array.) - An index therein,
i
- An iteration count,
n
Processing
- Store a number
x
with initial value of0
. - Do the following steps
n
times:- Add the current value of
i
tox
. - Decrement
a[i]
. - If
a[i]
is negative, increment it by the length ofa
. - Set
i
toa[i]
.
- Add the current value of
- After the iterations, add the final value of
i
tox
.
Output
After doing the above process, your program should output the final value of x
.
Example (n=5)
a i x
2,0,0,5,5,6,7,5,8,5 0 0
1,0,0,5,5,6,7,5,8,5 1 0
1,9,0,5,5,6,7,5,8,5 9 1
1,9,0,5,5,6,7,5,8,4 4 10
1,9,0,5,4,6,7,5,8,4 4 14
1,9,0,5,3,6,7,5,8,4 3 18
The program outputs 21
.
Test Case Generator
function newTestCase() {
var a = Array(Math.floor(Math.random() * 50) + 25).fill(0);
a = a.map(Math.random).map(e => e * a.length).map(Math.floor);
var i = Math.floor(Math.random() * a.length);
for (var x = 0, n = 0, aa = [...a], ii = i; Math.random() * a.length >= .5;) {
n++;
x += ii;
aa[ii]--;
if (aa[ii] < 0) aa[ii] += a.length;
ii = aa[ii];
}
return {a, i, n, shouldOutput: x + ii};
}
<button type="button" onclick="console.log(newTestCase());">Generate</button>