sudoku-solver/main.c

100 lines
2.3 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 "solve.h"
#include "sud.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define PROG_UPDATEPRD 20
#define NPUZZLES 1024U
static uint32_t getseed(void)
{
FILE *urandom = fopen("/dev/urandom", "rb");
if (urandom == NULL)
fprintf(stderr, "Failed to open /dev/urandom\n");
uint32_t seed = 0;
for (unsigned i = 0; i < 4; ++i)
seed = seed << 8 | fgetc(urandom);
fclose(urandom);
return seed;
}
int main(void)
{
uint32_t seed = getseed();
printf("Seed: %u\n\n", seed);
srand(seed);
struct sudoku puzzles[NPUZZLES];
unsigned i, j, bslen = 0;
fputs("Generating... ", stdout);
for (i = 0; i < NPUZZLES; ++i) {
if (i % PROG_UPDATEPRD == 0) {
for (j = 0; j < bslen; ++j)
putchar('\b');
bslen = (unsigned)printf("%u%%", 100 * i / NPUZZLES);
fflush(stdout);
}
gen(&puzzles[i]);
}
for (j = 0; j < bslen; ++j)
putchar('\b');
puts("100%");
bool res[NPUZZLES];
fputs("Solving... ", stdout);
for (i = 0; i < NPUZZLES; ++i)
res[i] = solve(&puzzles[i]);
puts("done");
unsigned error = 0, incomplete = 0, incorrect = 0, solved = 0;
for (i = 0; i < NPUZZLES; ++i) {
if (!res[i]) {
++error;
continue;
}
switch (check(&puzzles[i])) {
case INCOMPLETE:
++incomplete;
break;
case INCORRECT:
++incorrect;
break;
case SOLVED:
++solved;
break;
}
}
puts("\n SUMMARY\n =======");
printf("Solved: %4u/%u\n", solved, NPUZZLES);
printf("Incomplete: %4u/%u\n", incomplete, NPUZZLES);
printf("Incorrect: %4u/%u\n", incorrect, NPUZZLES);
printf("Solver error: %4u/%u\n", error, NPUZZLES);
return 0;
}