55 lines
1.4 KiB
C
55 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include "interference.h"
|
|
#include "puzzle.h"
|
|
#include "puzzle_io.h"
|
|
|
|
// run_puzzle solves a single puzzle and prints the output
|
|
void run_puzzle(char* puzzle_text);
|
|
|
|
int main(int argc, char* argv[]) {
|
|
char *filename;
|
|
|
|
if (argc < 2) {
|
|
filename = "sudoku_puzzles.txt";
|
|
} else {
|
|
filename = argv[1];
|
|
}
|
|
|
|
FILE *file = fopen(filename, "r");
|
|
if (file == NULL) {
|
|
perror("Failed to open file");
|
|
return -1;
|
|
}
|
|
|
|
char line[N_CELLS + 2];
|
|
// we expect a line of exactly N_CELLS+1 chars, where the last one is \n
|
|
// we replace the \n with a \0 for compatibility with puzzle_init_string
|
|
while (fgets(line, sizeof(line), file)) {
|
|
bool too_short = strlen(line) != N_CELLS + 1;
|
|
bool too_long = line[N_CELLS] != '\n';
|
|
if (too_short || too_long) {
|
|
crash("wrong number of characters in a puzzle");
|
|
}
|
|
line[N_CELLS] = '\0';
|
|
run_puzzle(line);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void run_puzzle(char* puzzle_text) {
|
|
// this only needs to be done once
|
|
// but it's harmless to do it more than once
|
|
interference_init();
|
|
|
|
// load and display info about the puzzle before and after solving
|
|
puzzle_t puzzle;
|
|
puzzle_init_string(&puzzle, puzzle_text);
|
|
|
|
printf("Initial state:\n\n");
|
|
puzzle_display(&puzzle);
|
|
printf("\nSolving...\n\n");
|
|
puzzle_solve(&puzzle);
|
|
puzzle_display(&puzzle);
|
|
printf("\n");
|
|
}
|