diff --git a/main.c b/main.c index f7a2efd..c0d02b6 100644 --- a/main.c +++ b/main.c @@ -65,7 +65,7 @@ int main(void) putchar('\b'); puts("100%"); - bool res[NPUZZLES]; + int res[NPUZZLES]; struct timespec start, end; fputs("Solving... ", stdout); fflush(stdout); @@ -85,14 +85,21 @@ int main(void) const double avg_micros = tot_micros / NPUZZLES; unsigned solved = 0; + unsigned tot_passes = 0; for (i = 0; i < NPUZZLES; ++i) { - if (res[i] && check(&puzzles[i]) == SOLVED) + if (res[i] < 0) + continue; + if (check(&puzzles[i]) == SOLVED) ++solved; + tot_passes += (unsigned long)res[i]; } + const double succ_rate = (double)solved / NPUZZLES; + const unsigned avg_passes = tot_passes / NPUZZLES; puts("\n SUMMARY\n ======="); - printf("Success rate: %.0lf%%\n", 1e2 * (double)solved / NPUZZLES); + printf("Success rate: %.0lf%%\n", 1e2 * succ_rate); printf("Average time: %.3lf µs\n", avg_micros); + printf("Average n.o. passes: %u\n", avg_passes); return 0; } diff --git a/solve.c b/solve.c index ed9fb61..f762dd1 100644 --- a/solve.c +++ b/solve.c @@ -80,14 +80,14 @@ static enum apply_res apply_rules(struct sudoku *sud, cellgroup *group) return matched ? MATCH : NO_MATCH; } -bool solve(struct sudoku *sud) +int solve(struct sudoku *sud) { cellgroup group = { 0 }; - unsigned r, c; + unsigned r, c, i, j, n; bool match; enum apply_res res; - for (unsigned i = 0;; ++i) { + for (n = 0;; ++n) { match = false; /* Apply rules to each row. */ @@ -100,7 +100,7 @@ bool solve(struct sudoku *sud) res = apply_rules(sud, &group); if (res == ERROR) - return false; + return -1; else if (res == MATCH) match = true; } @@ -115,7 +115,7 @@ bool solve(struct sudoku *sud) res = apply_rules(sud, &group); if (res == ERROR) - return false; + return -1; else if (res == MATCH) match = true; } @@ -124,6 +124,6 @@ bool solve(struct sudoku *sud) /* Exit if no matches. */ if (!match) - return true; + return n; } } diff --git a/solve.h b/solve.h index 1f65e7b..df47153 100644 --- a/solve.h +++ b/solve.h @@ -22,9 +22,10 @@ #include "sud.h" /** - * Attempt to solve the given sudoku. Returns when no rules match for - * an entire pass over the puzzle. + * Attempt to solve the given sudoku. Finishes when no rules match for + * an entire pass over the puzzle, returning the number of passes, or + * -1 on error. */ -bool solve(struct sudoku *sud); +int solve(struct sudoku *sud); #endif