diff options
| author | David Phillips <dbphillipsnz@gmail.com> | 2016-03-09 00:21:35 +1300 | 
|---|---|---|
| committer | David Phillips <david@sighup.nz> | 2019-03-10 21:15:30 +1300 | 
| commit | 6bc7fdab077b1877f71b69f4b2c1e8046a302e9b (patch) | |
| tree | ba7815f0557ed9cef1056ba9c4ba81584c1650bc | |
| download | sudoku-6bc7fdab077b1877f71b69f4b2c1e8046a302e9b.tar.xz | |
Initial working copy, buggy
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | cell.h | 7 | ||||
| -rw-r--r-- | display.c | 26 | ||||
| -rw-r--r-- | display.h | 1 | ||||
| -rw-r--r-- | solve.c | 174 | 
6 files changed, 218 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b22666a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +solve diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09fc878 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS += -Werror -Wall + +all: solve + +solve: display.o + +clean: +	rm -f *.o solve  @@ -0,0 +1,7 @@ +struct cell +{ +	char val; +	char not[9]; +} cell; + + diff --git a/display.c b/display.c new file mode 100644 index 0000000..f147570 --- /dev/null +++ b/display.c @@ -0,0 +1,26 @@ +#include "cell.h" + +#include <stdio.h> + +void display(struct cell board[9][9]) +{ +	int x; +	int y; +	int val; +	for (y = 0; y < 9; y++) +	{ +		if (y % 3 == 0 && y != 0) +			printf("------+------+------\n"); +		for (x = 0; x < 9; x++) +		{ +			if (x % 3 == 0 && x != 0) +				printf("|"); + +			val = board[x][y].val; + +			printf("%c ", val == 0? ' ' : val+'0'); +		} +		printf("\n"); +	} +	printf("\n"); +} diff --git a/display.h b/display.h new file mode 100644 index 0000000..b3b5a3d --- /dev/null +++ b/display.h @@ -0,0 +1 @@ +void display(struct cell board[9][9]); @@ -0,0 +1,174 @@ +#include <stdio.h> +#include <string.h> + +#include "cell.h" +#include "display.h" + +void update_not_row_col(struct cell b[9][9]) +{ +	int x = 0; +	int y = 0; +	int i = 0; +	int val = 0; + +	for (y = 0; y < 9; y++) +	{ +		for (x = 0; x < 9; x++) +		{ +			val = b[x][y].val; +			if (val != 0) +			{ +				for (i = 0; i < 9; i++) +				{ +					b[i][y].not[val - 1] = 1; +					b[x][i].not[val - 1] = 1; +				} +			} +		} +	} +} + +void not_to_val_row_col(struct cell b[9][9]) +{ +	int x = 0; +	int y = 0; +	int i = 0; +	int val = 0; +	int total = 0; + +	for (y = 0; y < 9; y++) +	{ +		total = 0; +		for (x = 0; x < 9; x++) +		{ +			if (b[x][y].val != 0) +				continue; + +			for (i = 0; i < 9; i++) +			{ +				if (b[x][y].not[i]) +				{ +					total++; +				} else { +					val = i + 1; +				} +			} +			if (total == 8) +				b[x][y].val = val; +		} +	} +} + + +void not_to_val_cell(struct cell b[9][9]) +{ +	int cx = 0; +	int cy = 0; +	int x = 0; +	int y = 0; +	int n = 0; +	int potentials = 0; +	int okx, oky; +	struct cell c; + +	/* refactor this crap */ +	for (cy = 0; cy < 9; cy += 3) +	{ +		for (cx = 0; cx < 9; cx += 3) +		{ +			/* for each val that has to be in cell */ +			for (n = 1; n <= 9; n++) +			{ +				for (y = 0; y < 3; y++) +				{ +					for (x = 0; x < 3; x++) +					{ +						c = b[cx+x][cy+y]; +						if (c.val == n) +						{ +							potentials = 0; +							y = 3; +							break; +						} +						if (c.val) +							continue; + +						if (c.not[n-1] == 0) +						{ +							potentials++; +							okx = x; +							oky = y; +						} +					} +				} +				if (potentials == 1) +				{ +					b[cx+okx][cy+oky].val = n; +					potentials = 0; +				} +			} +		} +	} +} + +int main(int argc, char **argv) +{ +//	int x = 0; +//	int y = 0; +	struct cell board[9][9]; + +	memset(board, 0, sizeof(board)); + + +	board[7][0].val = 3; +	board[4][1].val = 3; +	board[0][7].val = 3; +	board[1][4].val = 3; + +	/* dummy board taken from the guardian lol */ +	/* FIXME: have  a way to input boards, silly */ +	board[0][0].val = 7; +	board[0][4].val = 5; + +	board[1][2].val = 5; +	board[1][4].val = 9; + +	board[2][1].val = 6; +	board[2][2].val = 4; +	board[2][5].val = 2; + +	board[3][0].val = 1; +	board[3][5].val = 9; +	board[3][6].val = 7; + +	board[4][0].val = 9; +	board[4][2].val = 6; +	board[4][3].val = 4; +	board[4][7].val = 8; +	board[4][8].val = 5; + +	board[5][4].val = 2; + +	board[6][4].val = 8; +	board[6][6].val = 5; +	board[6][7].val = 6; + +	board[7][1].val = 8; +	board[7][6].val = 3; + +	board[8][0].val = 4; +	board[8][4].val = 1; +	board[8][5].val = 7; +	board[8][8].val = 8; + + + +	display(board); + +	update_not_row_col(board); +	not_to_val_cell(board); +	not_to_val_row_col(board); + +	display(board); + +}  | 
