constant n = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}}; function AB(sequence text, integer y, x, step) integer wtb = 0, bn = 0 integer prev = '#', next string p2468 = "" for i=1 to length(n) do next = text[y+n[i][1]][x+n[i][2]] wtb += (prev='.' and next<='#') bn += (i>1 and next<='#') if and_bits(i,1)=0 then p2468 = append(p2468,prev) end if prev = next end for if step=2 then -- make it p6842 p2468 = p2468[3..4]&p2468[1..2] end if return {wtb,bn,p2468} end function procedure Zhang_Suen(sequence text) integer wtb, bn, changed, changes string p2468 -- (p6842 for step 2) text = split(text,'\n') while 1 do changed = 0 for step=1 to 2 do changes = 0 for y=2 to length(text)-1 do for x=2 to length(text[y])-1 do if text[y][x]='#' then {wtb,bn,p2468} = AB(text,y,x,step) if wtb=1 and bn>=2 and bn<=6 and find('.',p2468[1..3]) and find('.',p2468[2..4])then changes = 1 text[y][x] = '!' -- (logically still black) end if end if end for end for if changes then for y=2 to length(text)-1 do text[y] = substitute(text[y],"!",".") end for changed = 1 end if end for if not changed then exit end if end while puts(1,join(text,"\n")) end procedure string small_rc = """ ................................ .#########.......########....... .###...####.....####..####...... .###....###.....###....###...... .###...####.....###............. .#########......###............. .###.####.......###....###...... .###..####..###.####..####.###.. .###...####.###..########..###.. ................................""" Zhang_Suen(small_rc)