sudoku-solver/main.c

73 lines
1.8 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 "ds.h"
#include "solve.h"
#include "sud.h"
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#define IFNAME "puzzles"
#define OFNAME "results"
#define FSIZE (NPUZZ * NCELLS)
int main(void)
{
int fd;
fd = open(IFNAME, O_RDONLY);
if (fd == -1) {
fputs("Failed to open puzzles file\n", stderr);
return EXIT_FAILURE;
}
char *ibuf = mmap(NULL, FSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
if (ibuf == MAP_FAILED) {
fputs("Failed to mmap() puzzles file\n", stderr);
return EXIT_FAILURE;
}
close(fd);
fd = open(OFNAME, O_RDWR);
if (fd == -1) {
fputs("Failed to open results file\n", stderr);
return EXIT_FAILURE;
}
char *obuf = mmap(NULL, FSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (obuf == MAP_FAILED) {
fputs("Failed to mmap() results file\n", stderr);
return EXIT_FAILURE;
}
close(fd);
struct sudoku sud;
for (unsigned o = 0; o < FSIZE; o += NCELLS) {
load(&sud, ibuf + o);
solve(&sud);
save(&sud, obuf + o);
}
munmap(ibuf, FSIZE);
munmap(obuf, FSIZE);
return EXIT_SUCCESS;
}