summaryrefslogtreecommitdiff
path: root/solve.c
blob: f8947c9c9a5071ee0fca2891c3d3cf0ed4932d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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);

}