# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem #3 – Junior Division
# Author :  Aadit Mathur
# Language: Python 3.x
# -----------------------------------------------------------------------------------------


import ovcc_grader_strings

def hexToBinary(number_):
    number = str(number_)
    #The conversion of hexadecimal numbers to decimal numbers
    if number == 'A':
        number = 10
    elif number == 'B':
        number = 11
    elif number == 'C':
        number = 12
    elif number == 'D':
        number = 13
    elif number == 'E':
        number = 14
    elif number == 'F':
        number = 15

    number = int(number)
    newNumList = [0, 0, 0, 0]

    #Conversion to binary
    if number >= 8:
        newNumList[0] = 1
        number -= 8
    if number >= 4:
        newNumList[1] = 1
        number -= 4
    if number >= 2:
        newNumList[2] = 1
        number -= 2
    if number >= 1:
        newNumList[3] = 1
        number -= 1
    return newNumList

#Finds the representation of a term at a certain point for 3 variables(0,0 becomes AB~C, etc.)
def findIdOfVietch(r, c):
    theId = ['', '', '']
    #Identifies for B or ~B
    if r == 0:
        theId[1] = 'B'
    elif r == 1:
        theId[1] = '~B'
    #Identifies for A or ~A
    if c == 0 or c == 1:
        theId[0] = 'A'
    elif c == 2 or c == 3:
        theId[0] = '~A'
    #Identifies for C or ~C
    if c == 1 or c == 2:
        theId[2] = 'C'
    elif c == 0 or c == 3:
        theId[2] = '~C'
    return ''.join(theId)

#Cancels out negations, uses a shortcut for groups that you keep the common variable
def simplifyIds(id1, id2, id3=None, id4=None):
    simplifiedId = ['', '', '']
    if id3 == None and id4 == None:
        #The real logic
        if ('A' in id1) and ('A' in id2) and ('~A' not in id1) and ('~A' not in id2):
            simplifiedId[0] = 'A'
        if ('~A' in id1) and ('~A' in id2):
            simplifiedId[0] = '~A'
        if ('B' in id1) and ('B' in id2) and ('~B' not in id1) and ('~B' not in id2):
            simplifiedId[1] = 'B'
        if ('~B' in id1) and ('~B' in id2):
            simplifiedId[1] = '~B'
        if ('C' in id1) and ('C' in id2) and ('~C' not in id1) and ('~C' not in id2):
            simplifiedId[2] = 'C'
        if ('~C' in id1) and ('~C' in id2):
            simplifiedId[2] = '~C'
        #In case things can't be simplified
        if simplifiedId == ['', '', '']:
            return ' + '.join([id1, id2])
        #Otherwise, return the simplified id
        else:
            return ''.join(simplifiedId)
    else:
        #The real logic
        if ('A' in id1) and ('A' in id2) and ('A' in id3) and ('A' in id4) and ('~A' not in id1) and ('~A' not in id2) and ('~A' not in id3) and ('~A' not in id4):
            simplifiedId[0] = 'A'
        if ('~A' in id1) and ('~A' in id2) and ('~A' in id3) and ('~A' in id4):
            simplifiedId[0] = '~A'
        if ('B' in id1) and ('B' in id2) and ('B' in id3) and ('B' in id4) and ('~B' not in id1) and ('~B' not in id2) and ('~B' not in id3) and ('~B' not in id4):
            simplifiedId[1] = 'B'
        if ('~B' in id1) and ('~B' in id2) and ('~B' in id3) and ('~B' in id4):
            simplifiedId[1] = '~B'
        if ('C' in id1) and ('C' in id2) and ('C' in id3) and ('C' in id4) and ('~C' not in id1) and ('~C' not in id2) and ('~C' not in id3) and ('~C' not in id4):
            simplifiedId[2] = 'C'
        if ('~C' in id1) and ('~C' in id2) and ('~C' in id3) and ('~C' in id4):
            simplifiedId[2] = '~C'
        #In case things can't be simplified
        if simplifiedId == ['', '', '']:
            return ' + '.join([id1, id2, id3, id4])
        #Otherwise, return the simplified id
        else:
            return ''.join(simplifiedId)
#Finds groups of the first priority: 4 in a row
def check4row(T, curr_row):
    #Check for 4 in a row
    #Shortcut: since there is no wraparound for rows just check column 0 and the ones in front
    if T[curr_row][0] == 1 and T[curr_row][1] == 1 and T[curr_row][2] == 1 and T[curr_row][3] == 1:
        #Return the row that they all correspond to
        if curr_row == 0:
            T[0][0] = 0
            T[0][1] = 0
            T[0][2] = 0
            T[0][3] = 0
            return 'B'
        elif curr_row == 1:
            T[1][0] = 0
            T[1][1] = 0
            T[1][2] = 0
            T[1][3] = 0
            return '~B'
#The 2nd priority: Look for a group of 4 in columns
def check4squareWithoutWrap(T, curr_row, curr_col):
    #Check for square without wrap
    if T[curr_row][curr_col] == 1:
        #The scenario for a square
        #1 1
        #1 1
        try:
            if T[curr_row+1][curr_col] == 1:
                if T[curr_row][curr_col+1] == 1:
                    if T[curr_row+1][curr_col+1] == 1:
                        T[curr_row][curr_col] = 0
                        T[curr_row+1][curr_col] = 0
                        T[curr_row][curr_col+1] = 0
                        T[curr_row+1][curr_col+1] = 0
                        return simplifyIds(findIdOfVietch(curr_row, curr_col), findIdOfVietch(curr_row+1, curr_col), findIdOfVietch(curr_row, curr_col+1), findIdOfVietch(curr_row+1, curr_col+1))
        except IndexError:
            pass
#The 4th priority: Look for a group of 2 horizontally
def check4twoRowWithoutWrap(T, curr_row, curr_col):
    if T[curr_row][curr_col] == 1:
        try:
            if T[curr_row + 1][curr_col] == 1:
                T[curr_row][curr_col] = 0
                T[curr_row+1][curr_col] = 0
                return simplifyIds(findIdOfVietch(curr_row, curr_col), findIdOfVietch(curr_row+1, curr_col))
        except IndexError:
            pass
#The 5th priority: Look for a group of 2 vertically
def check4twoColWithoutWrap(T, curr_row, curr_col):
    if T[curr_row][curr_col] == 1:
        try:
            if T[curr_row][curr_col + 1] == 1:
                T[curr_row][curr_col] = 0
                T[curr_row][curr_col+1] = 0
                return simplifyIds(findIdOfVietch(curr_row, curr_col), findIdOfVietch(curr_row, curr_col+1))
        except IndexError:
            pass

def theFinalFunction(a,b):
    T = [hexToBinary(a), hexToBinary(b)]
    rows = len(T)
    cols = len(T[0])
    theAnswer = []
    #Priority 1
    aA = check4row(T, 0)
    if aA != None:
        theAnswer.append(aA)
    aB = check4row(T, 1)
    if aB != None:
        theAnswer.append(aB)
    #Priority 2
    for r in range(0,rows):
        for c in range(0,cols):
            b = check4squareWithoutWrap(T, r, c)
            if b != None:
                theAnswer.append(b)
    #Priority 3: Group 4 end column adjacent Xs
    if T[0][0] == 1 and T[1][0] == 1 and T[0][3] == 1 and T[1][3] == 1:
        T[0][0] = 0
        T[1][0] = 0
        T[0][3] = 0
        T[1][3] = 0
        theAnswer.append('~C')
    #Priority 4
    for r in range(0,rows):
        for c in range(0,cols):
            d = check4twoRowWithoutWrap(T, r, c)
            if d != None:
                theAnswer.append(d)
    #Priority 5
    for r in range(0,rows):
        for c in range(0,cols):
            e = check4twoColWithoutWrap(T, r, c)
            if e != None:
                theAnswer.append(e)
    #Priority 6: Group 2 end column adjacent Xs
    if T[0][0] == 1 and T[0][3] == 1:
        T[0][0] = 0
        T[0][3] = 0
        theAnswer.append('B~C')
    if T[1][0] == 1 and T[1][3] == 1:
        T[1][0] = 0
        T[1][3] = 0
        theAnswer.append('~B~C')
    #Priority 7: Represent single Xs
    for r in range(0,rows):
        for c in range(0,cols):
            if T[r][c] == 1:
                theAnswer.append(findIdOfVietch(r, c))
    return ' + '.join(theAnswer)

input = ovcc_grader_strings.readinput()
ovcc_grader_strings.clearoutput()
for lin in input:
    line = list(''.join(lin))
    S1 = line[0]
    S2 = line[1]
    ovcc_grader_strings.writeoutput(theFinalFunction(S1, S2))