-2
\$\begingroup\$

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.

\$\endgroup\$
11
  • 3
    \$\begingroup\$ I predict a 2-byte solution in a stack-based language: Sum, Mod. \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:08
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Commented Sep 9, 2020 at 0:10
  • 4
    \$\begingroup\$ 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. \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:13
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – Bubbler
    Commented Sep 9, 2020 at 0:20
  • 2
    \$\begingroup\$ @Bubbler I believe out meta site has been special-cased so new users can use the sandbox right away. \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:37

6 Answers 6

5
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Porting one of Python answers, ⊃|+/ should work (taking one vector argument of three numbers). \$\endgroup\$
    – Bubbler
    Commented Sep 9, 2020 at 0:46
  • \$\begingroup\$ @Bubbler Oh, right, of course. \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:47
4
\$\begingroup\$

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)
\$\endgroup\$
3
  • \$\begingroup\$ As predicted. Well, Jelly isn't really a stack-based language, but it basically behaves as one. \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:14
  • \$\begingroup\$ ...and it does not look all that much like a golfing language! \$\endgroup\$ Commented Sep 9, 2020 at 0:15
  • 3
    \$\begingroup\$ Right, ASCII-only. What is this heresy‽ \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:16
4
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Nice use of a hook! \$\endgroup\$
    – Adám
    Commented Sep 9, 2020 at 0:38
2
\$\begingroup\$

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

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

C (gcc), 27 26 bytes

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

Try it online!

Saved 1 thanks to @att !

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 26 bytes \$\endgroup\$
    – att
    Commented Sep 9, 2020 at 2:06
0
\$\begingroup\$

Japt, 5 bytes

Nx uU

Try it

Nx      - sum inputs
   uU   - modulo first input
\$\endgroup\$

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