aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Brattlof <hello@bryanbrattlof.com>2020-08-07 20:31:16 -0400
committerBryan Brattlof <hello@bryanbrattlof.com>2020-08-07 20:31:16 -0400
commit98c9c7f41ab7bda5a727eba15638756c1d964630 (patch)
tree790256ca04e8d73636c254cf2decc591191f744b
parentb1981a81cc7cd08b343131dbb9386454a0a7d040 (diff)
downloadsudoku-98c9c7f41ab7bda5a727eba15638756c1d964630.tar.gz
sudoku-98c9c7f41ab7bda5a727eba15638756c1d964630.tar.bz2
c: rename main.c to sudoku.c
to keep with the current sudokeu.<ext> with the sudoku solvers in other languages, I've changed the c lang to sudoku.c
-rw-r--r--c/main.c34
-rw-r--r--c/makefile4
-rw-r--r--c/sudoku.c50
3 files changed, 52 insertions, 36 deletions
diff --git a/c/main.c b/c/main.c
deleted file mode 100644
index cdc9e6b..0000000
--- a/c/main.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include <stdio.h>
-
-
-void display(char puzzle[])
-{
- printf("\n ");
-
- int i = 0;
- while(puzzle[i] != '\0') {
- printf(" %d", puzzle[i] - '0');
-
- if(i % 9 == 8)
- printf("\n ");
- i++;
- }
-
- printf("\n");
-}
-
-
-char* solve(char puzzle[])
-{
- display(puzzle);
-}
-
-
-int main(int argc, char *argv[])
-{
- for(int i=1; i < argc; i++) {
- printf("Puzzle No.%d: %s\n", i, argv[i]);
- solve(argv[i]);
- }
- return 0;
-}
diff --git a/c/makefile b/c/makefile
index f68bc0e..f9e05e6 100644
--- a/c/makefile
+++ b/c/makefile
@@ -1,5 +1,5 @@
CC=gcc
CFLAGS=-I.
-sudoku: main.c
- $(CC) -o sudoku main.c
+sudoku: sudoku.c
+ $(CC) -o sudoku sudoku.c
diff --git a/c/sudoku.c b/c/sudoku.c
new file mode 100644
index 0000000..040ba8d
--- /dev/null
+++ b/c/sudoku.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+
+
+char* solve(char puzzle[])
+{
+
+ /* search for an unanswered square */
+ int i = 0;
+ while(puzzle[i] != '\0') {
+ if(puzzle[i] - '0' != 0) {
+ goto next; // this square is answered
+ }
+
+ // found an unanswered square
+ //printf("%d", puzzle[i] - '0');
+
+ next:
+ i++;
+ }
+
+ return puzzle; // we solved it!
+}
+
+
+void display(char puzzle[])
+{
+ printf("\n ");
+
+ int i = 0;
+ while(puzzle[i] != '\0') {
+ printf(" %d", puzzle[i] - '0');
+
+ if(i % 9 == 8)
+ printf("\n ");
+ i++;
+ }
+
+ printf("\n");
+}
+
+
+int main(int argc, char *argv[])
+{
+ for(int i=1; i < argc; i++) {
+ char* puzzle = solve(argv[i]);
+
+ display(puzzle);
+ }
+ return 0;
+}