I have got such 2d jagged array (row is 1st dimension, column is 2nd dimension, bottom to up, left to right)
0 0 b b b c
0 0 h g g c
0 0 h a a c
0 0 f f d d
0 0 i j e e
0 0 i j 0 0
I would like to get a 2d jagged array summarize the positions of consecutive elements for each row, and ignore all 0
or single elements (assume that things like x x x y x x
would not occur, so there won't be 2 blocks of same chars in a row)
the ideal outcome would be
{(b,3,5,2)} // char, length, rowID, columnID
{(g,2,4,3)}
{(a,2,3,3)}
{(f,2,2,2), (d,2,2,4)}
{(e,2,1,4)}
{}
this is my code, i feel it is not that readable, so I appreciate any suggestion in C# or F# (Linq extension method is welcomed as well)
Tuple<char, int, int, int>[][] GetHorizontalBricks()
{
List<Tuple<char, int, int, int>[]> ret = new List<Tuple<char, int, int, int>[]>();
for (int rowID = 0; rowID < rowNum; rowID++)
{
List<Tuple<char, int, int, int>> rowBricks = new List<Tuple<char, int, int, int>>();
var row = myBoard[rowID];
int recentCount = 1;
int columnID = 0;
while (columnID < columnNum)
{
char recent = row[columnID];
int next = columnID + 1;
for (; next < columnNum; next++)
{
if (row[next] == recent)
{
recentCount++;
}
else
{
break;
}
}
if (recentCount > 1 && recent != '0')
{
rowBricks.Add(Tuple.Create(recent, recentCount, rowID, columnID));
}
columnID = next;
recentCount = 1;
}
ret.Add(rowBricks.ToArray());
}
return ret.ToArray();
}