From 37e7c6481afcd10482df2dd4c36d9c4fc27813db Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 23 Nov 2022 17:56:12 +0000 Subject: [PATCH] Make fill proportion a parameter to gen() --- main.c | 8 +++++--- sud.c | 9 +++------ sud.h | 5 +++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 42d89aa..09146da 100644 --- a/main.c +++ b/main.c @@ -48,7 +48,9 @@ static uint32_t getseed(void) return seed; } -static void genpuzzles(struct sudoku puzzles_out[NPUZZLES], bool print_progress) +static void genpuzzles(struct sudoku puzzles_out[NPUZZLES], + double fill_prop, + bool print_progress) { unsigned i, j, bslen = 0; @@ -63,7 +65,7 @@ static void genpuzzles(struct sudoku puzzles_out[NPUZZLES], bool print_progress) fflush(stdout); } - gen(&puzzles_out[i]); + gen(&puzzles_out[i], fill_prop); } if (print_progress) { @@ -118,7 +120,7 @@ int main(void) srand(seed); struct sudoku puzzles[NPUZZLES]; - genpuzzles(puzzles, true); + genpuzzles(puzzles, 0.33, true); struct bench_res res = runbench(puzzles); puts("\n SUMMARY\n ======="); diff --git a/sud.c b/sud.c index aabe37e..420d8f1 100644 --- a/sud.c +++ b/sud.c @@ -22,9 +22,6 @@ #include #include -#define REM_PROB_NUMER 2 -#define REM_PROB_DENOM 3 - #define MAX_FILL_ATTEMPTS 32 static void initposs(struct sudoku *sud) @@ -76,7 +73,7 @@ static void clear(struct sudoku *sud, unsigned r, unsigned c) } } -void gen(struct sudoku *sud) +void gen(struct sudoku *sud, double fill_prop) { /* Generate a completed sudoku. */ retry: @@ -93,10 +90,10 @@ retry: } } - /* Remove cells with probability of `1 / REMOVE_PROB_RECIP`. */ + /* Remove cells with probability of 1 - fill_prop. */ for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned c = 0; c < NDIGITS; ++c) { - if (rand() % REM_PROB_DENOM < REM_PROB_NUMER) + if ((double)rand() / (double)RAND_MAX < 1 - fill_prop) clear(sud, r, c); } } diff --git a/sud.h b/sud.h index df07d4b..18ceb8a 100644 --- a/sud.h +++ b/sud.h @@ -41,9 +41,10 @@ enum update_res { NOT_ALLOWED, ALREADY_DET, OK }; enum check_res { INCOMPLETE, INCORRECT, SOLVED }; /** - * Populate the sudoku with some random values. + * Populate the sudoku with some random values. The proportion of + * cells that are filled will be approximately `fill_prop`. */ -void gen(struct sudoku *sud); +void gen(struct sudoku *sud, double fill_prop); /** * Attempt to update the cell at row `r`, column `c` to have the value