# The Nineteenth Bakery [duplicate]

Yes, this is very similar to this question. However, that concerns infinite linked lists, and you do not need to handle out of bounds indexes in that question.

You are in a strange city, looking for the fabled Nineteenth Bakery, where it is said they sell the most delicious deleted posts. The order of shops here is strange. Each street has a number of shops on it, but they are not numbered in order. Instead, each shop has a number n, indicating that the next shop is n shops form the beginning of the street.

For example, consider the street s, which is like this: [1,4,3,-1,2]. To find shop number 3, you first start with the first shop on the street, shop #1. In this case, s[0] is 1. That means that shop #2 is at index 1. s[1] is 4, meaning shop #3 is at index 4. If an index is -1 or is greater than the bounds of the array, that means that there is no "next" shop. A shop may reference a shop already visited. For example, if you are at shop #10 and the next shop has been visited and is #5, that means shop #5 is also shop #11.

You are given a positive number n and a street, which is a non-empty list of numbers. n may be 1 or 0-indexed. You are to follow the path of shops until you get to shop #n. Then output the index (0, or 1-indexed) of shop #n in the list. If there is no shop #n, output any consistent value that is not a positive integer (i.e negative integers, null, undefined, etc.).

## Examples (Array indexes 0-indexed, n 1-indexed)

2, [3,-1, 8, 2] -> 3
1, [13, 46] -> 0
4, [1,2,3,4] -> 3
5280, [8] -> -1
3, [2,0,2,-1] -> 2


## Scoring

This is , so the shortest answer in bytes wins.

• "If an index is -1 or is out of bounds of the array" - does this mean we do or do not need to handle negative numbers other than -1? (They are out of bounds but then again, so is -1.) Commented Mar 9, 2022 at 20:37
• Yes. -1 is the only negative number possible Commented Mar 9, 2022 at 20:44
• got it now. fwiw, it was confusing that we were mixing indexing styles for n and the array.... Commented Mar 10, 2022 at 0:38
• Possible duplicate of Follow a linked list Commented Mar 10, 2022 at 2:01
• I don't see that as different enough to make this challenge not a duplicate. We'll see what the rest of the community thinks. Commented Mar 10, 2022 at 2:18

# JavaScript (ES6), 38 bytes

Expects (0-indexed-array)(1-indexed-n). Returns NaN if the shop is not found.

a=>g=(n,i=0,q=+a[i])=>--n?g(n,q):i+q-q


Try it online!

### Commented

a =>        // a[] = input array
g = (       // g is a recursive function taking:
n,        //   n = remaining number of shops
i = 0,    //   i = current position, starting at 0
q = +a[i] //   q = value stored at a[i] (next shop),
//       forced to NaN if undefined
) =>        //
--n ?       // decrement n; if it's not 0:
g(n, q)   //   recursive call with q as the new position
:           // else:
i + q - q //   return either i or NaN (if q is NaN)

• Cool beans, glad it didn't thwart the approach! Commented Mar 9, 2022 at 20:49

# K (ngn/k), 7 bytes

{x y/0}


Try it online!

Takes the array (y) and n (x) 0-indexed. Returns 0N (the integer null) if the shop is not found.

Successively indexes into the array x times, starting with the first index (hence the 0). The value at that index is then used as the index for the next iteration. Out-of-bound indexes return 0N, the integer null. Only the last value is returned, representing the index of the n-th shop.

# Jelly, 11 bytes

Jelly does not like bounds!

1³JiⱮ¤ḣṪƊ⁴¡


A full program that accepts a non-empty list of integers, the shop's pointers (1-indexed), and a non-negative integer, the path-length (i.e. "0-indexed $$\n\$$"), and prints the resulting (1-indexed) index or 0 if the path-length is not possible.

Try it online!

### How?

1³JiⱮ¤ḣṪƊ⁴¡ - Main Link: shops, path_length
1           - one -> the initial value of Current, used below
¡ - repeat...
⁴  - ...times: path_length
³          -     shops
J         -     range of length -> Valids=[1,2,...,number_of_shops]
Ɱ       -     for s in shops:
i        -       1-indexed index of s in Valids or 0 if not there
-> s or 0 if s is out of bounds
ḣ     -   head to (1-indexed) index Current
Ṫ    -   tail (Note: an empty list yields 0) -> new value of Current
- implicit print of Current

• You may assume that there is at least one element ;) "and a street, which is a non-empty list of numbers" Commented Mar 9, 2022 at 20:19
• Thanks @Seggan it's so dense I didn't spot that; was going to ask... Commented Mar 9, 2022 at 20:23

# Retina 0.8.2, 61 bytes

\d+
$* ; ;1; +1;1(1)*(;(?<-1>1*,?)*(1*)) ;$3$2 1*;(1*).*$.1


Try it online! Link includes test cases. Takes n as 0-indexed but the shops are 1-indexed. Explanation:

\d+
$*  Convert to unary. ; ;1;  Start with the shop at index 1. +1;1(1)*(;(?<-1>1*,?)*(1*)) ;$3$2  Until either n is zero, the index is zero, or the index is out of range, replace the index with the value at that index and decrement n. 1*;(1*).*$.1


Extract the final index and convert it to decimal.

# J, 14 bytes

1 :'{&u&0 ::_'


Try it online!

If "index error" is allowed for my out of bounds value, I can do -4 bytes

• 0-index for both
• Returns _ (infinity) for out of bounds.