# Introduction

Arrays are usually used with for-loops or iterators. One different task might be the use as a cheap way to rotate through a set of items, so after reaching the last index, the next item would be the first item of the array. This is useful for cases like menus and physical controls.

# Challenge

Given the size of an array, the initial index, and the movement (which can be positive, zero, or negative), output the resulting index in the array after cyclically moving through it.

As soon as a number which is greater than the current array index + the number of remaining indices, the added index would go out of bounds. The other way arround, substracting a greater negative value than the current index from itself, the index would be negative and therefore invalid. This should be compensated by the solution. Also, when exceeding the index by not only one, but two or more array lengths, this should also be compensated (see example 2).

# Example input and output

We assume (for the sake of simplicity) that arrays start at 0.

Array for visualization: ['a', 'b', 'c', 'd', 'e', 'f']

### Example 1

Input:

Array size: 6

Current Index: 1 (-> 'b')

Movement: +5

Index Movement:

2 -> 3 -> 4 -> 5 -> 0

Output:

Index: 0 (-> 'a')

### Example 2

Input:

Array size: 6

Current Index: 2 (-> 'c')

Movement: -10

Index Movement:

1 -> 0 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 4

Output:

Index: 4 (-> 'e')

# Objective

The solution with the fewest bytes wins the challenge. Using Lists, Linked Lists etc. is not allowed, fixed arrays only. Non-codegolfing languages are preferred.

• I predict a 2-byte solution in a stack-based language: Sum, Mod.
Commented Sep 9, 2020 at 0:08
• Are we meant to take an array (plus start index and rotation) and produce the element or the length of an array (plus start index and rotation) and produce the index? Commented Sep 9, 2020 at 0:10
• What exactly do you mean by Using Lists, Linked Lists etc. is not allowed, fixed arrays only.? Be careful not to make false assumptions about the properties of programming languages.
Commented Sep 9, 2020 at 0:13
• Please use the sandbox next time you have another challenge idea. (I'm aware that new users might not have access to the sandbox right away.) Also, banning certain features of languages is not recommended on the site, because e.g. in this case, you're unnecessarily penalizing languages like Haskell whose only built-in sequence type is linked list. Commented Sep 9, 2020 at 0:20
• @Bubbler I believe out meta site has been special-cased so new users can use the sandbox right away.
Commented Sep 9, 2020 at 0:37

# APL (Dyalog Unicode), 5 4 bytes (SBCS)

Note: APL is not a golfing language.

-1 thanks to Bubbler.

Anonymous tacit prefix function. Takes (array size, current index, movement) as argument.

⊃|+/

Try it online!

+/‌ sum the right argument

⊃| division remainder when divided by the first element of the argument

• Porting one of Python answers, ⊃|+/ should work (taking one vector argument of three numbers). Commented Sep 9, 2020 at 0:46
• @Bubbler Oh, right, of course.
Commented Sep 9, 2020 at 0:47

# Jelly, 2 bytes

S%

A dyadic Link accepting the start index and the rotation on the left and the length on the right which yields the finish index.

Try it online!

### How?

S% - Link: [start, rotation]; length
S  - sum -> start + rotation
% - modulo (length)
• As predicted. Well, Jelly isn't really a stack-based language, but it basically behaves as one.
Commented Sep 9, 2020 at 0:14
• ...and it does not look all that much like a golfing language! Commented Sep 9, 2020 at 0:15
• Right, ASCII-only. What is this heresy‽
Commented Sep 9, 2020 at 0:16

# J, 3 bytes

|+/

Try it online!

We've got APL and Jelly, so here comes J which is between APL and Jelly in the language hierarchy. Turns out the byte count is also in the middle :) And J is not a golfing language either.

Now the byte count is the exact average of Jelly (2B) and APL (4B)!

### How it works

|+/  Left: array size, Right: 2-item array of initial index and offset
+/  index + offset
|    modulo array size
• Nice use of a hook!
Commented Sep 9, 2020 at 0:38

# Python, 19 bytes

lambda l,p:sum(p)%l

An unnamed function accepting length, (start index, rotation) which returns the finish index.

Try it online!

20s:

lambda l,i,r:(i+r)%l

TIO

lambda l,*i:sum(i)%l

TIO

lambda*a:sum(a)%a[0]

TIO

# C (gcc), 27 26 bytes

r;f(z,i,m){r=(m%z+z+i)%z;}

Try it online!

Saved 1 thanks to @att !

• 26 bytes
– att
Commented Sep 9, 2020 at 2:06

# Japt, 5 bytes

Nx uU

Try it

Nx      - sum inputs
uU   - modulo first input