Use uint16_t instead of struct for cell state

- Bits 0-8 are the possible values
- Bits 9-12 is the concrete value
- Bit 15 is 1 if the cell is determined
This commit is contained in:
2022-11-24 00:37:48 +00:00
parent 4a2cfb392f
commit f51e9a06b0
3 changed files with 49 additions and 41 deletions

26
sud.h
View File

@@ -20,21 +20,21 @@
#define SUD_H
#include <stdbool.h>
#include <stdint.h>
#define SEGLEN 3
#define NDIGITS (SEGLEN * SEGLEN)
#define NCELLS (NDIGITS * NDIGITS)
#define NDIGITS 9
#define NCELLS 81
struct cellstate {
bool det;
union {
unsigned val;
bool pvals[NDIGITS];
};
};
#define DETMASK 0x8000
#define VALSHIFT 9
#define VALMASK 0x1e00
#define DET(x) (x & DETMASK)
#define VAL(x) ((x & VALMASK) >> VALSHIFT)
struct sudoku {
struct cellstate cells[NDIGITS][NDIGITS];
uint16_t cells[NDIGITS][NDIGITS];
};
enum update_res { NOT_ALLOWED, ALREADY_DET, OK };
@@ -47,12 +47,6 @@ enum check_res { INCOMPLETE, INCORRECT, SOLVED };
*/
bool load(struct sudoku *sud, const char *ptr);
/**
* Populate the sudoku with some random values. The proportion of
* cells that are filled will be approximately `fill_prop`.
*/
void gen(struct sudoku *sud, double fill_prop);
/**
* Attempt to update the cell at row `r`, column `c` to have the value
* `val`. Returns `OK` on success, `ALREADY_DET` if the cell is