#Justin Lumpkin
#3/12/20
#Int 5
#Enloe
#Potter
#Contest 3

#filename ='intdata3.txt'
filename ='F:/3int.txt'


class Veitch():
    def __init__(self, t=[]):
        self.terms=t
        self.mat=[[0 for i in range(4)] for j in range(4)]
        self.gen_matrix()
    def gen_matrix(self):
        matrix =[[1 for i in range(4)] for j in range(4)]

        for t in terms:
            #print(t)
            if t.find('~A')!=-1:
                for i in range(4):
                    matrix[i][0]=0
                    matrix[i][1]=0
                t=t[:t.find('~A')]+t[t.find('~A')+2:]
            elif  t.find('A')!=-1:
                for i in range(4):
                    matrix[i][2]=0
                    matrix[i][3]=0
                t=t[:t.find('A')]+t[t.find('A')+1:]

            if t.find('~B')!=-1:
                for i in range(4):
                    matrix[0][i]=0
                    matrix[1][i]=0
                t=t[:t.find('~B')]+t[t.find('~B')+2:]
            elif  t.find('B')!=-1:
                for i in range(4):
                    matrix[2][i]=0
                    matrix[3][i]=0
                t=t[:t.find('B')]+t[t.find('B')+1:]

            if t.find('~C')!=-1:
                for i in range(4):
                    matrix[i][1]=0
                    matrix[i][2]=0
                t=t[:t.find('~C')]+t[t.find('~C')+2:]
                
            elif  t.find('C')!=-1:
                for i in range(4):
                    matrix[i][0]=0
                    matrix[i][3]=0
                t=t[:t.find('C')]+t[t.find('C')+1:]

            if t.find('~D')!=-1:
                for i in range(4):
                    matrix[1][i]=0
                    matrix[2][i]=0
                t=t[:t.find('~D')]+t[t.find('~D')+2:]

            elif  t.find('D')!=-1:
                for i in range(4):
                    matrix[0][i]=0
                    matrix[3][i]=0
                t=t[:t.find('D')]+t[t.find('D')+1:]
            self.mat=self.add(self.mat, matrix)
            matrix=[[1 for i in range(4)] for j in range(4)]
        self.to_hex()
    def add(self,m1, m2):
        for i in range(4):
            for j in range(4):
                if m2[i][j]==1:
                    m1[i][j]=1
        return m1
    def to_hex(self):
        self.hex=''
        for row in self.mat:
            line =''
            for i in range(4):
                line+=str(row[i])
            line = hex(int(line,2))[-1]
            self.hex+=line
        self.hex=self.hex.upper()
        print(self.hex)

with open(filename) as file_object:
    i=1
    print('Intermediate:\n')
    for line in file_object:
        terms =[]
        while line.find('+')!=-1:
            terms.append(line[:line.find('+')])
            line=line[line.find('+')+1:]
        if line[-1]=='\n':
            terms.append(line[:-1])
        else:
            terms.append(line)
       
        print(str(i)+') ', end ='')
        v=Veitch(terms)
        i+=1

        #print()