"""
Neil Vakharia
Int5 5/5
Enloe
Potter
Contest 3
"""

def replace_nots(s):
    s=s.replace('~A','a')
    s=s.replace('~B','b')
    s=s.replace('~C','c')
    s=s.replace('~D','d')
    return s
         
def fill_board(board):
    for i in range(0,4):
        for j in range(0,4):
            if board[i][j]!="3":
                board[i][j]="1"
    return board

def clean_board(board):
    for i in range(0,4):
        for j in range(0,4):
            if board[i][j]=="3":
                board[i][j]="1"
    return board

def lock_board(board):
    for i in range(0,4):
        for j in range(0,4):
            if board[i][j]=="1":
                board[i][j]="3"
    return board

def erase_row(board, r1, r2):
    for i in range(0,4):
        if board[r1][i]!='3':
            board[r1][i]='0'
        if board[r2][i]!='3':
            board[r2][i]='0'
    return board

def erase_col(board, c1, c2):
    for i in range(0,4):
        if board[i][c1]!='3':
            board[i][c1]='0'
        if board[i][c2]!='3':
            board[i][c2]='0'
    return board

def erase(board, char):
    if char=='A': board=erase_col(board,2,3)
    elif char=='a': board=erase_col(board,0,1)
    elif char=='B': board=erase_row(board,2,3)
    elif char=='b': board=erase_row(board,0,1)
    elif char=='C': board=erase_col(board,0,3)
    elif char=='c': board=erase_col(board,1,2)
    elif char=='D': board=erase_row(board,0,3)
    elif char=='d': board=erase_row(board,1,2)
    return board
    
filename = 'acsl3input.txt'

with open(filename) as file_object:
    for line in file_object:

        expression=line.rstrip()
        terms=[]
        board=[['','','',''],['','','',''],['','','',''],['','','','']]
        output=""

        #replace the nots
        expression=replace_nots(expression)
        #fill board
        board=fill_board(board)

        #break into terms
        plus_pos=expression.find('+')
        while plus_pos!=-1:
            terms.append(expression[:plus_pos])
            expression=expression[plus_pos+1:]
            plus_pos=expression.find('+')
        terms.append(expression)

        #work with board
        for term in terms:
            board=fill_board(board)
            for char in term:
                erase(board, char)
            board=lock_board(board)
        board=clean_board(board)      

        #get output
        for row in board:
            binary=""
            answer=""
            for letter in row:
                binary=binary+letter
            answer=hex(int(binary,2))
            answer=answer[len(answer)-1:].upper()
            output=output+answer
        print(output)