diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 73 |
1 files changed, 32 insertions, 41 deletions
@@ -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(¬in,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); |