/* * 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 "lut.h" #include "sud.h" #include #include #define MAX_FILL_ATTEMPTS 32 #define CELLINIT 0x01ff static void init(struct sudoku *sud) { for (unsigned i = 0; i < NCELLS; ++i) sud->cells[i] = CELLINIT; } bool load(struct sudoku *sud, const char *ptr) { init(sud); for (unsigned i = 0; i < NCELLS; ++i) { if (ptr[i] == '0') continue; if (update(sud, i, ptr[i] - '1') != OK) return false; } return true; } void save(struct sudoku *sud, char *ptr) { for (unsigned i = 0; i < NCELLS; ++i) { if (DET(sud->cells[i])) *ptr++ = '1' + VAL(sud->cells[i]); else *ptr++ = '0'; } } enum update_res update(struct sudoku *sud, unsigned i, unsigned val) { const uint16_t clearmask = ~(1 << val); /* Update possible values of cells in same row, column and * segment. */ for (unsigned j = 0; j < NGROUP; ++j) { sud->cells[rowidx_lut[i][j]] &= clearmask; sud->cells[colidx_lut[i][j]] &= clearmask; sud->cells[segidx_lut[i][j]] &= clearmask; } /* Set the cell's value. */ sud->cells[i] = 0; sud->cells[i] |= DETMASK; sud->cells[i] |= val << VALSHIFT; 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 (DET(sud->cells[IDX(r, c)])) printf("%u ", VAL(sud->cells[IDX(r, c)]) + 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 (!DET(sud->cells[IDX(r, c)])) return INCOMPLETE; ++digitcounts[VAL(sud->cells[IDX(r, c)])]; } if (!checkcounts(digitcounts)) return INCORRECT; } /* Check each column. */ for (c = 0; c < NDIGITS; ++c) { zerocounts(digitcounts); for (r = 0; r < NDIGITS; ++r) { if (!DET(sud->cells[IDX(r, c)])) return INCOMPLETE; ++digitcounts[VAL(sud->cells[IDX(r, c)])]; } 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 (!DET(sud->cells[IDX(r, c)])) return INCOMPLETE; ++digitcounts[VAL(sud->cells[IDX(r, c)])]; } if (!checkcounts(digitcounts)) return INCORRECT; } /* If we've got this far, all is well. */ return SOLVED; } bool filled(const struct sudoku *sud) { for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned c = 0; c < NDIGITS; ++c) { if (!(DET(sud->cells[IDX(r, c)]))) return false; } } return true; }