Use LUT for group indexes instead of calculating on-the-fly

This commit is contained in:
Camden Dixie O'Brien 2022-11-26 14:27:12 +00:00
parent 09a9f95740
commit 40c12c3458
7 changed files with 225 additions and 128 deletions

4
.gitignore vendored
View File

@ -2,6 +2,7 @@
*.o *.o
sudoku sudoku
eval eval
genlut
# Test data # Test data
sudoku.csv sudoku.csv
@ -15,3 +16,6 @@ compile_commands.json
# Profiling results # Profiling results
gmon.out gmon.out
# Generated code
lut.c

View File

@ -24,10 +24,7 @@ CFLAGS += -static
LDFLAGS += -lpthread LDFLAGS += -lpthread
# For profiling SRC = main.c sud.c solve.c lut.c
# CFLAGS += -pg
SRC = main.c sud.c solve.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
default: sudoku eval default: sudoku eval
@ -35,16 +32,16 @@ default: sudoku eval
sudoku: $(OBJ) sudoku: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@
eval: eval.o
$(CC) $(CFLAGS) $(LDFLAGS) eval.o -o $@
clean: clean:
rm -f sudoku *.o rm -f sudoku eval genlut *.o lut.c
lut.c: genlut
./genlut > $@
eval.o: ds.h eval.o: ds.h
main.o: sud.h solve.h ds.h main.o: sud.h solve.h ds.h
sud.o: sud.h sud.o: sud.h lut.h
solve.o: solve.h solve.o: solve.h lut.h
puzzles: sudoku.csv puzzles: sudoku.csv
pv sudoku.csv | tail -n+2 | cut -d, -f1 | tr -d '\n' > $@ pv sudoku.csv | tail -n+2 | cut -d, -f1 | tr -d '\n' > $@

84
genlut.c Normal file
View File

@ -0,0 +1,84 @@
/*
* 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])
{
printf("const unsigned %s[NCELLS][NDIGITS - 1] = {\n", name);
for (unsigned i = 0; i < NCELLS; ++i) {
printf("\t[%2u] = { ", i);
for (unsigned 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];
/* Populate tables. */
unsigned rowi, coli, segi;
for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) {
/* Calculate row and column indices. */
rowi = coli = 0;
for (unsigned 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;
const unsigned sr = SEGLEN * (r / SEGLEN);
const unsigned sc = SEGLEN * (c / SEGLEN);
for (unsigned i = sr; i < sr + SEGLEN; ++i) {
for (unsigned 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;
}

30
lut.h Normal file
View File

@ -0,0 +1,30 @@
/*
* 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/>.
*/
#ifndef LUT_H
#define LUT_H
#include "sud.h"
#define NGROUP (NDIGITS - 1)
extern const unsigned rowidx_lut[NCELLS][NGROUP];
extern const unsigned colidx_lut[NCELLS][NGROUP];
extern const unsigned segidx_lut[NCELLS][NGROUP];
#endif

139
solve.c
View File

@ -17,14 +17,15 @@
*/ */
#include "solve.h" #include "solve.h"
#include "lut.h"
#include <stdio.h> #include <stdio.h>
static void setpval(struct sudoku *sud, unsigned r, unsigned c, uint16_t pval) static void setpval(struct sudoku *sud, unsigned i, uint16_t pval)
{ {
for (unsigned val = 0; val < NDIGITS; ++val) { for (unsigned val = 0; val < NDIGITS; ++val) {
if (pval & 1) { if (pval & 1) {
update(sud, r, c, val); update(sud, i, val);
return; return;
} }
pval >>= 1; pval >>= 1;
@ -33,86 +34,80 @@ static void setpval(struct sudoku *sud, unsigned r, unsigned c, uint16_t pval)
int solve(struct sudoku *sud) int solve(struct sudoku *sud)
{ {
unsigned r, c, n, i, j, val, sr, sc; unsigned n, i, j, val;
bool match; bool match;
uint16_t valmask, pvals; uint16_t valmask, pvals;
for (n = 0;; ++n) { for (n = 0;; ++n) {
match = false; match = false;
for (r = 0; r < NDIGITS; ++r) { for (i = 0; i < NCELLS; ++i) {
for (c = 0; c < NDIGITS; ++c) { if (DET(sud->cells[i]))
if (DET(sud->cells[r][c])) continue;
continue;
/* /*
* Check if there's only one possible value this cell * Check if there's only one possible value this cell
* can have. * can have.
*/ */
pvals = sud->cells[r][c] & PVALSMASK; pvals = sud->cells[i] & PVALSMASK;
for (val = 0; val < NDIGITS; ++val) { for (val = 0; val < NDIGITS; ++val) {
if (pvals & 1) { if (pvals & 1) {
if (pvals == 1) { if (pvals == 1) {
update(sud, r, c, val); update(sud, i, val);
match = true; match = true;
goto next_cell; goto next_cell;
}
break;
} }
pvals >>= 1; break;
} }
pvals >>= 1;
/*
* Check if there's a possible value unique to this
* cell in its row.
*/
valmask = 0;
for (i = 0; i < NDIGITS; ++i) {
if (i != r && !DET(sud->cells[i][c]))
valmask |= sud->cells[i][c];
}
if ((pvals = sud->cells[r][c] & ~valmask)) {
setpval(sud, r, c, pvals);
match = true;
continue;
}
/*
* Check if there's a possible value unique to this
* cell in its column.
*/
valmask = 0;
for (i = 0; i < NDIGITS; ++i) {
if (i != c && !DET(sud->cells[r][i]))
valmask |= sud->cells[r][i];
}
if ((pvals = sud->cells[r][c] & ~valmask)) {
setpval(sud, r, c, pvals);
match = true;
continue;
}
/*
* Check if there's a possible value unique to this
* cell in its segment.
*/
sr = SEGLEN * (r / SEGLEN);
sc = SEGLEN * (c / SEGLEN);
valmask = 0;
for (i = sr; i < sr + SEGLEN; ++i) {
for (j = sc; j < sc + SEGLEN; ++j) {
if ((i != r || j != c) && !DET(sud->cells[i][j]))
valmask |= sud->cells[i][j];
}
}
if ((pvals = sud->cells[r][c] & ~valmask)) {
setpval(sud, r, c, pvals);
match = true;
continue;
}
next_cell:;
} }
/*
* Check if there's a possible value unique to this
* cell in its row.
*/
valmask = 0;
for (j = 0; j < NGROUP; ++j) {
if (!DET(sud->cells[rowidx_lut[i][j]]))
valmask |= sud->cells[rowidx_lut[i][j]];
}
if ((pvals = sud->cells[i] & ~valmask)) {
setpval(sud, i, pvals);
match = true;
continue;
}
/*
* Check if there's a possible value unique to this
* cell in its column.
*/
valmask = 0;
for (j = 0; j < NGROUP; ++j) {
if (!DET(sud->cells[colidx_lut[i][j]]))
valmask |= sud->cells[colidx_lut[i][j]];
}
if ((pvals = sud->cells[i] & ~valmask)) {
setpval(sud, i, pvals);
match = true;
continue;
}
/*
* Check if there's a possible value unique to this
* cell in its segment.
*/
valmask = 0;
for (j = 0; j < NGROUP; ++j) {
if (!DET(sud->cells[segidx_lut[i][j]]))
valmask |= sud->cells[segidx_lut[i][j]];
}
if ((pvals = sud->cells[i] & ~valmask)) {
setpval(sud, i, pvals);
match = true;
continue;
}
next_cell:;
} }
/* Exit if no matches. */ /* Exit if no matches. */

66
sud.c
View File

@ -16,6 +16,7 @@
* <https://www.gnu.org/licenses/>. * <https://www.gnu.org/licenses/>.
*/ */
#include "lut.h"
#include "sud.h" #include "sud.h"
#include <stdio.h> #include <stdio.h>
@ -27,10 +28,8 @@
static void init(struct sudoku *sud) static void init(struct sudoku *sud)
{ {
for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned i = 0; i < NCELLS; ++i)
for (unsigned c = 0; c < NDIGITS; ++c) sud->cells[i] = CELLINIT;
sud->cells[r][c] = CELLINIT;
}
} }
bool load(struct sudoku *sud, const char *ptr) bool load(struct sudoku *sud, const char *ptr)
@ -41,7 +40,7 @@ bool load(struct sudoku *sud, const char *ptr)
for (unsigned i = 0; i < NCELLS; ++i) { for (unsigned i = 0; i < NCELLS; ++i) {
if (ptr[i] == '0') if (ptr[i] == '0')
continue; continue;
if (update(sud, i / NDIGITS, i % NDIGITS, ptr[i] - '1') != OK) if (update(sud, i, ptr[i] - '1') != OK)
return false; return false;
} }
@ -50,40 +49,29 @@ bool load(struct sudoku *sud, const char *ptr)
void save(struct sudoku *sud, char *ptr) void save(struct sudoku *sud, char *ptr)
{ {
for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned i = 0; i < NCELLS; ++i) {
for (unsigned c = 0; c < NDIGITS; ++c) { if (DET(sud->cells[i]))
if (DET(sud->cells[r][c])) *ptr++ = '1' + VAL(sud->cells[i]);
*ptr++ = '1' + VAL(sud->cells[r][c]); else
else *ptr++ = '0';
*ptr++ = '0';
}
} }
} }
enum update_res update(struct sudoku *sud, unsigned r, unsigned c, unsigned val) enum update_res update(struct sudoku *sud, unsigned i, unsigned val)
{ {
unsigned tr, tc;
const uint16_t clearmask = ~(1 << val); const uint16_t clearmask = ~(1 << val);
/* Update possible values of cells in same row. */ /* Update possible values of cells in same row, column and
for (tc = 0; tc < NDIGITS; ++tc) * segment. */
sud->cells[r][tc] &= clearmask; for (unsigned j = 0; j < NGROUP; ++j) {
sud->cells[rowidx_lut[i][j]] &= clearmask;
/* Update possible values of cells in same column. */ sud->cells[colidx_lut[i][j]] &= clearmask;
for (tr = 0; tr < NDIGITS; ++tr) sud->cells[segidx_lut[i][j]] &= clearmask;
sud->cells[tr][c] &= clearmask;
/* 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)
sud->cells[tr][tc] &= clearmask;
} }
/* Set the cell's value. */ /* Set the cell's value. */
sud->cells[r][c] |= DETMASK; sud->cells[i] |= DETMASK;
sud->cells[r][c] |= val << VALSHIFT; sud->cells[i] |= val << VALSHIFT;
return OK; return OK;
} }
@ -106,8 +94,8 @@ void print(const struct sudoku *sud)
if (c != 0 && c % SEGLEN == 0) if (c != 0 && c % SEGLEN == 0)
fputs("| ", stdout); fputs("| ", stdout);
if (DET(sud->cells[r][c])) if (DET(sud->cells[IDX(r, c)]))
printf("%u ", VAL(sud->cells[r][c]) + 1); printf("%u ", VAL(sud->cells[IDX(r, c)]) + 1);
else else
fputs(" ", stdout); fputs(" ", stdout);
} }
@ -138,9 +126,9 @@ enum check_res check(const struct sudoku *sud)
for (r = 0; r < NDIGITS; ++r) { for (r = 0; r < NDIGITS; ++r) {
zerocounts(digitcounts); zerocounts(digitcounts);
for (c = 0; c < NDIGITS; ++c) { for (c = 0; c < NDIGITS; ++c) {
if (!DET(sud->cells[r][c])) if (!DET(sud->cells[IDX(r, c)]))
return INCOMPLETE; return INCOMPLETE;
++digitcounts[VAL(sud->cells[r][c])]; ++digitcounts[VAL(sud->cells[IDX(r, c)])];
} }
if (!checkcounts(digitcounts)) if (!checkcounts(digitcounts))
return INCORRECT; return INCORRECT;
@ -150,9 +138,9 @@ enum check_res check(const struct sudoku *sud)
for (c = 0; c < NDIGITS; ++c) { for (c = 0; c < NDIGITS; ++c) {
zerocounts(digitcounts); zerocounts(digitcounts);
for (r = 0; r < NDIGITS; ++r) { for (r = 0; r < NDIGITS; ++r) {
if (!DET(sud->cells[r][c])) if (!DET(sud->cells[IDX(r, c)]))
return INCOMPLETE; return INCOMPLETE;
++digitcounts[VAL(sud->cells[r][c])]; ++digitcounts[VAL(sud->cells[IDX(r, c)])];
} }
if (!checkcounts(digitcounts)) if (!checkcounts(digitcounts))
return INCORRECT; return INCORRECT;
@ -164,9 +152,9 @@ enum check_res check(const struct sudoku *sud)
for (j = 0; j < NDIGITS; ++j) { for (j = 0; j < NDIGITS; ++j) {
r = 3 * (i / 3) + j / 3; r = 3 * (i / 3) + j / 3;
c = 3 * (i % 3) + j % 3; c = 3 * (i % 3) + j % 3;
if (!DET(sud->cells[r][c])) if (!DET(sud->cells[IDX(r, c)]))
return INCOMPLETE; return INCOMPLETE;
++digitcounts[VAL(sud->cells[r][c])]; ++digitcounts[VAL(sud->cells[IDX(r, c)])];
} }
if (!checkcounts(digitcounts)) if (!checkcounts(digitcounts))
return INCORRECT; return INCORRECT;
@ -180,7 +168,7 @@ bool filled(const struct sudoku *sud)
{ {
for (unsigned r = 0; r < NDIGITS; ++r) { for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) { for (unsigned c = 0; c < NDIGITS; ++c) {
if (!(DET(sud->cells[r][c]))) if (!(DET(sud->cells[IDX(r, c)])))
return false; return false;
} }
} }

13
sud.h
View File

@ -34,8 +34,10 @@
#define DET(x) (x & DETMASK) #define DET(x) (x & DETMASK)
#define VAL(x) ((x & VALMASK) >> VALSHIFT) #define VAL(x) ((x & VALMASK) >> VALSHIFT)
#define IDX(r, c) (NDIGITS * (r) + (c))
struct sudoku { struct sudoku {
uint16_t cells[NDIGITS][NDIGITS]; uint16_t cells[NCELLS];
}; };
enum update_res { NOT_ALLOWED, ALREADY_DET, OK }; enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
@ -54,13 +56,10 @@ bool load(struct sudoku *sud, const char *ptr);
void save(struct sudoku *sud, char *ptr); void save(struct sudoku *sud, char *ptr);
/** /**
* Attempt to update the cell at row `r`, column `c` to have the value * Attempt to update the cell at index `i` to have the value
* `val`. Returns `OK` on success, `ALREADY_DET` if the cell is * `val`. Returns `OK`.
* already determined or `NOT_ALLOWED` if the cell being `val` would
* violate the sudoku rules.
*/ */
enum update_res enum update_res update(struct sudoku *sud, unsigned i, unsigned val);
update(struct sudoku *sud, unsigned r, unsigned c, unsigned val);
/** /**
* Print a string representation of the sudoku to stdout. * Print a string representation of the sudoku to stdout.