#Abhinav Solasa
#Period 1A
#Int5
#Enloe
#Potter

def changegrid (grid, letter, position):
    if position == 0:
        if letter == "A":
            for r in range(0,4):
                for c in range(0,2):
                    grid[r][c] = 1
        elif letter == "B":
            for r in range(0,2):
                for c in range(0,4):
                    grid[r][c] = 1
        elif letter == "C":
            for r in range(0,4):
                for c in range(1,3):
                    grid[r][c] = 1
        elif letter == "D":
            for r in range(1,3):
                for c in range(0,4):
                    grid[r][c] = 1
        elif letter == "~A":
            for r in range(0,4):
                for c in range(2,4):
                    grid[r][c] = 1
        elif letter == "~B":
            for r in range(2,4):
                for c in range(0,4):
                    grid[r][c] = 1
        elif letter == "~C":
            for r in range(0,4):
                grid[r][0] = 1
                grid[r][3] = 1
        elif letter == "~D":
            for c in range(0,4):
                grid[0][c] = 1
                grid[3][c] = 1
                              
    else:
        if letter == "A":
           for r in range(0,4):
                for c in range(2,4):
                    grid[r][c] = 0
        elif letter == "B":
            for r in range(2,4):
                for c in range(0,4):
                    grid[r][c] = 0
        elif letter == "C":
            for r in range(0,4):
                grid[r][0] = 0
                grid[r][3] = 0
        elif letter == "D":
            for c in range(0,4):
                grid[0][c] = 0
                grid[3][c] = 0
        elif letter == "~A":
             for r in range(0,4):
                for c in range(0,2):
                    grid[r][c] = 0
        elif letter == "~B":
            for r in range(0,2):
                for c in range(0,4):
                    grid[r][c] = 0
        elif letter == "~C":
            for r in range(0,4):
                for c in range(1,3):
                    grid[r][c] = 0
        elif letter == "~D":
            for r in range(1,3):
                for c in range(0,4):
                    grid[r][c] = 0
            
    return (grid)
        
#filename = "E:/3int.txt"
filename = "D:/Python Resources/attempt 2/New Folder/acsl3input.txt"
with open(filename) as f:
    for line in f:
        terms = []
        element  = ""
        #print(line)
        for i in range(0, len(line)):
            if line[i] != '+':
                #print("Letter: " + str(line[i]))
                element += line[i]
                #print("Element: " + element)
            elif line[i] == '+':
                terms.append(element)
                element = ""
        terms.append(element)

        r = 4
        c = 4
        grid = [[0] * c for i in range(r)]

        #creates grid for locking terms
        secondarygrid = [[0] * c for i in range(r)]

        #Hard part-> turning corresponding cells on and off
        for v in range(0, len(terms)):
            #print("Term: " + terms[v])
            term = terms[v]
            letters = []
            i = 0
            while i < len(term):
                if term[i] == '~':
                    string = ""
                    string = string + term[i] + term[i+1]
                    letters.append(string)
                    i += 2
                    
                else:
                    letters.append(term[i])
                    i += 1
            #print("Letters: ")
            #print(letters)

            for i in range(0,len(letters)):
                grid = changegrid(grid, letters[i], i)
            #print("Term grid: ")
            #print(grid)
            if v == 0:
                for r in range(0,4):
                    for c in range(0,4):
                        secondarygrid[r][c] = grid[r][c]
            else:
                for r in range(0,4):
                    for c in range(0,4):
                        if grid[r][c] == 1:
                            secondarygrid[r][c] = 1
            

        #Converting to hexadecimal part of code 

        rownumbers = []
        for i in range(0,4):
            rownum = ""
            for f in range(0,4):
                rownum += str(secondarygrid[i][f])
            rownumbers.append(rownum)

        answer = ""
        for i in range(0, len(rownumbers)):
            convert = 0
            number = int(rownumbers[i])
            test = number
            count = 3
            while(count >=0):
                digit = int((test/(10**count)))
                test -= ((digit)*(10**count))
                convert += (digit*(2**count))
                count -=1
            convert = int(convert)
            num = str(hex(convert))
            num = num[2:]
            answer += str(num)
            
        print("Answer: " + answer)