I have a list say [A, B, C,D,E,F,G,H]
I wish to form a 3D cube-like structure using these elements where neighbors of each element will be stored in a tuple.
I want the approach through coordinates.
Each element should be given x, y and z coordinates and based on x,y,z I will fetch the neighbors.
My problem is if I use a nested for loop then assignment within loop is not happening which is not allowing me to iterate through the list.
index = -1
for x <- 1..row_length do
for y <- 1..row_length do
for z <- 1..row_length do
Index = index + 1
{Enum.at(list,index), x, y, z}
end
end
end
I tried using
g = Enum.map(a, fn node ->
for x <- 1..2 do
for y <- 1..2 do
for z <- 1..2 do
index = index + 1
{node, index, x, y, z}
end
end
end
end)
But the above code two code snippets give me
[
[
[[{A, 1, 1, 1}, {A, 1, 1, 2}], [{A, 1, 2, 1}, {A, 1, 2, 2}]],
[[{A, 2, 1, 1}, {A, 2, 1, 2}], [{A, 2, 2, 1}, {A, 2, 2, 2}]]
],
[
[[{B, 1, 1, 1}, {B, 1, 1, 2}], [{B, 1, 2, 1}, {B, 1, 2, 2}]],
[[{B, 2, 1, 1}, {B, 2, 1, 2}], [{B, 2, 2, 1}, {B, 2, 2, 2}]]
],
[
[[{C, 1, 1, 1}, {C, 1, 1, 2}], [{C, 1, 2, 1}, {C, 1, 2, 2}]],
[[{C, 2, 1, 1}, {C, 2, 1, 2}], [{C, 2, 2, 1}, {C, 2, 2, 2}]]
],
[
[[{D, 1, 1, 1}, {D, 1, 1, 2}], [{D, 1, 2, 1}, {D, 1, 2, 2}]],
[[{D, 2, 1, 1}, {D, 2, 1, 2}], [{D, 2, 2, 1}, {D, 2, 2, 2}]]
],
[
[[{E, 1, 1, 1}, {E, 1, 1, 2}], [{E, 1, 2, 1}, {E, 1, 2, 2}]],
[[{E, 2, 1, 1}, {E, 2, 1, 2}], [{E, 2, 2, 1}, {E, 2, 2, 2}]]
],
[
[[{F, 1, 1, 1}, {F, 1, 1, 2}], [{F, 1, 2, 1}, {F, 1, 2, 2}]],
[[{F, 2, 1, 1}, {F, 2, 1, 2}], [{F, 2, 2, 1}, {F, 2, 2, 2}]]
],
[
[[{G, 1, 1, 1}, {G, 1, 1, 2}], [{G, 1, 2, 1}, {G, 1, 2, 2}]],
[[{G, 2, 1, 1}, {G, 2, 1, 2}], [{G, 2, 2, 1}, {G, 2, 2, 2}]]
],
[
[[{H, 1, 1, 1}, {H, 1, 1, 2}], [{H, 1, 2, 1}, {H, 1, 2, 2}]],
[[{H, 2, 1, 1}, {H, 2, 1, 2}], [{H, 2, 2, 1}, {H, 2, 2, 2}]]
]
]
Where as desired results would be
{A, 1,1,1}
{B,1,1,2}
{C,1,2,1}
{D,1,2,2}
{E,2,1,1}
{F,2,1,2}
{G,2,2,1}
{H,2,2,2}