% output stores final result
% input - matrix given by user
function output = lookup_solve_3(input)
	output = [0, 0, 0; 0, 0, 0; 0, 0, 0];
	% input used to store state of game after every press of button
	% presses (r,c) button and toggles the neighbouring buttons too
	function toggle(r, c)			
		input(r, c) = ~input(r, c);
		if r - 1 >= 1
			input(r - 1, c) = ~input(r - 1, c);
		end
		if r + 1 <= 3
			input(r + 1, c) = ~input(r + 1, c);
		end
		if c - 1 >= 1
			input(r, c - 1) = ~input(r, c - 1);
		end
		if c + 1 <= 3
			input(r, c + 1) = ~input(r, c + 1);
		end
	endfunction

	% takes a row and the button to press in it as input and presses corresponding buttons
	function toggle_row(row, but_press)
		for k = 1 : 3
			if (but_press(1, k) == 1)
				toggle(row, k);
				output(row, k) = ~output(row, k);	
			end
		endfor	
	endfunction
	
	% the table given in problem statement
	bot_top_toggle = [0, 1, 1;
					  1, 1, 1;
					  1, 0, 0;
					  1, 1, 0;
					  1, 0, 1;
					  0, 0, 1;
					  0, 1, 0];
	
	while (true)
		% 1st and 2nd row made zero
		for i = 1 : 2
			toggle_row(i + 1, input(i, :) );
		endfor

		% binary_equivalent of last row for easy access
		bottom_binary = 4 * input(3, 1) + 2 * input(3, 2) + input(3, 3);
		
		if (bottom_binary ~= 0)
			% toggling top row according to state of bottom row
			toggle_row(1, bot_top_toggle(bottom_binary, :));
		else 
			break;
		end
	endwhile
endfunction


	
