#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
Nurikabe Solution Checker - Skeleton File
The Problem file format is space delimited numbers , the actual number where applicable and 0 for white spaces with each line being a row of data
The Solution file format is similar, with 0 representing white cells and 1 representing black cells

*/
/*
Function Headers / Global  - Modify as required
*/

int checkBoard (int* problem, int* solution);
void solveBoard(int* problem , int* solution);

int main(int argc, char** argv) {
/* DO NOT MODIFY*/
	int* problem = (int *)malloc(sizeof(int)*25) ;
	int* solution = (int *)malloc(sizeof(int)*25);
	
	int i;
	char linebuffer[100];
	int op_mode=0;


	if (argc!=4 && argc!=3) {
		fprintf(stderr,"Invalid Parameters , Expect (<Mode-check/solve>, Default = check) <Problem Input file name> <Solution Input file name> \n");
		exit(200);
	}
	
	if (argc==4) {
	
	
	if ( strcmp(argv[1],"check")==0 ) {
		op_mode=0;
	}
	else if ( strcmp(argv[1],"solve")==0  ) {
		op_mode=1;
	}
	else {
		fprintf(stderr,"Invalid operation - supports only check and solve \n");
		exit(205);

	}

	}
	FILE* prob_file = fopen(argv[1+argc-3],"r");
	if (prob_file==NULL) {
		fprintf(stderr,"Cannot Open Problem File\n");
		exit(201);

	}

	FILE* soln_file = fopen(argv[2+argc-3],(op_mode==0) ? "r" : "w");
	if (soln_file==NULL) {
		fprintf(stderr,"Cannot Open Solution File\n");
		exit(202);

	}		

	printf("problem board:\n");
	for(i=0;i<5;i++) {
		fgets((char*)(&linebuffer),100,prob_file);
		sscanf((char*)(&linebuffer),"%d %d %d %d %d",&problem[5*i + 0],&problem[5*i + 1],&problem[5*i + 2],&problem[5*i + 3],&problem[5*i + 4]);
		printf("%d %d %d %d %d\n", problem[5*i], problem[5*i + 1], problem[5*i + 2], problem[5*i + 3], problem[5*i+4]);
		
		if (op_mode==0) {
			fgets((char*)(&linebuffer),100,soln_file);
			sscanf((char*)(&linebuffer),"%d %d %d %d %d",&solution[5*i + 0],&solution[5*i + 1],&solution[5*i+2],&solution[5*i+3],&solution[5*i+4]);	
		}

	}	

	if (op_mode==1) solveBoard(problem,solution); 

	fclose(prob_file);
	if (op_mode==1) {
		for(i=0;i<5;i++) {
	
			fprintf(soln_file,"%d %d %d %d %d\n",solution[5*i+0],solution[5*i+1],solution[5*i+2],solution[5*i+3],solution[5*i+4]);	
		}
	}

	fclose(soln_file);

	if (op_mode==0) {

		if(checkBoard(problem,solution)==1) {	
			 printf("Valid Solution\n");
			 free(problem);
			 free(solution);
			 return 1;  
		}
		else {

			printf("Invalid Solution\n");
			free(problem);
			free(solution);
			return 0;
		}
	}
	else {
		free(problem);
		free(solution);
		return 0;
	}
}

//End Main

int checkBoard(int* problem , int* solution)  {
//YOUR ANSWER HERE - Return 1 if valid, 0 if invalid

}
void solveBoard(int* problem , int* solution)  {
//DO NOT FILL IN UNLESS EXPLICITLY NOTIFIED


}
