5
\$\begingroup\$

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 of 0.
  • Do the following steps n times:
    1. Add the current value of i to x.
    2. Decrement a[i].
    3. If a[i] is negative, increment it by the length of a.
    4. Set i to a[i].
  • After the iterations, add the final value of i to x.

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>

\$\endgroup\$
5
\$\begingroup\$

Ruby, 54 53 bytes

->a,i,n{i+(1..n).map{i=a[i]+=a[i]<1?a.size-1:-1}.sum}

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python 2, 71 bytes

def f(a,i,n):x=0;exec"x+=i;a[i]+=len(a)*(a[i]<1)-1;i=a[i];"*n;print x+i

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Kotlin, 86 82 bytes

{a,i,n->var x=0;var y=i;(0..n).map{x+=y;a[y]+=if(a[y]<1)a.size-1 else-1;y=a[y]};x}

Try it online!


Ungolfed

{a, i, n ->
    var x=0
    var y=i
    (0..n).map{
        x+=y
        a[y]+=if(a[y]<1) a.size -1 else -1
        y=a[y]
    }
    x
}
\$\endgroup\$
3
\$\begingroup\$

R, 95 83 bytes

function(a,i,n){for(j in 1:n){F=i+F-1
i=(a[i]=a[i]-1+(a[i]-1<0)*sum(a|1))+1}
F+i-1}

Try it online!

Takes i as a 1-based index, so it requires some adjustment to account for that.

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 57 56 55 bytes

Saved one byte thanks to @Lynn.

a=>n=>g=(i,x=0)=>n--?g(a[i]-=a[i]?1:1-a.length,x+i):x+i

Takes input in currying syntax in the format f(a)(n)(i).

Test Cases

f=
a=>n=>g=(i,x=0)=>n--?g(a[i]-=a[i]?1:1-a.length,x+i):x+i
;
console.log( /*21*/ f([2,0,0,5,5,6,7,5,8,5])(5)(0) )
console.log( /*2066*/ f([18,26,9,26,58,53,35,28,0,47,44,6,24,11,66,48,63,64,3,19,67,55,59,35,37,22,41,8,51,44,3,58,58,9,31,29,20,63,43,19,53,28,40,70,20,46,3,69,34,63,49,29,42,69,26,49,37,26,41,62,7,3,66,55,27,59,9,2,36,24,61,6,7])(63)(26) )
console.log( /*1182*/ f([58,3,43,19,8,8,50,24,31,10,8,17,23,2,8,19,19,45,9,46,7,4,6,31,33,19,8,18,37,11,57,28,35,20,22,35,42,38,7,2,48,28,5,17,54,35,5,35,29,9,22,39,27,49,62,40,0,48,52,42,40,24,26])(46)(39) )

\$\endgroup\$
1
\$\begingroup\$

C, 66 63 bytes

The first argument is the length of the array.

Thanks to Justin Mariner for saving 3 bytes.

f(l,a,i,n,x)int*a;{for(x=i;n--;x+=i=a[i]+=l*!a[i]-1);return x;}

Getting negative after decrement means it is zero before that.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 59 bytes

def f(a,i,n):x=i;exec"a[i]=i=~-a[i]%len(a);x+=i;"*n;print x

Try it online!

Similar to XCoder's, but makes use of simultaneous assignment, and python's delightful negative modulus, plus re-ordering some instructions to eliminate the extra addition.

\$\endgroup\$
0
\$\begingroup\$

Java, 87 bytes

I went with a recursive method for this

int r(int[]a,int i,int n){return n>0?(i=a[i]>0?--a[i]:(a[i]=a.length-1))+r(a,i,--n):0;}

see IDEone here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.