Remove all the indirection

This commit is contained in:
Camden Dixie O'Brien 2022-11-23 16:04:23 +00:00
parent 5aea129dc9
commit 27d7f3f01b

85
solve.c
View File

@ -20,90 +20,35 @@
#include <stdio.h> #include <stdio.h>
struct match {
unsigned row;
unsigned col;
unsigned val;
};
struct cellref {
unsigned row, col;
struct cellstate *state;
};
enum apply_res { ERROR, NO_MATCH, MATCH };
typedef struct cellref cellgroup[NDIGITS];
typedef bool (*rulefn)(cellgroup *group, struct match *match_out);
static bool one_place_for_digit(cellgroup *group, struct match *match_out)
{
for (unsigned val = 0; val < NDIGITS; ++val) {
unsigned n = 0, row = NDIGITS, col = NDIGITS;
for (unsigned i = 0; i < NDIGITS; ++i) {
if (!(*group)[i].state->det && (*group)[i].state->pvals[val]) {
row = (*group)[i].row;
col = (*group)[i].col;
++n;
if (n > 1)
break;
}
}
if (n == 1) {
match_out->row = row;
match_out->col = col;
match_out->val = val;
return true;
}
}
return false;
}
static const rulefn rules[] = {
&one_place_for_digit,
};
static const unsigned nrules = sizeof(rules) / sizeof(rules[0]);
static enum apply_res apply_rules(struct sudoku *sud, cellgroup *group)
{
struct match m;
bool matched = false;
for (unsigned i = 0; i < nrules; ++i) {
if (!rules[i](group, &m))
continue;
if (!update(sud, m.row, m.col, m.val))
return ERROR;
matched = true;
}
return matched ? MATCH : NO_MATCH;
}
int solve(struct sudoku *sud) int solve(struct sudoku *sud)
{ {
cellgroup group = { 0 }; unsigned r, c, n, val, np, pr, pc;
unsigned r, c, n;
bool match; bool match;
enum apply_res res;
for (n = 0;; ++n) { for (n = 0;; ++n) {
match = false; match = false;
/* Apply rules to each row. */ /* Check if only one place a value can go on each row. */
for (r = 0; r < NDIGITS; ++r) { for (r = 0; r < NDIGITS; ++r) {
for (val = 0; val < NDIGITS; ++val) {
np = 0;
for (c = 0; c < NDIGITS; ++c) { for (c = 0; c < NDIGITS; ++c) {
group[c].row = r; if (!sud->cells[r][c].det && sud->cells[r][c].pvals[val]) {
group[c].col = c; pr = r;
group[c].state = &sud->cells[r][c]; pc = c;
++np;
if (np > 1)
break;
}
} }
res = apply_rules(sud, &group); if (np == 1) {
if (res == ERROR) if (update(sud, pr, pc, val) != OK)
return -1; return -1;
else if (res == MATCH)
match = true; match = true;
} }
}
}
/* Exit if no matches. */ /* Exit if no matches. */
if (!match) if (!match)