puzzle contents
raw puzzle

Original Problem

To settle the debate of 0-based vs 1-based indexing I have created a language where you must explicitly state the range of indices an array should have.

For example, given an array definition "A[-1..1] = 1 2 3", you would have:
A[-1] = 1
A[0] = 2
A[1] = 3

You are given a list ofnarray definitions and your job is to figure out what number is found in a given indexiof an arrayarr. Note that the indexing operations may be nested (in the above example, A[A[-1]] would produce result 3).

Line 1:An integernfor the number of array assignments
Nextnlines:One array assignment per line:array_identifier[first_index..last_index] =last_index - first_index + 1integers separated by space
Linen+2:Element to print:arr[i]
A single integer
1 <=n<= 100
Each array name consists of only uppercase letters (A to Z)
Array lengths are between 1 and 100 (no empty arrays)
Indexing operations have at most 50 levels of nesting
Indices are always within bounds in the test cases


Parsing the input strings in an elegant way is probably the basis for a pretty solution. Since the assignments are always of a simple format, we use a regex to read in the data and create a scope hashmap where we put our arrays and the start index to:

const scope = {};
const n = parseInt(readline());
for (let i = 0; i < n; i++) {
  const t = readline().match(/([A-Z]+)\[([0-9-]+)\.\.([0-9-]+)\] = ([-\d ]+)/);
  scope[t[1]] = {
    data: t[4].split(" "),
    startIndex: t[2]

With this efficient lookup table of variables, we now need to recursively evaluate the given query, which can be something like A[0] but also nested strings like C[B[A[0]]. When we ignore the brackets [ and ] and use the list C B A 0, the only thing we have to do is put in the index from left to right:

const x = readline().match(/[A-Z]+|[\d-]+/g);
function solution(index) {
  let cur = scope[x.pop()];
  index = cur.data[index - cur.startIndex];
  if (x.length == 0) {
    return index;
  return solution(index);