google kickstart round H. painter

wonderful world·2021년 11월 20일
0

https://codingcompetitions.withgoogle.com/kickstart/round/0000000000435914/00000000008d9a88

'''
Red + Yellow = Orange
Red + Blue = Purple
Yellow + Blue = Green
Red + Yellow + Blue = Gray
'''
# U:0x00    U = Uncolored
# R:0x01    R = Red
# Y:0x02    Y = Yellow
# B:0x04    B = Blue
# O:0x03    O = Orange
# P:0x05    P = Purple
# G:0x06    G = Green
# A:0x07    A = Gray
def f(P):
    vs = encode(P)
    strokes = count_stroke(vs)
    return strokes

# return the number of strokes
def count_stroke(vs):
    c=0
    cs=[1,2,4]
    for color in cs:
        c+=count(vs,color)
    return c

# return how many strokes are necessary
def count(vs,color):
    def contains(v,color):
        return v & color
    
    def subtract(v, color):
        return v ^ color
        
    stroking=False
    strokes=0
    for i,v in enumerate(vs):
        if contains(v, color):
            vs[i] = subtract(v, color)
            if stroking==False:
                strokes += 1
            stroking=True
        else:
            stroking=False
    return strokes

def encode(P):
    color_code = {
        'U':0x00,
        'R':0x01,
        'Y':0x02,
        'B':0x04,
        'O':0x03,
        'P':0x05,
        'G':0x06,
        'A':0x07
    }
    return [color_code[p] for p in P]

assert f('YYYBBBYYY') == 3
assert f('YYGGBB') == 2
assert f('ROAOR') == 3

if __name__ == '__main__':
    def read_int(): return int(input())
    
    T = read_int()
    for i in range(T):
        N = read_int()
        P = input()
        ans = f(P)
        print (f'Case #{i+1}: {ans}')
    
profile
hello wirld

0개의 댓글