-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku-solver.c
429 lines (402 loc) · 11 KB
/
sudoku-solver.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <stdio.h> //printf, file, getline
#include <pthread.h> //multithreading
#include <unistd.h>
#include <sys/types.h> //types
#include <stdlib.h>
#include <semaphore.h> //sem_wait, sem_post, sem_t
#include <string.h> //strtok
#include <stdbool.h> //bool, true, false
#include <time.h> //clock_t
//constants
#define INPUTFILE "file/in.txt"
#define OUTPUTFILE "file/out.txt"
#define LIMIT 9 //max rows, max columns
#define MAXRETRIES_SINGLETHREADED 1 //max retries per row/diagonal w/ 1 thread
#define MAXRETRIES_MULTITHREADED 500 //max retries per thread before exiting
#define RETRY_WAIT_NS 10
/* note: MAXRETRIES is high to try to probabilistically get several full
runs at solving the board instead of guaranteeing it. To get a full
guarantee, I would be effectively making several threads act as one
also, there is a RETRY_WAIT_NS pause between each retry */
//new types
typedef struct board_t {
int board[LIMIT][LIMIT];
} board_t;
typedef struct thread_container_t {
int row, col;
int max_retries;
board_t *board_wrapper_ptr;
} thread_container_t;
//i/o functions
board_t readBoardFromFile(char *filepath);
void writeBoardToFile(int board[][LIMIT], char *filepath);
void printBoard(int board[][LIMIT]);
void printBoardComparison(int problem[][LIMIT], int solution[][LIMIT]);
//solving functions
void solveBoardInManyWays(board_t *wrapper);
void *solveSectionDiagonally(void *thread_container);
void *solveSectionHorizontally(void *thread_container);
int trySolve(int board[][LIMIT], int row, int col, int solved);
void sleep_ns(int ns);
//validation functions
bool isCorrect(int board[][LIMIT]);
int percentSolved(int board[][LIMIT]);
//global vars
int reference[LIMIT][LIMIT];
sem_t mutex;
int main(void) {
//initialize semaphores
if (sem_init(&mutex, 0, 1) < 0) { // 0 = multithreaded
fprintf(stderr, "ERROR: could not initialize &semaphore.\n");
exit(0);
}
//get problem board from input file, make a clone
board_t wrapper = readBoardFromFile(INPUTFILE);
memcpy(reference, wrapper.board, sizeof(int)*LIMIT*LIMIT);
//solve board in-place
solveBoardInManyWays(&wrapper);
//dump solution
printBoardComparison(reference, wrapper.board);
writeBoardToFile(wrapper.board, OUTPUTFILE);
//clean up
sem_destroy(&mutex);
return 0;
}
void solveBoardInManyWays(board_t *wrapper_ptr) {
//thread variables
pthread_t tids[17];
pthread_attr_t attrs[17];
thread_container_t curs[17];
int i;
clock_t begin, end;
//macro for board, c version of 'int board[][] = wrapper.board'
int (*board)[LIMIT] = wrapper_ptr->board;
//1 thread, horizontal
int tries = 0;
int percentSolvd = 0;
begin = clock();
while(percentSolvd < 100 && tries < 1000) { //1000 tries to get it right
for(i=0; i < 9; i++){
curs[i].row = 8 - i;
curs[i].col = 0;
curs[i].board_wrapper_ptr = wrapper_ptr;
curs[i].max_retries = MAXRETRIES_SINGLETHREADED;
solveSectionHorizontally(&curs[i]);
percentSolvd = percentSolved(board);
if(percentSolvd == 100)
break;
}
tries++;
}
end = clock();
printf("time taken: %.02lfms (horizontal solve, 1 thread) correct solution: %s\n", (double)(end - begin) / CLOCKS_PER_SEC*1000,
isCorrect(board) ? "true" : "false");
//reset board for next solve
memcpy(board, reference, sizeof(int)*LIMIT*LIMIT);
//1 thread, diagonal
tries = 0;
percentSolvd = 0;
begin = clock();
while(percentSolvd < 100 && tries < 1000) { //1000 tries to get it right
for(i=0; i < 17; i++){
if(i < 9){
curs[i].row = 8 - i;
curs[i].col = 0;
}
else {
curs[i].row = 0;
curs[i].col = i - 8;
}
curs[i].board_wrapper_ptr = wrapper_ptr;
curs[i].max_retries = MAXRETRIES_SINGLETHREADED;
solveSectionDiagonally(&curs[i]);
percentSolvd = percentSolved(board);
if(percentSolvd == 100)
break;
}
tries++;
}
end = clock();
printf("time taken: %.02lfms (diagonal solve, 1 thread) correct solution: %s\n", (double)(end - begin) / CLOCKS_PER_SEC*1000,
isCorrect(board) ? "true" : "false");
//reset board for next solve
memcpy(board, reference, sizeof(int)*LIMIT*LIMIT);
//9 threads, horizontal (one per row)
begin = clock();
for(i=0; i < 9; i++){
pthread_attr_init(&attrs[i]);
curs[i].row = 8 - i;
curs[i].col = 0;
curs[i].board_wrapper_ptr = wrapper_ptr;
curs[i].max_retries = MAXRETRIES_MULTITHREADED;
pthread_create(&tids[i], &attrs[i], solveSectionHorizontally, &curs[i]);
}
for(i=0; i < 9; i++)
pthread_join(tids[i], NULL);
end = clock();
printf("time taken: %.02lfms (horizontal solve, 9 threads) correct solution: %s\n", (double)(end - begin) / CLOCKS_PER_SEC*1000,
isCorrect(board) ? "true" : "false");
//reset board for next solve
memcpy(board, reference, sizeof(int)*LIMIT*LIMIT);
//17 threads, diagonal (one per diagonal)
begin = clock();
for(i=0; i < 17; i++){
pthread_attr_init(&attrs[i]);
if(i < 9){
curs[i].row = 8 - i;
curs[i].col = 0;
}
else{
curs[i].row = 0;
curs[i].col = i - 8;
}
curs[i].board_wrapper_ptr = wrapper_ptr;
curs[i].max_retries = MAXRETRIES_MULTITHREADED;
pthread_create(&tids[i], &attrs[i], solveSectionDiagonally, &curs[i]);
}
for(i=0; i < 17; i++)
pthread_join(tids[i], NULL);
end = clock();
printf("time taken: %.02lfms (diagonal solve, 17 threads) correct solution: %s\n", (double)(end - begin) / CLOCKS_PER_SEC*1000,
isCorrect(board) ? "true" : "false");
}
void *solveSectionDiagonally(void *thread_container) {
thread_container_t *container = thread_container;
int row = container->row;
int col = container->col;
int max_retries = container->max_retries;
//macro for board, c version of 'int board[][] = board'
int (*board)[LIMIT] = container->board_wrapper_ptr->board;
int solved = 0; //num of non-empty cells in diagonal
//caclulate how many cells this section contains (diagonal cells)
int toSolve;
if (row > col)
toSolve = LIMIT - row;
else
toSolve = LIMIT - col;
//keep going until all needed cells are solved or num of retries too high
int retries = 0;
int prevSolved = 0;
while (solved < toSolve && retries < max_retries) {
solved = 0;
//progress diagonally
int r = row, c = col;
while (r < LIMIT && c < LIMIT) {
sem_wait(&mutex);
//solve cell
if (board[r][c] == -1)
solved = trySolve(board, r, c, solved);
else
solved++;
sem_post(&mutex);
r++;
c++;
}
if (solved == prevSolved){
sleep_ns(RETRY_WAIT_NS); //try to wait for another thread to update other cells
retries++;
}
else
retries = 0;
prevSolved = solved;
}
return 0;
pthread_exit(0);
}
void *solveSectionHorizontally(void *thread_container) {
thread_container_t *container = thread_container;
int row = container->row;
int col = container->col;
int max_retries = container->max_retries;
//macro for board, c version of 'int board[][] = board'
int (*board)[LIMIT] = container->board_wrapper_ptr->board;
int solved = 0; //number of non-empty cells in row
int toSolve = LIMIT; //number of cells per row
//keep going until all needed cells are solved or num of retries too high
int retries = 0;
int prevSolved = 0;
while (solved < toSolve && retries < max_retries) {
solved = 0;
//progress horizontally
for (col=0; col < LIMIT; col++) {
sem_wait(&mutex);
//solve cell
if (board[row][col] == -1)
solved = trySolve(board, row, col, solved);
else
solved++;
sem_post(&mutex);
}
if (solved == prevSolved){
sleep_ns(RETRY_WAIT_NS); //try to wait for another thread to update other cells
retries++;
}
else
retries = 0;
prevSolved = solved;
}
return 0;
pthread_exit(0);
}
//subroutine to try to solve a cell
int trySolve(int board[][LIMIT], int row, int col, int solved) {
bool used[10] = {0,0,0,0,0,0,0,0,0,0}; //tracks which numbers can't be candidates in [1]..[9]
used[0] = true; //0 is never an option in sudoku
int r, c;
int retval = solved;
//eliminate options from column
for (r=0; r < LIMIT; r++) {
if (board[r][col] != -1)
used[board[r][col]] = true;
}
//eliminate options from row
for (c=0; c < LIMIT; c++) {
if (board[row][c] != -1)
used[board[row][c]] = true;
}
//check if solution was found
int i, last, sum = 0;
for (i=1; i < 10; i++){
sum += !used[i];
if(!used[i])
last = i;
}
if (sum == 1){
board[row][col] = last;
retval++;
}
else {
//eliminate options from 3x3
r = (row / 3) * 3;
c = (col / 3) * 3;
int rMax = r + 2;
int cMax = c + 2;
for (r = (row / 3) * 3; r <= rMax; r++){
for (c = (col / 3) * 3; c <= cMax; c++){
if (board[r][c] != -1)
used[board[r][c]] = true;
}
}
//check if solution was found
sum = 0;
for (i=1; i < 10; i++){
sum += !used[i];
if(!used[i])
last = i;
}
if (sum == 1){
board[row][col] = last;
retval++;
}
}
return retval;
}
bool isCorrect(int board[][LIMIT]) {
int r, c;
int validsum = 0, sum = 0;
for(r=1; r <= LIMIT; r++){
validsum += r;
}
for(r=0; r < LIMIT; r++){
sum = 0;
for(c=0; c < LIMIT; c++){
sum += board[r][c];
}
if(sum != validsum)
return false;
}
return true;
}
int percentSolved(int board[][LIMIT]) {
int r, c;
float percent = 0;
for(r=0; r < 9; r++){
for(c=0; c < LIMIT; c++){
if(board[r][c] != -1)
percent += 1.f/(LIMIT*LIMIT);
}
}
return (int)(percent*100 + 0.5);
}
void printBoard(int board[][LIMIT]) {
unsigned int r, c;
for (r=0; r < LIMIT; r++) {
for (c=0; c < LIMIT; c++) {
if (board[r][c] == -1)
printf("x ");
else
printf("%d ", board[r][c]);
}
printf("\n");
}
}
void printBoardComparison(int problem[][LIMIT], int solution[][LIMIT]) {
unsigned int r, c;
for (r=0; r < LIMIT; r++) {
for (c=0; c < LIMIT; c++) {
if (problem[r][c] == -1)
printf("x ");
else
printf("%d ", problem[r][c]);
}
if(r == LIMIT/2)
printf(" -> ");
else
printf(" ");
for (c=0; c < LIMIT; c++) {
if (solution[r][c] == -1)
printf("x ");
else
printf("%d ", solution[r][c]);
}
printf("\n");
}
}
board_t readBoardFromFile(char *filepath){
printf("reading from file: \'%s\'\n", filepath);
FILE *inputfile = fopen(filepath, "r");
board_t wrapper;
char *line;
size_t len = 0;
int row = 0;
while (row < LIMIT && getline(&line, &len, inputfile) > 1) {
int col = 0;
//get each value from the input file
line[strcspn(line, "\r\n")] = 0; //remove trailing newline char
char *token = strtok(line, " ");
while (col < LIMIT && token != NULL) {
if (strcmp(token, "x") == 0)
wrapper.board[row][col] = -1;
else
wrapper.board[row][col] = (int)strtol(token, NULL, 0);
token = strtok(NULL, " ");
col++;
}
row++;
}
fclose(inputfile);
return wrapper;
}
void writeBoardToFile(int board[][LIMIT], char *filepath) {
printf("writing to file: \'%s\'\n", filepath);
FILE *outputfile = fopen(filepath, "w");
int row, col;
for (row=0; row < LIMIT; row++) {
for (col=0; col < LIMIT; col++) {
fprintf(outputfile, "%d ", board[row][col]);
}
fprintf(outputfile, "\n");
}
fclose(outputfile);
}
void sleep_ns(int ns) //cross-platform sleep function (*nix)
{
#if _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = ns / 1000000;
ts.tv_nsec = ns;
nanosleep(&ts, NULL);
#else
usleep(ns);
#endif
}