diff --git a/sud.c b/sud.c index 420d8f1..3d2bb12 100644 --- a/sud.c +++ b/sud.c @@ -35,6 +35,18 @@ static void initposs(struct sudoku *sud) } } +bool load(struct sudoku *sud, const char *ptr) +{ + initposs(sud); + for (unsigned i = 0; i < NCELLS; ++i) { + if (ptr[i] == '0') + continue; + if (update(sud, i / NDIGITS, i % NDIGITS, ptr[i] - '1') != OK) + return false; + } + return true; +} + static void clear(struct sudoku *sud, unsigned r, unsigned c) { unsigned tr, tc, val; diff --git a/sud.h b/sud.h index 18ceb8a..bdb8971 100644 --- a/sud.h +++ b/sud.h @@ -23,6 +23,7 @@ #define SEGLEN 3 #define NDIGITS (SEGLEN * SEGLEN) +#define NCELLS (NDIGITS * NDIGITS) struct cellstate { bool det; @@ -40,6 +41,12 @@ enum update_res { NOT_ALLOWED, ALREADY_DET, OK }; enum check_res { INCOMPLETE, INCORRECT, SOLVED }; +/** + * Read `NCELLS` values from the given pointer and load them into the + * sudoku. + */ +bool load(struct sudoku *sud, const char *ptr); + /** * Populate the sudoku with some random values. The proportion of * cells that are filled will be approximately `fill_prop`.