How can I get this math result?

Currently I’m working on modular train sections. And now I want red part to be cabin, yellow part to be cabin with window. blue part is connect only. from number is look like this:
12321
1223221
112232211
11222322211
1112223222111
How can I achieve this in for loop…?
The result I want basically is like:
1,1
1,2
2,2
2,3
3,3
3,4
4,4
4,5

Use an array of int’s to define the colours you want then loop through the array when setting the colours to set the material of the mesh index or the individual mesh material.
(In the example the zero pin can be unchecked as you are ranging between 1 and 4 and make the switch 1 to 4)

In terms of the different train sections (Window carriage), you can do many things, you can add a child static mesh in the loop and have a ‘set static mesh’ to be ‘Carriage with window’ then set the material of that carriage after the fact