/*
* 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
* .
*/
#ifndef SUD_H
#define SUD_H
#include
#define SEGLEN 3
#define NDIGITS 9
#define NCELLS 81
#define DETMASK 0x8000
#define VALSHIFT 9
#define VALMASK 0x1e00
#define PVALSMASK 0x01ff
#define DET(x) (x & DETMASK)
#define VAL(x) ((x & VALMASK) >> VALSHIFT)
#define IDX(r, c) (NDIGITS * (r) + (c))
struct sudoku {
uint16_t cells[NCELLS];
};
enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
enum check_res { INCOMPLETE, INCORRECT, SOLVED };
/**
* Read `NCELLS` values from the given pointer and load them into the
* sudoku.
*/
int load(struct sudoku *sud, const char *ptr);
/**
* Write the sudoku to the given pointer (`NCELLS` bytes).
*/
void save(struct sudoku *sud, char *ptr);
/**
* Attempt to update the cell at index `i` to have the value
* `val`. Returns `OK`.
*/
enum update_res update(struct sudoku *sud, unsigned i, unsigned val);
/**
* Print a string representation of the sudoku to stdout.
*/
void print(const struct sudoku *sud);
/**
* Determine whether the sudoku has been solved correctly, contains
* invalid choices or is incomplete.
*/
enum check_res check(const struct sudoku *sud);
/**
* Determine whether all the sudoku's cells have been determined.
*/
int filled(const struct sudoku *sud);
#endif