From 38ce9d0abbb4b72d437c3fbe1fbbfa3c132ff252 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 26 Nov 2022 01:12:11 +0000 Subject: [PATCH] Add step to solve() to check if cells have only one possible value --- solve.c | 22 +++++++++++++++++++++- sud.h | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/solve.c b/solve.c index 6e6db06..ba9c90a 100644 --- a/solve.c +++ b/solve.c @@ -24,7 +24,7 @@ int solve(struct sudoku *sud) { unsigned r, c, n, i, j, val, np, pr, pc; bool match; - uint16_t valmask; + uint16_t valmask, pvals; for (n = 0;; ++n) { match = false; @@ -103,6 +103,26 @@ int solve(struct sudoku *sud) } } + /* Check each cell for if it has only one possible value. */ + for (r = 0; r < NDIGITS; ++r) { + for (c = 0; c < NDIGITS; ++c) { + if (DET(sud->cells[r][c])) + continue; + + pvals = sud->cells[r][c] & PVALSMASK; + for (val = 0; val < NDIGITS; ++val) { + if (pvals & 1) { + if (pvals == 1) { + update(sud, r, c, val); + match = true; + } + break; + } + pvals >>= 1; + } + } + } + /* Exit if no matches. */ if (!match) return n; diff --git a/sud.h b/sud.h index e9cf138..3ced19d 100644 --- a/sud.h +++ b/sud.h @@ -29,6 +29,7 @@ #define DETMASK 0x8000 #define VALSHIFT 9 #define VALMASK 0x1e00 +#define PVALSMASK 0x01ff #define DET(x) (x & DETMASK) #define VAL(x) ((x & VALMASK) >> VALSHIFT)