sudoku-solver/genlut.c

86 lines
2.2 KiB
C

/*
* 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
* <https://www.gnu.org/licenses/>.
*/
#include "sud.h"
#include <stdio.h>
#define Q(x) #x
#define PRLUT(x) prlut(Q(x), x)
static void prlut(const char *name, unsigned lut[NCELLS][NDIGITS - 1])
{
unsigned i, j;
printf("const unsigned %s[NCELLS][NDIGITS - 1] = {\n", name);
for (i = 0; i < NCELLS; ++i) {
fputs("\t{ ", stdout);
for (j = 0; j < NDIGITS - 1; ++j) {
printf("%2u", lut[i][j]);
if (j != NDIGITS - 2)
putchar(',');
putchar(' ');
}
fputs("},\n", stdout);
}
fputs("};\n", stdout);
}
int main(void)
{
unsigned rowidx_lut[NCELLS][NDIGITS - 1];
unsigned colidx_lut[NCELLS][NDIGITS - 1];
unsigned segidx_lut[NCELLS][NDIGITS - 1];
unsigned rowi, coli, segi, r, c, i, j, sr, sc;
/* Populate tables. */
for (r = 0; r < NDIGITS; ++r) {
for (c = 0; c < NDIGITS; ++c) {
/* Calculate row and column indices. */
rowi = coli = 0;
for (i = 0; i < NDIGITS; ++i) {
if (i != c)
rowidx_lut[r * NDIGITS + c][rowi++] = r * NDIGITS + i;
if (i != r)
colidx_lut[r * NDIGITS + c][coli++] = i * NDIGITS + c;
}
/* Calculate segment indices. */
segi = 0;
sr = SEGLEN * (r / SEGLEN);
sc = SEGLEN * (c / SEGLEN);
for (i = sr; i < sr + SEGLEN; ++i) {
for (j = sc; j < sc + SEGLEN; ++j) {
if (i == r && j == c)
continue;
segidx_lut[r * NDIGITS + c][segi++] = i * NDIGITS + j;
}
}
}
}
printf("/*\n * This file is auto-generated; do not edit.\n */\n\n");
printf("#include \"lut.h\"\n\n");
PRLUT(rowidx_lut);
putchar('\n');
PRLUT(colidx_lut);
putchar('\n');
PRLUT(segidx_lut);
return 0;
}