From 4a29783f46542d7fb21fcedf1f8953c63cdf4391 Mon Sep 17 00:00:00 2001 From: amy Date: Mon, 21 Nov 2022 10:03:56 -0600 Subject: [PATCH] init --- .gitignore | 1 + justfile | 7 +++ main.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 .gitignore create mode 100644 justfile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99bda00 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cdoku diff --git a/justfile b/justfile new file mode 100644 index 0000000..2db8db4 --- /dev/null +++ b/justfile @@ -0,0 +1,7 @@ +alias b:= build +alias d:= dev +dev: + nodemon --exec "gcc -o cdoku main.c && ./cdoku" ./main.c + +build: + gcc -o cdoku main.c && ./cdoku diff --git a/main.c b/main.c new file mode 100644 index 0000000..b7fa076 --- /dev/null +++ b/main.c @@ -0,0 +1,127 @@ +#include +#include +typedef struct { + int * at; + size_t used; + size_t size; +} array; +void arrayinit(array *a,size_t init){ + a->at = malloc(init*sizeof(int)); + a->used = 0; + a->size = init; +} +void arrayins(array *a, int e){ + if(a->used == a->size){ + a->size *= 2; + a->at = realloc(a->at, a->size * sizeof(int)); + } + a->at[a->used++] = e; +} +void arraykill(array *a){ + free(a->at); + a->at = NULL; + a->used = a->size = 0; +} +typedef struct { + array* at; + size_t used; + size_t size; +} matrix; +void matrixinit(matrix *m,size_t init){ + m->at = malloc(init*sizeof(array)); + m->used = 0; + m->size = init; +} +void matrixins(matrix *m,array e){ + if(m->used <= m->size){ + m->size *= 4; + m->at = realloc(m->at,m->size*sizeof(e)); + } + m->at[m->used++]=e; +} +void matrixkill(matrix*m){ + free(m->at); + m->at = NULL; + m->used = m->size = 0; +} +int isvalid(array*a){ + for(int i = 0;i!=a->used;i++){ + for(int z = 0;z!=a->used;z++){ + if(a->at[i]==a->at[z]&&i!=z&&a->at[i]!=0){ + return 1; + } + } + } + return 0; +} +matrix getsol(array*a){ + array empty,contains,temp; + matrix sol; + matrixinit(&sol,9); + arrayinit(&empty,9); + arrayinit(&temp,9); + arrayinit(&contains,9); + for(int i = 0; i!=9;i++){ + if(a->at[i]!=0){ + arrayins(&contains,a->at[i]); + }else{ + arrayins(&empty,i); + } + + arrayins(&temp,a->at[i]); + } + for(int i = 0;i!=empty.used;i++){ + printf("%i",empty.at[i]); + } + printf("\n%i\n",(int)empty.used); + for(int it = 0; it != (int)empty.used; it++){ + int e = empty.at[it]; + + } + for(int i = 0; i != 9; i++){ + if(temp.at[i]==0){ + for(int z =1; z!= 10;z++){ + temp.at[i]=z; + if(isvalid(&temp)==0){ + break; + } + temp.at[i]=0; + } + } + } + for(int ll = 0; ll!=9;ll++){ + printf("%i",temp.at[ll]); + } + + printf("\n%i",isvalid(&temp)); + return sol; +} +int main(){ + matrix sudoku; + matrixinit(&sudoku,9); + int puzzle[9][9] = { + {1,2,3, 4,0,5, 6,7,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 0,0,0}, + {0,0,0, 0,0,0, 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]); + } + matrixins(&sudoku,trow); + } + getsol(&sudoku.at[0]); + + //arrayins(&sudoku,5); + //printf("%d",sudoku.at[0].at[0]); + return 0; +} +