//NAME			    :	Kayla O'Donnell
//SCHOOL		    :	Kalamazoo AMSC
//DIVISION		    :	Intermediate-5
//PROGRAM		    :	Veitch
//CONTEST		    :	Round 3
//DATE			    :	25 February 2020
//DESCRIPTION	    :	Output the hexadecimal form of the Karnaugh
//				    :	map for a given boolean expression.

#include 
#include 
using namespace std;

int main()
{
	//signature
	cout << "Kayla O'Donnell\nAPCS\nIntermediate-5 Round 3\nVeitch\n\n\n";

	//defines Veitch diagram positions and other variables
	int a[16] = { 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 };
	int b[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
	int c[16] = { 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0 };
	int d[16] = { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 };
	string hex = "0123456789ABCDEF";
    ifstream fin("3int_testdata.txt");

	//repeats program
	for (int runs = 0; runs < 10; ++runs)
	{
		//reads input and declares variables
		string exp, term;
		fin >> exp;
		bool yeeps[16] = { false };

		while (exp.length() > 0)
		{
			//sets the term
            if (exp.find('+') == -1)
            {
                term = exp;
                exp = "";
            }
            else
            {
                term = exp.substr(0, exp.find('+'));
                exp = exp.substr(exp.find('+') + 1, exp.length());
            }
            int state[4] = {-1, -1, -1, -1};
            if (term.find('A') != -1)
            {
                if (term.find('A') > 0 && term[term.find('A') - 1] == '~') state[0] = 0;
                else state[0] = 1;
            }
            if (term.find('B') != -1)
            {
                if (term.find('B') > 0 && term[term.find('B') - 1] == '~') state[1] = 0;
                else state[1] = 1;
            }
            if (term.find('C') != -1)
            {
                if (term.find('C') > 0 && term[term.find('C') - 1] == '~') state[2] = 0;
                else state[2] = 1;
            }
            if (term.find('D') != -1)
            {
                if (term.find('D') > 0 && term[term.find('D') - 1] == '~') state[3] = 0;
                else state[3] = 1;
            }

            //fills all boxes that can be filled by that term
            for (int i = 0; i < 16; ++i)
            {
                if ((state[0] == a[i] || state[0] == -1) && (state[1] == b[i] || state[1] == -1) && (state[2] == c[i] || state[2] == -1) && (state[3] == d[i] || state[3] == -1))
                {
                    yeeps[i] = true;
                }
            }
        }
    
        //outputs results
        cout << runs + 1 << ".\t";
        for (int i = 0; i < 16; i += 4)
        {
            cout << hex[yeeps[i] * 8 + yeeps[i + 1] * 4 + yeeps[i + 2] * 2 + yeeps[i + 3]];
        }
        cout << endl;
	}

    //exits program
    fin.close();
	return 0;
}