summaryrefslogtreecommitdiff
path: root/solver.c
diff options
context:
space:
mode:
Diffstat (limited to 'solver.c')
-rw-r--r--solver.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/solver.c b/solver.c
new file mode 100644
index 0000000..b6de45e
--- /dev/null
+++ b/solver.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "debug.h"
+#include "update.h"
+#include "cell.h"
+#include "solve.h"
+#include "display.h"
+#include "load.h"
+
+int solve(struct cell (*board)[9][9])
+{
+ int i = 0;
+ int total_changes = 0;
+ int change_count = -1;
+ /* 9*9 is worst case */
+ for (i = 0; i < 9*9 && change_count; i++) {
+ change_count = 0;
+ update_not_row_col(board);
+ change_count += not_to_val_cell(board);
+ change_count += solve_row_col(board);
+ change_count += cells_fill_certainties(board);
+ total_changes += change_count;
+// display(board);
+ }
+ return total_changes;
+}
+
+int main(int argc, char **argv)
+{
+ int total_changes = 0;
+ struct cell board[9][9];
+ FILE *f = NULL;
+
+ memset(board, 0, sizeof(board));
+
+ if (argc != 2) {
+ printf("Syntax: %s puzzle_file\n", argv[0]);
+ return 1;
+ }
+
+ f = fopen(argv[1], "r");
+ if (!f) {
+ perror("fopen");
+ return 1;
+ }
+
+ if (load(f, &board) < 0) {
+ fclose(f);
+ return 1;
+ }
+
+ fclose(f);
+
+ display(board);
+ total_changes = solve(&board);
+
+ if (board_is_solved(&board) < 0) {
+ printf("Could not solve the board! Here is the closest (%d changes)\n", total_changes);
+ } else {
+ printf("Solution (%d changes)\n", total_changes);
+ }
+ display(board);
+
+}