Your input is a ragged list of possibly empty lists of non-negative integers. For example, [[2,0],[[]],[[[],[1],[]],[]]]
is a valid input. This input is a "compressed" ragged list. What this means is that when we have a list of numbers, we interpret those as a list of indices, indexing the output.
For example, if I=[[2,0],[[]],[[[],[1],[]],[]]]
then the decompressed list is O=[[[[],[[]],[]],[[]],[[[],[[]],[]],[]]]
, because if we replace [2,0]
with O[2][0]=[[],[[]],[]]
and [1]
with O[1]=[[]]
in the input list, we get O
as the output.
The naïve method is just to have the input as a working list and then iteratively replace lists of numbers with by indexing the working list. However this runs into two potential problems:
First, consider an input like I=[[1,0,0,0],[2],[[[[]]]]]
. Here if we index this input like so: I[1][0][0][0]
we will get an index error. We would have to first replace [2]
with I[2]
giving tmp=[[1,0,0,0],[[[[]]]],[[[[]]]]]
. Now we can replace [1,0,0,0]
with tmp[1][0][0][0]
giving O=[[],[[[[]]]],[[[[]]]]]
as the output.
Another difficulty is that we can get a form of co-recursion with inputs like [1],[[0,1],[]]
. This decompresses to [[[],[]],[[],[]]]
Full blown infinite recursion like [[0]]
or [[1],[[0],[0]]]
won't happen though.
Rules
Your input is a ragged list I
that may contain lists consisting of only numbers. Your task is to find a ragged list O
, containing only lists, where if you replace every list L
of numbers in I
by O[L]
you get O
as the output. Your program must output O
. You may assume that a unique solution exists.
You can choose between 0- and 1-based indexing. You can also choose the order of the indices, i.e. whether [2,3,4]
corresponds to O[2][3][4]
or O[4][3][2]
.
This is code-golf so shortest code wins.
Examples
[] -> []
[[],[[],[]]] -> [[],[[],[]]]
[[[],[]],[[0],[0]],[[1],[1]]] -> [[[],[]],[[[],[]],[[],[]]],[[[[],[]],[[],[]]],[[[],[]],[[],[]]]]]
[[[],[[],[],[[]]]],[0,1,2]] -> [[[],[[],[],[[]]]],[[]]]
[[1,0,0,0],[2],[[[[]]]]] -> [[],[[[[]]]],[[[[]]]]]
[[1],[[],[0,0]]] -> [[[],[]],[[],[]]]
[[1],[[2,0,2],[0,0],[]],[[1],[0]]] -> [[[],[],[]],[[],[],[]],[[[],[],[]],[[],[],[]]]]
I
to be on several lines, making it visually parseable. I had to copy pasteI
andO
to an editor to make sense of the brackets (the firstO=
is missing a final bracket, by the way). \$\endgroup\$[1],[[0,1],[]]
" is supposed to have a wrapping pair of brackets around it (maybe it's implied, but best to have it explicit). \$\endgroup\$O=
is missing a final bracket" <-- actually, it just has an extra initial one that's to be deleted. \$\endgroup\$