Compare commits
13 Commits
13be9261ac
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f797701718 | |||
| e974dc9171 | |||
| 21c10de9da | |||
| 088135ac32 | |||
| 4c707a0fa6 | |||
| 0453c21a3c | |||
| 8fefe103f0 | |||
| b12fff9d22 | |||
| 9c10b8efbf | |||
| c965f65422 | |||
| 330c42c4da | |||
| 69daa07b63 | |||
| a5b6c52053 |
24
src/main.c
24
src/main.c
@@ -3,8 +3,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sudoku.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
(void)argc, (void)argv;
|
||||
printf("%s\n", "Hello, World!");
|
||||
u16 board[SUDOKU_LEN] = {
|
||||
// clang-format off
|
||||
0x010, 0x004, 0x000, 0x000, 0x040, 0x000, 0x000, 0x000, 0x000,
|
||||
0x020, 0x000, 0x000, 0x001, 0x100, 0x010, 0x000, 0x000, 0x000,
|
||||
0x000, 0x100, 0x080, 0x000, 0x000, 0x000, 0x000, 0x020, 0x000,
|
||||
0x080, 0x000, 0x000, 0x000, 0x020, 0x000, 0x000, 0x000, 0x004,
|
||||
0x008, 0x000, 0x000, 0x080, 0x000, 0x004, 0x000, 0x000, 0x001,
|
||||
0x040, 0x000, 0x000, 0x000, 0x002, 0x000, 0x000, 0x000, 0x020,
|
||||
0x000, 0x020, 0x000, 0x000, 0x000, 0x000, 0x002, 0x080, 0x000,
|
||||
0x000, 0x000, 0x000, 0x008, 0x001, 0x100, 0x000, 0x000, 0x010,
|
||||
0x000, 0x000, 0x000, 0x000, 0x080, 0x000, 0x000, 0x040, 0x100,
|
||||
// clang-format on
|
||||
};
|
||||
sudoku_init(board);
|
||||
sudoku_print(board);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
sudoku_solve_step(board);
|
||||
sudoku_print(board);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
52
src/sudoku.c
52
src/sudoku.c
@@ -8,17 +8,55 @@
|
||||
|
||||
#include "util/intdef.h"
|
||||
|
||||
void sudoku_init(u16 *board) {
|
||||
for (uint i = 0; i < SUDOKU_LEN; i++) {
|
||||
board[i] |= SUDOKU_ALL & -!board[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* removes `val` from `*brd`, if `*brd` is not equal to `val` */
|
||||
static inline void setbrdpos(u16 *brd, u16 val) {
|
||||
*brd &= ~(val & -(*brd != val));
|
||||
}
|
||||
|
||||
void sudoku_place(u16 *brd, u16 val, uint idx) {
|
||||
uint x = idx % SUDOKU_DPT;
|
||||
uint y = idx / SUDOKU_DPT;
|
||||
|
||||
uint irow = idx - x;
|
||||
uint ibox = SUDOKU_DPT * (y % SUDOKU_BOXLEN) + SUDOKU_BOXLEN * (x % SUDOKU_BOXLEN);
|
||||
|
||||
for (uint i = 0; i < SUDOKU_DPT; i++) {
|
||||
setbrdpos(&brd[irow + i], val);
|
||||
setbrdpos(&brd[x + (i * SUDOKU_DPT)], val);
|
||||
setbrdpos(&brd[ibox + (SUDOKU_DPT * (i % SUDOKU_BOXLEN))], val);
|
||||
}
|
||||
}
|
||||
|
||||
void sudoku_solve_step(u16 *board) {
|
||||
u16 bmask[SUDOKU_DEPTH_2]; // board mask
|
||||
memcpy(bmask, board, SUDOKU_DEPTH_2 * 2);
|
||||
// TODO: first, we must somehow loop through the columns
|
||||
// TODO: then, we must write to the bmask or do *magic* for the bits designating to 1-9 to be unset if ONLY one of them is set
|
||||
for (uint i = 0; i < SUDOKU_LEN; i++) {
|
||||
switch (board[i]) {
|
||||
case SUDOKU_1:
|
||||
case SUDOKU_2:
|
||||
case SUDOKU_3:
|
||||
case SUDOKU_4:
|
||||
case SUDOKU_5:
|
||||
case SUDOKU_6:
|
||||
case SUDOKU_7:
|
||||
case SUDOKU_8:
|
||||
case SUDOKU_9:
|
||||
sudoku_place(board, board[i], i);
|
||||
// printf("%i,%i\n", i % SUDOKU_DPT, i / SUDOKU_DPT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sudoku_print(const u16 *board) {
|
||||
for (uint i = 0; i < SUDOKU_DEPTH_2; i++) {
|
||||
printf("+———-———-———-———-———-———-———-———-———+\n");
|
||||
for (uint i = 0; i < SUDOKU_LEN; i++) {
|
||||
char tile;
|
||||
switch (board[i]) {
|
||||
// case 0: tile = 'x'; break;
|
||||
case SUDOKU_1: tile = '1'; break;
|
||||
case SUDOKU_2: tile = '2'; break;
|
||||
case SUDOKU_3: tile = '3'; break;
|
||||
@@ -31,7 +69,9 @@ void sudoku_print(const u16 *board) {
|
||||
default: tile = ' '; break;
|
||||
};
|
||||
|
||||
printf("|%c ", tile);
|
||||
printf("| %c ", tile);
|
||||
// printf("|%3X", board[i]);
|
||||
if (i % 9 == 8) printf("|\n");
|
||||
}
|
||||
printf("+———-———-———-———-———-———-———-———-———+\n");
|
||||
}
|
||||
|
||||
16
src/sudoku.h
16
src/sudoku.h
@@ -4,8 +4,9 @@
|
||||
|
||||
#include "util/intdef.h"
|
||||
|
||||
#define SUDOKU_DEPTH 9 // defines the "depth" of the puzzle.
|
||||
#define SUDOKU_DEPTH_2 (SUDOKU_DEPTH * SUDOKU_DEPTH) // computes how many squares there are
|
||||
#define SUDOKU_BOXLEN 3
|
||||
#define SUDOKU_DPT 9
|
||||
#define SUDOKU_LEN (SUDOKU_DPT * SUDOKU_DPT)
|
||||
|
||||
/* bitmask */
|
||||
enum sudoku_bitmask {
|
||||
@@ -18,10 +19,17 @@ enum sudoku_bitmask {
|
||||
SUDOKU_7 = 0x040,
|
||||
SUDOKU_8 = 0x080,
|
||||
SUDOKU_9 = 0x100,
|
||||
SUDOKU_ALL = 0x01FF,
|
||||
};
|
||||
|
||||
/* TODO: write documentation */
|
||||
/* initialises the sudoku board to `SUDOKU_ALL`. (if the tile is empty) */
|
||||
void sudoku_init(u16 *board);
|
||||
|
||||
/* places `val` at `idx` in `board`, and removes `val` from the possibilities in neighbouring tiles */
|
||||
void sudoku_place(u16 *board, u16 val, uint idx);
|
||||
|
||||
/* loops through the board, updating all places that only have a single possibility set. */
|
||||
void sudoku_solve_step(u16 *board);
|
||||
|
||||
/* TODO: write documentation */
|
||||
/* prints the sudoku board to `stdout` */
|
||||
void sudoku_print(const u16 *board);
|
||||
|
||||
Reference in New Issue
Block a user