def or_2d(list1, list2):
    def or_operator(inlist1, inlist2):
        ret1 = []
        for j in range(len(inlist1)):
            if inlist1[j] == 1 or inlist2[j] == 1:
                ret1.append(1)
            else:
                ret1.append(0)
        return ret1

    ret = []
    for i in range(len(list1)):
        ret.append(or_operator(list1[i], list2[i]))
    return ret


def and_2d(list1, list2):
    def and_operator(inlist1, inlist2):
        return [inlist1[j] * inlist2[j] for j in range(len(inlist1))]

    ret = []
    for i in range(len(list1)):
        ret.append(and_operator(list1[i], list2[i]))
    return ret


# noinspection PyShadowingBuiltins
def not_2d(list):
    def not_operator(l):
        ret1 = []
        for j in l:
            if j == 0:
                ret1.append(1)
            else:
                ret1.append(0)
        return ret1

    ret = []
    for i in range(len(list)):
        ret.append(not_operator(list[i]))
    return ret


def arr_to_hex_2d(arr):
    def arr_to_hex(inarr):
        place = 3
        tot = 0
        for i in range(len(inarr)):
            if inarr[i] == 1:
                tot += 2 ** place
            place -= 1
        return decToHex[tot]

    hex_str = ""
    for i in range(len(arr)):
        hex_str += arr_to_hex(arr[i])
    return hex_str


decToHex = {0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7",
            8: "8", 9: "9", 10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F"}

letterToArrayMap = {
    "A": [[1, 1, 0, 0],
          [1, 1, 0, 0],
          [1, 1, 0, 0],
          [1, 1, 0, 0]],
    "B": [[1, 1, 1, 1],
          [1, 1, 1, 1],
          [0, 0, 0, 0],
          [0, 0, 0, 0]],
    "C": [[0, 1, 1, 0],
          [0, 1, 1, 0],
          [0, 1, 1, 0],
          [0, 1, 1, 0]],
    "D": [[0, 0, 0, 0],
          [1, 1, 1, 1],
          [1, 1, 1, 1],
          [0, 0, 0, 0]]
}

inp = open("3int_sampledata.txt", "r").readlines()
for line in inp:
    veitchArray = [[0 for j in range(4)] for i in range(4)]
    line = line.strip("\n")
    line = line.strip(" ")
    split_inp = line.split("+")
    for i in range(len(split_inp)):
        and_arr = [[1 for o in range(4)] for k in range(4)]
        term = split_inp[i]
        not_flag = False
        for j in range(len(term)):
            if term[j] == "~":
                not_flag = True
                continue
            else:
                second_arr = letterToArrayMap[term[j]]
                if not_flag:
                    not_flag = False
                    second_arr = not_2d(second_arr)
            and_arr = and_2d(and_arr, second_arr)
        veitchArray = or_2d(veitchArray, and_arr)

    print(arr_to_hex_2d(veitchArray))