Imagine you have an array of integers, whose positivenon-negative values are pointers to other positions in the same array, only that those values represent tunnels, so if the value in position A is positive and points to position B, then the value in position B must be also positive and point to position A to represent both ends of the tunnel. So:
Challenge
- Given an array of integers, check if the array complies with the restriction to be a tunneling array and return two distinct, coherent values for truthy and falsey.
- The values in the array will be below zero for non-tunnel positions, and zero or above for tunnel positions. If your array is 1-indexed, then the zero value represents a non-tunnel position. Non-tunnel values do not need to be checked.
- If a positive value in a cell points to itself, that's a falsey. If A points to B, B to C and C to A, that's a falsey. If a positive value points beyond the limits of the array, that's a falsey.
Examples
The following examples are 0-indexed:
[-1, -1, -1, 6, -1, -1, 3, -1, -1] Truthy (position 3 points to position 6 and vice versa)
[1, 0] Truthy (position 0 points to position 1 and vice versa)
[0, 1] Falsey (positions 0 and 1 point to themselves)
[4, 2, 1, -1, 0, -1] Truthy
[2, 3, 0, 1] Truthy
[1, 2, 0] Falsey (no circular tunnels allowed)
[-1, 2, -1] Falsey (tunnel without end)
[] Truthy (no tunnels, that's OK)
[-1, -2, -3] Truthy (no tunnels, that's OK)
[1, 0, 3] Falsey (tunnel goes beyond limits)
[1] Falsey (tunnel goes beyond limits)
This is code-golf, so may the shortest code for each language win!