-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
188 lines (167 loc) · 5.06 KB
/
sudoku.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
#include<stdbool.h>
//bit mask for checking if sudokuresults is valid
enum { VALID_BITS = 0b1111111110 };
#define NUM_THREADS 27
typedef struct {
int row;
int col;
int result;
int** sudokuboard;
}Gamebox;
Gamebox bldsudokuboard( Gamebox gamebox ){
gamebox.sudokuboard = malloc( sizeof(int*) * 9 );
int* tmp = malloc( sizeof(int) * 81 );
for( int i = 0 ; i < 9 ; i++){
gamebox.sudokuboard[i] = tmp + i * 9;
}
FILE* file;
file = fopen( "test.txt", "r");
for( int i = 0 ; i < 9 ; i++ ){
for( int j = 0 ;j < 9 ; j++){
fscanf( file, "%d", &gamebox.sudokuboard[i][j]);
}
}
fclose( file);
return gamebox;
}
void printsudokuboard( Gamebox gamebox ){
for( int i = 0 ; i < 9 ; i++ ){
for( int j = 0 ; j < 9 ; j++ ){
printf( "%d", gamebox.sudokuboard[i][j]);
}
puts("");
}
}
//rowid should be 0 1 ... 8
int rowcheck( Gamebox* gamebox ){
int rowid = gamebox->row;
unsigned sudokuresults = 0;
for( int i = 0 ; i < 9 ; i++ ){
unsigned tmp = 1 << gamebox->sudokuboard[rowid][i];
sudokuresults |= tmp;
}
if( sudokuresults == VALID_BITS ){
return 1;
}
else return 0;
}
int colcheck( Gamebox* gamebox ){
int colid = gamebox->col;
unsigned sudokuresults = 0;
for( int i = 0 ; i < 9 ; i++ ){
unsigned tmp = 1 << gamebox->sudokuboard[i][colid];
sudokuresults |= tmp;
}
if( sudokuresults == VALID_BITS ){
return 1;
}
else return 0;
}
//the sudoku grid is fractal two levels of nesting(2d Euclidean Space)
//this tells us we can use an offset to be able to
//recursively solve the same problem
int sectioncheck( Gamebox* gamebox ){
int coloffset = gamebox->col * 3;
int rowoffset = gamebox->row * 3;
unsigned sudokuresults = 0;
for( int i = 0 ; i < 3 ; i++ ){
for(int j = 0 ; j < 3 ; j++){
unsigned tmp = 1 << gamebox->sudokuboard[ i + rowoffset ][ j + coloffset ];
sudokuresults |= tmp;
}
}
if( sudokuresults == VALID_BITS ){
return 1;
}
else return 0;
}
void* rowstartroutine( void* gamebox ){
Gamebox* sudoku = (Gamebox*) gamebox;
sudoku->result = rowcheck( sudoku);
return NULL;
}
void* colstartroutine( void* gamebox ){
Gamebox* sudoku = (Gamebox*) gamebox;
sudoku->result = colcheck( sudoku);
return NULL;
}
void* sectionstartroutine( void* gamebox ){
Gamebox* sudoku = (Gamebox*) gamebox;
sudoku->result = sectioncheck( sudoku);
return NULL;
}
int main( int argc, char *argv[]){
Gamebox gamebox;
gamebox = bldsudokuboard( gamebox);
printsudokuboard( gamebox);
pthread_t tid[NUM_THREADS];
int results[NUM_THREADS];
int t_count = 0;
bool validsudoku = true;
//TODO change to NUM_THREADS* sizeof
Gamebox** t_boxes = malloc( NUM_THREADS * sizeof(Gamebox*));
for( int i = 0 ; i < 9 ; i++ ){
Gamebox* threadboard = (Gamebox*) malloc( sizeof( Gamebox));
threadboard->row = i;
threadboard->sudokuboard = gamebox.sudokuboard;
t_boxes[t_count] = threadboard;
pthread_create( &tid[t_count], NULL, rowstartroutine, (void*) t_boxes[t_count]);
t_count++;
}
for( int i = 0 ; i < 9 ; i++ ){
Gamebox* threadboard = (Gamebox*) malloc( sizeof( Gamebox));
threadboard->col = i;
threadboard->sudokuboard = gamebox.sudokuboard;
t_boxes[t_count] = threadboard;
pthread_create( &tid[t_count], NULL, colstartroutine, (void*) t_boxes[t_count]);
t_count++;
}
for( int i = 0 ; i < 3 ; i++ ){
for( int j = 0 ; j < 3 ; j++){
Gamebox* threadboard = (Gamebox*) malloc( sizeof( Gamebox));
threadboard->row = i;
threadboard->col = j;
threadboard->sudokuboard = gamebox.sudokuboard;
t_boxes[t_count] = threadboard;
pthread_create( &tid[t_count], NULL, sectionstartroutine, (void*) t_boxes[t_count]);
t_count++;
}
}
//TODO add wtf you are if invalid
for( int i = 0 ; i < 9 ; i++ ){
pthread_join( tid[i], NULL );
if( t_boxes[i]->result != 1){
printf("Not valid sudoku row %d failed!\n", t_boxes[i]->row );
validsudoku = false;
}
}
for( int i = 9 ; i < 18 ; i++ ){
pthread_join( tid[i], NULL );
if( t_boxes[i]->result != 1){
printf("Not valid sudoku col %d failed!\n", t_boxes[i]->col );
validsudoku = false;
}
}
int grid = 0;
for( int i = 18 ; i < 27 ; i++ ){
pthread_join( tid[i], NULL );
if( t_boxes[i]->result != 1){
printf("Not valid sudoku gridrow %d gridcol %d failed!\n", t_boxes[i]->row, t_boxes[i]->col );
validsudoku = false;
}
}
if( validsudoku){
puts("valid sudoku!");
}
return 0;
}
/*
if( rowcheck( gamebox , 1)){
puts("valid row");
}
else puts("not valid row");
*/