Ad

collapsing runs of the same color isn't quite corect.
It fails a test case.

Code
Diff
  • import re
    
    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"
    
    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
    
    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]
    • import re
    • 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"
    • 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)
    • print(orig, row)
    • return row
    • 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]
Code
Diff
  • import re
    
    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"
    
    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
    
    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]
    • import re
    • 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"
    • 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
    • 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]
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"



def triangle(row):
    if len(row) == 1: return 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]