Add number of passes to benchmark output

This commit is contained in:
Camden Dixie O'Brien 2022-11-23 15:46:21 +00:00
parent 07c1fc7a0f
commit d74b5410b6
3 changed files with 20 additions and 12 deletions

13
main.c
View File

@ -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;
}

12
solve.c
View File

@ -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;
}
}

View File

@ -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