From 734c30391d96c4cb74e328f94b05b33b4e2db98a Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 11 Mar 2019 15:54:42 +1300 Subject: Replace cumbersome arrays with bitmasks --- solve.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'solve.c') diff --git a/solve.c b/solve.c index a96ddab..9cfa38f 100644 --- a/solve.c +++ b/solve.c @@ -29,7 +29,7 @@ int solve_row_col(struct cell (*b)[9][9]) for (i = 0; i < 9; i++) { - if ((*b)[x][y].not[i]) + if (CELL_IS_NOT((*b)[x][y].not, val)) { total++; } else { @@ -87,7 +87,7 @@ int not_to_val_cell(struct cell (*b)[9][9]) if (c->val) continue; - if (c->not[n-1] == 0) + if (!CELL_IS_NOT(c->not, n)) { potentials++; okx = x; @@ -103,15 +103,6 @@ int not_to_val_cell(struct cell (*b)[9][9]) (*b)[bx+okx][by+oky].val = n; potentials = 0; change_count++; - } else if(potentials) { - DEBUG_LOG("CELL: (%d,%d) could be one of %d\n", bx + okx, by + oky, potentials); - int k = 0; - for (k = 0; k < 9; k++) { - if (!(*b)[bx+okx][by+oky].not[k]) { - DEBUG_LOG(" %d", k+1); - } - } - DEBUG_LOG("\n\n"); } } } @@ -128,9 +119,8 @@ int cell_fill_certainties(struct cell (*b)[9][9], int bx, int by) int will_take_count = 0; int will_take_x = 0; int will_take_y = 0; -// struct cell *will_take = NULL; int val = 0; - int cell_has[9] = {0}; + int cell_has = 0; int x,y; /* Translate cell-based index to tile-based */ @@ -143,21 +133,21 @@ int cell_fill_certainties(struct cell (*b)[9][9], int bx, int by) val = (*b)[x][y].val; if (val) { DEBUG_LOG("CELL_CERT: Cell (%d,%d) has %d\n", bx/3, by/3, val); - cell_has[val - 1] = 1; + cell_has |= CELL_VALUE_MASK(val); } } } /* Second: For all missing values, see if only one tile will take it. * If only one will have it, put it in */ - for (val = 0; val < 9; val++) { + for (val = 1; val <= 9; val++) { will_take_count = 0; /* Does the cell lack the value? */ - if (!cell_has[val]) { + if (!(cell_has & CELL_VALUE_MASK(val))) { - DEBUG_LOG("Try to solve for %d\n", val + 1); + DEBUG_LOG("Try to solve for %d\n", val); /* Walk the cell and find which tile(s) will take it */ for (x = bx; x < bx + 3; x++) { @@ -165,7 +155,7 @@ int cell_fill_certainties(struct cell (*b)[9][9], int bx, int by) if ((*b)[x][y].val) { continue; } - if ((*b)[x][y].not[val] == 0) { + if (!CELL_IS_NOT((*b)[x][y].not, val)) { will_take_count++; will_take_x = x; will_take_y = y; @@ -177,8 +167,8 @@ int cell_fill_certainties(struct cell (*b)[9][9], int bx, int by) if (will_take_count == 1) { printf("%c%c must be %d because it is the only place in its boxthis will fit\n", NAME_CELL(will_take_x, will_take_y), - val + 1); - (*b)[will_take_x][will_take_y].val = val + 1; + val); + (*b)[will_take_x][will_take_y].val = val; change_count++; } } -- cgit v1.1