Add number of passes to benchmark output
This commit is contained in:
parent
07c1fc7a0f
commit
d74b5410b6
13
main.c
13
main.c
@ -65,7 +65,7 @@ int main(void)
|
|||||||
putchar('\b');
|
putchar('\b');
|
||||||
puts("100%");
|
puts("100%");
|
||||||
|
|
||||||
bool res[NPUZZLES];
|
int res[NPUZZLES];
|
||||||
struct timespec start, end;
|
struct timespec start, end;
|
||||||
fputs("Solving... ", stdout);
|
fputs("Solving... ", stdout);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
@ -85,14 +85,21 @@ int main(void)
|
|||||||
const double avg_micros = tot_micros / NPUZZLES;
|
const double avg_micros = tot_micros / NPUZZLES;
|
||||||
|
|
||||||
unsigned solved = 0;
|
unsigned solved = 0;
|
||||||
|
unsigned tot_passes = 0;
|
||||||
for (i = 0; i < NPUZZLES; ++i) {
|
for (i = 0; i < NPUZZLES; ++i) {
|
||||||
if (res[i] && check(&puzzles[i]) == SOLVED)
|
if (res[i] < 0)
|
||||||
|
continue;
|
||||||
|
if (check(&puzzles[i]) == SOLVED)
|
||||||
++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 =======");
|
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 time: %.3lf µs\n", avg_micros);
|
||||||
|
printf("Average n.o. passes: %u\n", avg_passes);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
12
solve.c
12
solve.c
@ -80,14 +80,14 @@ static enum apply_res apply_rules(struct sudoku *sud, cellgroup *group)
|
|||||||
return matched ? MATCH : NO_MATCH;
|
return matched ? MATCH : NO_MATCH;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool solve(struct sudoku *sud)
|
int solve(struct sudoku *sud)
|
||||||
{
|
{
|
||||||
cellgroup group = { 0 };
|
cellgroup group = { 0 };
|
||||||
unsigned r, c;
|
unsigned r, c, i, j, n;
|
||||||
bool match;
|
bool match;
|
||||||
enum apply_res res;
|
enum apply_res res;
|
||||||
|
|
||||||
for (unsigned i = 0;; ++i) {
|
for (n = 0;; ++n) {
|
||||||
match = false;
|
match = false;
|
||||||
|
|
||||||
/* Apply rules to each row. */
|
/* Apply rules to each row. */
|
||||||
@ -100,7 +100,7 @@ bool solve(struct sudoku *sud)
|
|||||||
|
|
||||||
res = apply_rules(sud, &group);
|
res = apply_rules(sud, &group);
|
||||||
if (res == ERROR)
|
if (res == ERROR)
|
||||||
return false;
|
return -1;
|
||||||
else if (res == MATCH)
|
else if (res == MATCH)
|
||||||
match = true;
|
match = true;
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ bool solve(struct sudoku *sud)
|
|||||||
|
|
||||||
res = apply_rules(sud, &group);
|
res = apply_rules(sud, &group);
|
||||||
if (res == ERROR)
|
if (res == ERROR)
|
||||||
return false;
|
return -1;
|
||||||
else if (res == MATCH)
|
else if (res == MATCH)
|
||||||
match = true;
|
match = true;
|
||||||
}
|
}
|
||||||
@ -124,6 +124,6 @@ bool solve(struct sudoku *sud)
|
|||||||
|
|
||||||
/* Exit if no matches. */
|
/* Exit if no matches. */
|
||||||
if (!match)
|
if (!match)
|
||||||
return true;
|
return n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
solve.h
7
solve.h
@ -22,9 +22,10 @@
|
|||||||
#include "sud.h"
|
#include "sud.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to solve the given sudoku. Returns when no rules match for
|
* Attempt to solve the given sudoku. Finishes when no rules match for
|
||||||
* an entire pass over the puzzle.
|
* 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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user