-- v1.0 fixed 'arch' case (see lines 75-85) function solveit pData local tData, nColumns, tABlocks, tCountBlocks, tARow, tARowAbove, tResult -- NB if you were to pass a file name instead, then you could read the file line-by-line -- to minimize storage needed. put pData into tData put 0 into tResult local lineCount repeat for each line L in tData add 1 to lineCount if nColumns = 0 then -- first line !! put the number of chars in L into nColumns else if nColumns <> the number of chars in L then put "Bad line length" && nColumns && L into fld "F" return -1 end if put tARow into tARowAbove put empty into tARow end if local charCount, prevChar, tBlock put 0 into charCount repeat for each char C in L add 1 to charCount put C into tARow[charCount]["color"] -- compare with previous (left) square if charCount > 1 then if C = prevChar then if tARow[charCount-1]["block"] is empty then -- a block of 2, not already in a block add 1 to tCountBlocks put tCountBlocks into tARow[charCount-1]["block"] put tCountBlocks into tARow[charCount]["block"] put charCount-1,linecount & ";" & charcount,lineCount & ";" into tABlocks[tCountBlocks] else -- left is already in a block put tARow[charCount-1]["block"]into tARow[charCount]["block"] put charcount,lineCount & ";" after tABlocks[tARow[charCount]["block"]] end if end if end if -- and then compare with the square above if C = tARowAbove[charCount]["color"] then -- same color as above, must merge if tARowAbove[charCount]["block"] is empty then -- previous row not in a block if tARow[charCount]["block"] is empty then -- and this one not in a bloce -- so we need to create a new block, and put this into it add 1 to tCountBlocks put tCountBlocks into tARow[charCount]["block"] put charCount,lineCount-1 & ";" & charCount,lineCount & ";" after tABlocks[tCountBlocks] else -- this row is already in a block - count for the above one put charCount,lineCount-1 & ";" after tABlocks[tARow[charCount]["block"]] end if else -- above row is in a block already switch case tARow[charCount]["block"] is empty -- and this one not in a block -- so we put this into the block from prev row put tARowAbove[charCount]["block"] into tARow[charCount]["block"] put charCount,lineCount & ";" after tABlocks[tARow[charCount]["block"]] break case tARow[charCount]["block"] = tARowAbove[charCount]["block"] -- SAME block -- same block, so already all counted break default -- BOTH in blocks, different ids - so MERGE local tOldBlock put tARow[charCount]["block"] into tBlock put tARowAbove[charCount]["block"] into tOldBlock -- deal with the case of an arch with a left foot foundation -- .rrr When doing 2,3 we merge with above, and must -- .r.r <--- update the rightmost block id (4,2) -- rr.r <--- to get this one (4,3) to match 3,3 repeat with i = charcount to the number of chars in L if tARowAbove[i]["block"] = tOldBlock then put tBlock into tARowAbove[i]["block"] end if end repeat put tABlocks[tOldBlock] after tABlocks[tBlock] put empty into tABlocks[tOldBlock] break end switch end if end if put C into prevChar end repeat end repeat local tKeys put the keys of tABlocks into tKeys set the itemDel to ";" sort lines of tKeys numeric descending by the number of items in tABlocks[each] put tABlocks[line 1 of tKeys] into tResult replace ";" with CR in tResult return tResult end solveit