# Name: Yikuan Sun
# Team: We Sense
# Intermediate Division
# ACSL Contest #3
# Date of Program: 03/07/20
# Grade: 7
# Language: Python 3
# Input Data Method: Read from file

"""
import sys
if int(str(sys.version).split()[0].split(".")[0]) < 3:
    print("Dear coach,\nI have politely asked you to use Python 3, but instead you run this on an outdated version of Python. This does not make me happy and I shall refuse to run this program.")
    sys.exit(">:(\n\nGet Python 3 plz")
"""

def myFunction(inputs):
    outputs = ["", "", "", "", ""]
    for counter_thing in range(5):
        try:
            grids = inputs[counter_thing].split("+")
            diagram = [["0","0","0","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"]]
            for i in range(len(grids)):
                grids[i] = "a".join(grids[i].split("~A"))
                grids[i] = "b".join(grids[i].split("~B"))
                grids[i] = "c".join(grids[i].split("~C"))
                grids[i] = "d".join(grids[i].split("~D"))
                selection_A = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_B = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_C = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_D = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_a = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_b = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_c = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                selection_d = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
                if "A" in grids[i]:
                    selection_A = [[1,1,0,0],[1,1,0,0],[1,1,0,0],[1,1,0,0]]
                if "a" in grids[i]:
                    selection_a = [[0,0,1,1],[0,0,1,1],[0,0,1,1],[0,0,1,1]]
                if "B" in grids[i]:
                    selection_B = [[1,1,1,1],[1,1,1,1],[0,0,0,0],[0,0,0,0]]
                if "b" in grids[i]:
                    selection_b = [[0,0,0,0],[0,0,0,0],[1,1,1,1],[1,1,1,1]]
                if "C" in grids[i]:
                    selection_C = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
                if "c" in grids[i]:
                    selection_c = [[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1]]
                if "D" in grids[i]:
                    selection_D = [[0,0,0,0],[1,1,1,1],[1,1,1,1],[0,0,0,0]]
                if "d" in grids[i]:
                    selection_d = [[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                for j in range(len(diagram)):
                    for k in range(len(diagram[j])):
                        if selection_A[j][k] == 1 and selection_a[j][k] == 1 and selection_B[j][k] == 1 and selection_b[j][k] == 1 and selection_C[j][k] == 1 and selection_c[j][k] == 1 and selection_D[j][k] == 1 and selection_d[j][k] == 1:
                            diagram[j][k] = "1"
            for l in range(4):
                x = str(int(str("".join(diagram[l])), base=2))
                if x == "10":
                    x = "A"
                if x == "11":
                    x = "B"
                if x == "12":
                    x = "C"
                if x == "13":
                    x = "D"
                if x == "14":
                    x = "E"
                if x == "15":
                    x = "F"
                outputs[counter_thing] += x
        except:
            outputs[counter_thing] = "ERRORERRORERROROHNOSUCKSTOBEME!!!!!!!!!"
    return outputs

def input_reader(filename):
    try:
        user_input = open(filename).read()
        print("--Success in reading input data--\n   Obtained the following inputs:\n\n" + str(user_input) + "\n")
    except:
        print("Failure in reading file, please key in inputs:")
        user_input = str(input("1. ")) + "\n" + str(input("2. ")) + "\n" + str(input("3. ")) + "\n" + str(input("4. ")) + "\n" + str(input("5. "))
        print()
    return user_input

output = myFunction(input_reader(input("FILENAME?\n > ")).split("\n"))
print("\n\tOutputs:")
for i in range(5):
    print(str(i + 1) + ". " + output[i])