aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoramelia <[email protected]>2022-11-28 14:14:21 -0600
committeramelia <[email protected]>2022-11-28 14:14:21 -0600
commita6e4f0abe2f8eb48a0a9f790406d7d5b2c8b53f5 (patch)
tree2a385f3b4ea650f35976576563ce872c906e765e
parent80c44143c6bdfe6bfa7c17280f29a590bd2428ff (diff)
almost done
-rw-r--r--example.sudoku17
-rw-r--r--main.c73
-rw-r--r--readme.md22
3 files changed, 71 insertions, 41 deletions
diff --git a/example.sudoku b/example.sudoku
new file mode 100644
index 0000000..a3ac871
--- /dev/null
+++ b/example.sudoku
@@ -0,0 +1,17 @@
+0 0 8 3 0 0 0 0 0
+1 0 3 0 9 0 2 0 0
+0 2 9 0 0 0 0 3 0
+8 0 0 0 0 4 5 0 0
+0 9 0 1 0 8 0 0 0
+0 0 2 9 0 0 0 0 8
+0 1 4 0 0 0 3 2 0
+0 0 0 0 1 0 8 0 0
+0 0 0 0 0 3 0 1 0
+
+; you can comment like this!
+; anything after the initial 9x9 will be ignored
+; spacing/linebreaks/non-integers are valid to put between numbers
+;
+; more indepth, for any line, the first 9 valid integers will be taken
+; ie, a line of 000,9e3p0 0(00) will give the program an
+; array equal to - {0,0,0, 9,3,0, 0,0,0}
diff --git a/main.c b/main.c
index 13f2318..03e9690 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <string.h>
+#include <ctype.h>
typedef struct {
int * at;
size_t used;
@@ -271,6 +273,7 @@ void arrayclear(array*a){
a->at=malloc(sizeof(int));
}
matrix bruteforce(matrix q){
+
matrix temp,notin;
array ttt,tty;
arrayinit(&ttt,1);
@@ -287,6 +290,7 @@ matrix bruteforce(matrix q){
matrixins(&notin,tty);
matrixins(&temp,ttt);
}
+ matrixlist(temp);
int isnot = 0;
int backtrace = 0;
for(int x = 0; x != temp.used; x++){
@@ -302,9 +306,6 @@ matrix bruteforce(matrix q){
}
} else {
//go through everything after previous value
- //matrixlist(temp);
- //printf("%i",temp.at[x].at[y]);
- //return q;
if(temp.at[x].at[y]!=9){
for(int z = temp.at[x].at[y]+1; z!=10;z++){
temp.at[x].at[y]=z;
@@ -326,13 +327,11 @@ matrix bruteforce(matrix q){
backtrace = 0;
}
}
- //if(x==2&&y==2)
//printf(" %i %i %i\n",x,y,backtrace);
if(backtrace==0){
y++;
} else {
if(y==0){
- //printf("backtraced alot!");
if(x==0){
printf("\n:( cant continue. must fail");
return temp;
@@ -345,53 +344,45 @@ matrix bruteforce(matrix q){
}
}
}
- matrixlist(temp);
- return q;
+ //matrixlist(temp);
+ return temp;
}
-int main(){
+int main( int argc, char *argv[] ) {
matrix sudoku;
matrixinit(&sudoku,9);
- /*int puzzle[9][9] = {
- {6,8,0, 4,0,3, 0,5,0},
- {4,0,2, 0,5,0, 3,6,8},
- {5,9,3, 6,7,8, 0,0,4},
- {0,1,7, 2,8,6, 9,4,5},
- {8,0,9, 5,0,4, 2,0,7},
- {2,5,4, 3,9,7, 8,1,0},
- {7,0,0, 8,3,1, 5,9,2},
- {9,3,5, 0,6,0, 4,0,1},
- {0,2,0, 9,0,5, 0,7,3},
- };*/
- int puzzle[9][9] = {
- {0,0,0, 4,9,6, 0,0,0},
- {7,0,3, 0,0,0, 0,9,0},
- {0,0,0, 0,0,0, 5,0,0},
- {0,8,4, 7,0,0, 0,0,0},
- {0,0,0, 0,0,0, 0,5,2},
- {3,0,0, 1,0,0, 7,0,0},
- {0,4,0, 0,5,0, 0,2,7},
- {6,0,0, 8,0,9, 0,0,3},
- {0,0,0, 0,0,4, 0,0,0},
- };
- for(int r = 0;r!=9;r++){
- array trow;
- arrayinit(&trow,1);
- for(int c = 0;c!=9;c++){
- arrayins(&trow,puzzle[r][c]);
+ FILE *fp = fopen(argv[1], "r");
+ if(fp == NULL||argv[1] == NULL) {
+ perror("Unable to open provided file\nusage:\ncdoku {path of file}\ncdoku example.sudoku\n\nsee examples at https://git.disroot.org/grantsquires/cdoku\n\n raw error");
+ return 1;
}
- matrixins(&sudoku,trow);
- }
+
+ char chunk[128]={};
+ array trow;
+ arrayinit(&trow,1);
+ while(fgets(chunk, sizeof(chunk), fp) != NULL) {
+ if(chunk[0]!=';'){
+ for(int i = 0; i!= strlen(chunk);i++){
+ if(chunk[i]=='\n'||trow.used==9){
+ break;
+ }
+ if(isdigit(chunk[i])){
+ arrayins(&trow,chunk[i]-'0');
+ }
+ }
+ matrixins(&sudoku,trow);
+ arrayclear(&trow);
+ if(sudoku.used==9)break;
+ }
+ }
+
/*
*everything before this is only
*to initialize the 'matrix' object as a sudoku puzzle
*that the program can read
*/
clock_t t;
- matrixlist(sudoku);
t = clock();
- //printf("\n%i\n",isMvalid(sudoku));
- //arraylist(getsquare(sudoku,0));
- bruteforce(sudoku);
+ matrixlist(bruteforce(sudoku));
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("took %f seconds",time_taken);
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..1bc638e
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,22 @@
+## cdoku
+
+this is a relativley simple and efficent sudoku puzzle brute force algorithm!
+
+check out ./example.sudoku and ./main.c for more documentation:)
+
+### building and running
+
+`just b` or `gcc -o cdoku main.c` to build the binary
+
+check out `just --list` for more options
+
+`cdoku {file path}`
+
+### examples and some bench marks
+
+1.60 GHz - ~0.169s
+
+### todo
+
+- read json formated
+- read sudoku as argument