//NAME : Chase Szafranski
//SCHOOL : Kalamazoo AMSC
//DIVISION : Senior-5
//PROGRAM : ACSL Veitch
//CONTEST : Round 3
//DATE : 25 February 2020

#include 
#include 

using namespace std;

int main()
{
	ifstream fin ("3sr_testdata.txt");
	
	cout << "Chase Szafranski\nAdvanced CS\nSenior ACSL Veitch\n\n";
	int grid[4][4];
	char num;
	bool works;
	string answer, inStg;
	for (int run = 1; run <= 5; run++)
	{
		//Initialize
		for (int i = 0; i < 4; i++)
		{
			for (int j =0; j < 4; j++)
			{
				grid[i][j] = 0;
			}
		}
		answer = "";
		
		//Input
		for (int i = 0; i < 4; i++)
		{
			fin >> num;
			inStg = "";
			if (num > 64)
				num -= 55;
			else
				num -= 48;
				
			for (int j =3; j >-1; j--)
			{
				if (num%2)
					inStg = "1" + inStg;
				else
					inStg = "0" + inStg;
				num /= 2;
			}
			while (inStg.length() < 4)
				inStg = "0" + inStg;
			for (int j = 0; j < 4; j++)
				if (inStg[j] == '1')
					grid[i][j] = 1;
		}
		
		//Output grid
		/*for (int i = 0; i < 4; i++)
		{
			for (int j =0; j < 4; j++)
			{
				cout << grid[i][j];
			}
			cout << "\n";
		}*/
		
		//8 adjacent spaces
		//Rows top to bottom
		for (int r = 0; r < 3; r++)
		{
			works = 1;
			for (int c = 0; c < 4 && works; c++)
			{
				if (!grid[r][c] || !grid[r+1][c])
				{
					works = 0;
				}
			}
			if (works)
			{
				for (int c = 0; c < 4; c++)
				{
					grid[r][c] = 0;
					grid[r+1][c] = 0;
				}
				if (r == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "B";
				}
				else if (r == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "D";
				}
				else if (r == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~B";
				}
			}
		}
		//Columns left to right
		for (int c = 0; c < 3; c++)
		{
			works = 1;
			for (int r = 0; r < 4 && works; r++)
			{
				if (!grid[r][c] || !grid[r][c+1])
				{
					works = 0;
				}
			}
			if (works)
			{
				for (int r = 0; r < 4; r++)
				{
					grid[r][c] = 0;
					grid[r][c+1] = 0;
				}
				if (c == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "A";
				}
				else if (c == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "C";
				}
				else if (c == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~A";
				}
			}
		}
		//End rows
		works = 1;
		for (int c = 0; c < 4 && works; c++)
		{
			if (!grid[0][c] || !grid[3][c])
				works = 0;
		
		}
		if (works)
		{
			for (int c = 0; c < 4; c++)
			{
				grid[0][c] = 0;
				grid[3][c] = 0;
			}
			if (answer.length() > 0)
					answer += "+";
				answer += "~D";
		}
		//End columns
		works = 1;
		for (int r = 0; r < 4 && works; r++)
		{
			if (!grid[r][0] || !grid[r][3])
				works = 0;
		}
		if (works)
		{
			for (int r = 0; r < 4; r++)
			{
				grid[r][0] = 0;
				grid[r][3] = 0;
			}
			if (answer.length() > 0)
					answer += "+";
				answer += "~C";
		}
		
		//4 adjacent spaces
		//Rows top down
		for (int r = 0; r < 4; r++)
		{
			works = 1;
			for (int c = 0; c < 4 && works; c++)
			{
				if (!grid[r][c])
					works = 0;
			}
			if (works)
			{
				for (int c = 0; c < 4 && works; c++)
					grid[r][c] = 0;
				
				if (r == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "B~D";
				}
				else if (r == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "BD";
				}
				else if (r == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~BD";
				}
				else if (r == 3)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~B~D";
				}
			}
		}
		//Columns left then right
		for (int c = 0; c < 4; c++)
		{
			works = 1;
			for (int r = 0; r < 4 && works; r++)
			{
				if (!grid[r][c])
					works = 0;
			}
			if (works)
			{
				for (int r = 0; r < 4 && works; r++)
					grid[r][c] = 0;
				
				if (c == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "A~C";
				}
				else if (c == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "AC";
				}
				else if (c == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~AC";
				}
				else if (c == 3)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~A~C";
				}
			}
		}
		//Blocks of four
		for(int r = 0; r < 3; r++)
		{
			for (int c = 0; c < 3; c++)
			{
				works = 1;
				for (int i = 0; i < 2 && works; i++)
				{
					for (int j = 0; j < 2 && works; j++)
					{
						if (!grid[r+i][c+j])
							works = 0;
					}
				}
				if (works)
				{
					for (int i = 0; i < 2 && works; i++)
					{
						for (int j = 0; j < 2 && works; j++)
						{
							grid[r+i][c+j] = 0;
						}
					}
					if (r == 0)
					{
						if (c == 0)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "AB";
						}
						else if (c == 1)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "BC";
						}
						else if (c == 2)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "~AB";
						}
					}
					else if (r == 1)
					{
						if (c == 0)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "AD";
						}
						else if (c == 1)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "CD";
						}
						else if (c == 2)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "~AD";
						}
					}
					else if (r == 2)
					{
						if (c == 0)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "A~B";
						}
						else if (c == 1)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "~BC";
						}
						else if (c == 2)
						{
							if (answer.length() > 0)
								answer += "+";
							answer += "~A~B";
						}
					}
				}
			}
		}
		//4 end-row
		for (int r = 0; r < 3; r++)
		{
			if (grid[r][0] && grid[r][3] && grid[r+1][0] && grid[r+1][3])
			{
				if (r == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "B~C";
				}
				else if (r == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~CD";
				}
				else if (r == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~B~C";
				}
				grid[r][0] = 0;
				grid[r][3] = 0;
				grid[r+1][0] = 0;
				grid[r+1][3] = 0;
			}
		}
		//4 end-column
		for (int c = 0; c < 3; c++)
		{
			if (grid[0][c] && grid[3][c] && grid[0][c+1] && grid[3][c+1])
			{
				if (c == 0)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "A~D";
				}
				else if (c == 1)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "C~D";
				}
				else if (c == 2)
				{
					if (answer.length() > 0)
						answer += "+";
					answer += "~A~D";
				}
				grid[0][c] = 0;
				grid[3][c] = 0;
				grid[0][c+1] = 0;
				grid[3][c+1] = 0;
			}
		}
		//4 corners
		if (grid[0][0] && grid[0][3] && grid[3][0] && grid[3][3])
		{
			if (answer.length() > 0)
				answer += "+";
			answer += "~C~D";
			grid[0][0] = 0;
			grid[0][3] = 0;
			grid[3][0] = 0;
			grid[3][3] = 0;
		}
		
		//2 adjacent
		//Rows
		for (int r = 0; r < 4; r++)
		{
			for (int c = 0; c < 3; c++)
			{
				if (grid[r][c] && grid[r][c+1])
				{
					grid[r][c] = 0;
					grid[r][c+1] = 0;
					if (answer.length() > 0)
						answer += "+";
					if (r == 0)
					{
						if (c == 0)
						{
							answer += "AB~D";
						}
						else if (c == 1)
						{
							answer += "BC~D";
						}
						else if (c == 2)
						{
							answer += "~AB~D";
						}
					}
					else if (r == 1)
					{
						if (c == 0)
						{
							answer += "ABD";
						}
						else if (c == 1)
						{
							answer += "BCD";
						}
						else if (c == 2)
						{
							answer += "~ABD";
						}
					}
					else if (r == 2)
					{
						if (c == 0)
						{
							answer += "A~BD";
						}
						else if (c == 1)
						{
							answer += "~BCD";
						}
						else if (c == 2)
						{
							answer += "~A~BD";
						}
					}
					else if (r == 3)
					{
						if (c == 0)
						{
							answer += "A~B~D";
						}
						else if (c == 1)
						{
							answer += "~BC~D";
						}
						else if (c == 2)
						{
							answer += "~A~B~D";
						}
					}
				}
			}
		}
		//Columns
		for (int c = 0; c < 4; c++)
		{
			for (int r = 0; r < 3; r++)
			{
				if (grid[r][c] && grid[r+1][c])
				{
					grid[r][c] = 0;
					grid[r+1][c] = 0;
					if (answer.length() > 0)
						answer += "+";
					if (c == 0)
					{
						if (r == 0)
						{
							answer += "AB~C";
						}
						else if (r == 1)
						{
							answer += "A~CD";
						}
						else if (r == 2)
						{
							answer += "A~B~C";
						}
					}
					else if (c == 1)
					{
						if (r == 0)
						{
							answer += "ABC";
						}
						else if (r == 1)
						{
							answer += "ACD";
						}
						else if (r == 2)
						{
							answer += "A~BC";
						}
					}
					else if (c == 2)
					{
						if (r == 0)
						{
							answer += "~ABC";
						}
						else if (r == 1)
						{
							answer += "~ACD";
						}
						else if (r == 2)
						{
							answer += "~A~BC";
						}
					}
					else if (c == 3)
					{
						if (r == 0)
						{
							answer += "~AB~C";
						}
						else if (r == 1)
						{
							answer += "~A~CD";
						}
						else if (r == 2)
						{
							answer += "~A~B~C";
						}
					}
				}
			}
		}
		//End of rows
		for (int r = 0; r < 4; r++)
		{
			if (grid[r][0] && grid[r][3])
			{
				grid[r][0] = 0;
				grid[r][3] = 0;
				
				if (answer.length() > 0)
					answer += "+";
				if (r == 0)
				{
					answer += "B~C~D";
				}
				else if (r == 1)
				{
					answer += "B~CD";
				}
				else if (r == 2)
				{
					answer += "~B~CD";
				}
				else if (r == 3)
				{
					answer += "~B~C~D";
				}
			}
		}
		//End of columns
		for (int c = 0; c < 4; c++)
		{
			if (grid[0][c] && grid[3][c])
			{
				grid[0][c] = 0;
				grid[3][c] = 0;
				
				if (answer.length() > 0)
					answer += "+";
				if (c == 0)
				{
					answer += "A~C~D";
				}
				else if (c == 1)
				{
					answer += "AC~D";
				}
				else if (c == 2)
				{
					answer += "~AC~D";
				}
				else if (c == 3)
				{
					answer += "~A~C~D";
				}
			}
		}
		
		//Single
		for (int r = 0; r < 4; r++)
		{
			for (int c = 0; c < 4; c++)
			{
				if (grid[r][c])
				{
					grid[r][c] = 0;
					if (answer.length() > 0)
						answer += "+";
					if (c < 2)
					{
						answer += "A";
					}
					else
					{
						answer += "~A";
					}
					if (r < 2)
						answer += "B";
					else
						answer += "~B";
					if (c == 1 || c == 2)
					{
						answer += "C";
					}
					else
						answer += "~C";
					if (r == 1 || r == 2)
						answer += "D";
					else
						answer += "~D";
				}
			}
		}
		
		cout << run << ".  "  << answer << endl;
	}
}