Ad

Missing what is it about. Just simplyfing things :)

Code
Diff
  • COLORS = set("RGB")
    def blend(c1, c2):
        colors = {c1, c2}
        return c1 if len(colors) == 1 else (COLORS - colors).pop()
    
    
    def preprocess(row):
        check = ""
        for r in row:
            if r != check:
                yield r
                check = r
    
    
    def triangle(row):
        row = list(preprocess(row))
        while len(row) > 1:
            for i,r in enumerate(row[:-1]):
                row[i] = blend(r, row[i+1])
            row.pop()
        return row[0]
    
    • import re
    • COLORS = set("RGB")
    • def blend(c1, c2):
    • if c1 == c2: return c1
    • colors = c1 + c2
    • if colors == "RB" or colors == "BR": return "G"
    • if colors == "RG" or colors == "GR": return "B"
    • if colors == "GB" or colors == "BG": return "R"
    • colors = {c1, c2}
    • return c1 if len(colors) == 1 else (COLORS - colors).pop()
    • def preprocess(row):
    • orig = row
    • row,_ = re.subn('RR+', 'R', row)
    • row,_ = re.subn('BB+', 'B', row)
    • row,_ = re.subn('GG+', 'G', row)
    • print(orig, row)
    • return row
    • check = ""
    • for r in row:
    • if r != check:
    • yield r
    • check = r
    • def triangle(row):
    • if len(row) == 1: return row
    • row = preprocess(row)
    • row = list(row)
    • for j in range(len(row)-1,0,-1):
    • for i in range(j):
    • row[i] = blend(row[i], row[i+1])
    • return row[0]
    • row = list(preprocess(row))
    • while len(row) > 1:
    • for i,r in enumerate(row[:-1]):
    • row[i] = blend(r, row[i+1])
    • row.pop()
    • return row[0]