/* * Copyright (C) 2022 Camden Dixie O'Brien * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License along with this program. If not, see * . */ #include "sud.h" #include #include #include #define REM_PROB_NUMER 2 #define REM_PROB_DENOM 3 #define MAX_FILL_ATTEMPTS 32 static void initposs(struct sudoku *sud) { for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned c = 0; c < NDIGITS; ++c) { sud->cells[r][c].det = false; for (unsigned i = 0; i < NDIGITS; ++i) sud->cells[r][c].pvals[i] = true; } } } static void clear(struct sudoku *sud, unsigned r, unsigned c) { unsigned tr, tc, val; /* Set undetermined with all possible values */ sud->cells[r][c].det = false; for (unsigned i = 0; i < NDIGITS; ++i) sud->cells[r][c].pvals[i] = true; /* Update cell's possible values based off column. */ for (tr = 0; tr < NDIGITS; ++tr) { if (tr == r || !sud->cells[tr][c].det) continue; val = sud->cells[tr][c].val; sud->cells[r][c].pvals[val] = false; } /* Update possible values of cells in same row. */ for (tc = 0; tc < NDIGITS; ++tc) { if (tc == c || !sud->cells[r][tc].det) continue; val = sud->cells[r][tc].val; sud->cells[r][c].pvals[val] = false; } /* Update possible values of cells in same segment. */ const unsigned segr0 = SEGLEN * (r / SEGLEN); const unsigned segc0 = SEGLEN * (c / SEGLEN); for (tr = segr0; tr < segr0 + SEGLEN; ++tr) { for (tc = segc0; tc < segc0 + SEGLEN; ++tc) { if ((tr == r && tc == c) || !sud->cells[tr][tc].det) continue; val = sud->cells[tr][tc].val; sud->cells[r][c].pvals[val] = false; } } } void gen(struct sudoku *sud) { /* Generate a completed sudoku. */ retry: initposs(sud); for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned c = 0; c < NDIGITS; ++c) { unsigned val, n = 0; do { val = (unsigned)(rand() % NDIGITS); ++n; if (n >= MAX_FILL_ATTEMPTS) goto retry; } while (update(sud, r, c, val) == NOT_ALLOWED); } } /* Remove cells with probability of `1 / REMOVE_PROB_RECIP`. */ for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned c = 0; c < NDIGITS; ++c) { if (rand() % REM_PROB_DENOM < REM_PROB_NUMER) clear(sud, r, c); } } } enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val) { unsigned tr, tc; assert(val < NDIGITS); /* Check that the cell is undetermined and the value is allowed. */ if (sud->cells[r][c].det) return ALREADY_DET; if (!sud->cells[r][c].pvals[val]) return NOT_ALLOWED; /* Update possible values of cells in same column. */ for (tr = 0; tr < NDIGITS; ++tr) { if (tr == r || sud->cells[tr][c].det) continue; sud->cells[tr][c].pvals[val] = false; } /* Update possible values of cells in same row. */ for (tc = 0; tc < NDIGITS; ++tc) { if (tc == c || sud->cells[r][tc].det) continue; sud->cells[r][tc].pvals[val] = false; } /* Update possible values of cells in same segment. */ const unsigned segr0 = SEGLEN * (r / SEGLEN); const unsigned segc0 = SEGLEN * (c / SEGLEN); for (tr = segr0; tr < segr0 + SEGLEN; ++tr) { for (tc = segc0; tc < segc0 + SEGLEN; ++tc) { if ((tr == r && tc == c) || sud->cells[tr][tc].det) continue; sud->cells[tr][tc].pvals[val] = false; } } /* Set the cell's value. */ sud->cells[r][c].det = true; sud->cells[r][c].val = val; return OK; } void print(const struct sudoku *sud) { for (unsigned r = 0; r < NDIGITS; ++r) { /* * Print horizontal divider if on a segment boundary (but not * at the start). */ if (r != 0 && r % SEGLEN == 0) puts("------+-------+------"); for (unsigned c = 0; c < NDIGITS; ++c) { /* * Print vertical divider if on a segment boundary (but * not at the start). */ if (c != 0 && c % SEGLEN == 0) fputs("| ", stdout); if (sud->cells[r][c].det) printf("%u ", sud->cells[r][c].val + 1); else fputs(" ", stdout); } putchar('\n'); } } static void zerocounts(unsigned counts[NDIGITS]) { for (unsigned i = 0; i < NDIGITS; ++i) counts[i] = 0; } static bool checkcounts(unsigned counts[NDIGITS]) { for (unsigned i = 0; i < NDIGITS; ++i) { if (counts[i] != 1) return false; } return true; } enum check_res check(const struct sudoku *sud) { unsigned r, c, i, j, digitcounts[NDIGITS]; /* Check each row. */ for (r = 0; r < NDIGITS; ++r) { zerocounts(digitcounts); for (c = 0; c < NDIGITS; ++c) { if (!sud->cells[r][c].det) return INCOMPLETE; ++digitcounts[sud->cells[r][c].val]; } if (!checkcounts(digitcounts)) return INCORRECT; } /* Check each column. */ for (c = 0; c < NDIGITS; ++c) { zerocounts(digitcounts); for (r = 0; r < NDIGITS; ++r) { if (!sud->cells[r][c].det) return INCOMPLETE; ++digitcounts[sud->cells[r][c].val]; } if (!checkcounts(digitcounts)) return INCORRECT; } /* Check each segment. */ for (i = 0; i < NDIGITS; ++i) { zerocounts(digitcounts); for (j = 0; j < NDIGITS; ++j) { r = 3 * (i / 3) + j / 3; c = 3 * (i % 3) + j % 3; if (!sud->cells[r][c].det) return INCOMPLETE; ++digitcounts[sud->cells[r][c].val]; } if (!checkcounts(digitcounts)) return INCORRECT; } /* If we've got this far, all is well. */ return SOLVED; }