

def compare_conf(c1: str, c2: str):
    s = 0

    for e in c2:
        if e in c1:
            s+=1
    
    return s


def find_config(sig, val):
    conf = {}
    c4 = ""
    c7 = ""

    for n in sorted(sig):
        match len(n):
            case 2:
                conf["".join(sorted(n))] = 1
            case 3:
                conf["".join(sorted(n))] = 7
                c7 = "".join(sorted(n))
            case 4:
                conf["".join(sorted(n))] = 4
                c4 = "".join(sorted(n))
            case 7:
                conf["".join(sorted(n))] = 8

    for n in sorted(sig):
        match len(n):
            case 5:
                if compare_conf(n, c7) == 3:
                    conf["".join(sorted(n))] = 3
                elif compare_conf(n, c4) == 2:
                    conf["".join(sorted(n))] = 2
                elif compare_conf(n, c4) == 3:
                    conf["".join(sorted(n))] = 5
            case 6:
                print(c4)
                if compare_conf(n, c7) == 2:
                    conf["".join(sorted(n))] = 6
                elif compare_conf(n, c4) == 4:
                    conf["".join(sorted(n))] = 9
                elif compare_conf(n, c4) == 3:
                    conf["".join(sorted(n))] = 0

    return conf

with open("input.txt", 'r') as f:
    data = [[l.strip().split('|')[0].strip().split(' '), l.strip().split('|')[1].strip().split(' ')] for l in f.readlines()]

    # c = 0

    # for l in data:
    #     for e in l:
    #         len(e)
    #         if len(e) == 2 or len(e) == 4 or len(e) == 3 or len(e) == 7:
    #             c += 1

    # print(c)

    sum = 0

    for l in data:
        # print(l)
        conf = find_config(*l)
        print(conf)
        sum += int("".join(list(map(str, [conf["".join(sorted(n))] for n in l[1]]))))
        # sum += int("".join([conf["".join(sorted(n))] for n in l[1]]))    

    print(sum)

