##hunter O'Connor
##3/12/2020
##int5
##Enloe
##Potter
##Contest 3


  def invertLetter(letter):
    if len(letter) > 1:
        letter = letter[1]
    else:
        letter = '~'+letter
    return letter
def fillSquare(square,letter,val):
    if letter == 'A':
        for c in range(0,2):
            for r in range (0,4):
                square[r][c] = val
    elif letter == '~A':
        for c in range(2,4):
            for r in range (0,4):
                square[r][c] = val
    elif letter == 'B':
        for c in range(0,4):
            for r in range (0,2):
                square[r][c] = val
    elif letter == '~B':
        for c in range(0,4):
            for r in range (2,4):
                square[r][c] = val
    elif letter == 'C':
        for c in [1,2]:
            for r in range (0,4):
                square[r][c] = val
    elif letter == '~C':
        for c in [0,3]:
            for r in range (0,4):
                square[r][c] = val
    elif letter == 'D':
        for c in range(0,4):
            for r in [1,2]:
                square[r][c] = val
    elif letter == '~D':
        for c in range(0,4):
            for r in [0,3]:
                square[r][c] = val
##    print(letter)
##    for row in square:
##        print(row)
    return square
    
def createSquare(terms):
    squares = []
    for term in terms:
        term = term + ' '
        square  = [[0,0,0,0],
                   [0,0,0,0],
                   [0,0,0,0],
                   [0,0,0,0]]
        lettersToFill = []
        for letter in ['A','B','C','D']:
            if term.find(letter)>-1:
                lettersToFill.append(term[:term.find(letter)+1])
                term = term[term.find(letter)+1:]
        #print(terms)
        square = fillSquare(square,lettersToFill[0],1)
        for letter in lettersToFill[1:]:
            square = fillSquare(square,invertLetter(letter),0)
        squares.append(square)
    for r in range(0,4):
        for c in range(0,4):
            for square in squares[1:]:
                if square[r][c]==1:
                    squares[0][r][c]=1
    #print(squares[0])
    return squares[0]
            


def acslInt(inpt):
    #format string
    terms = []
    while inpt.find('+')>-1:
        term = inpt[:inpt.find('+')].rstrip().strip()
        inpt = inpt[inpt.find('+')+1:].rstrip().strip()
        terms.append(term)
    terms.append(inpt)
    square = createSquare(terms)
    #print(square)
    ans = ''
    for row in square:
        digit = ''
        for c in row:
            digit += str(c)
        #print(digit)
        ans=ans+str(hex(int(digit,base=2))[2:].upper())
    #print(ans)
    return ans
            
        



if __name__=='__main__':
    #print([1,2,3,4,5,6][2:4])
    #filename = 'intTest.txt'
    filename = 'F:\\3int.txt'
    with open(filename) as file_object:     #opens file and assigns it to file_object
        lines = file_object.readlines()     #returns file_object as a list of
                                                #lines and puts it in lines
    trial = 1
    for line in lines:
        inpt=line.rstrip()
        print('Trial '+str(trial)+': \n input: '+inpt+'\n output: '+acslInt(inpt)+'\n')
        trial+=1