From da1ea8895d24aa571e9f3cc7ecca403d4cd889a6 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Fri, 13 Mar 2020 14:25:28 +0100 Subject: [PATCH] Add element-level optimizations --- board.c | 2 -- board.h | 6 +++++- main.c | 26 ++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/board.c b/board.c index 47f75e4..fbada8b 100644 --- a/board.c +++ b/board.c @@ -78,7 +78,6 @@ board_make_links (struct board *board) for (board_pos y = 0; y < 9; ++y) for (board_pos x = 0; x < 9; ++x) { - struct board_element *elem = BOARD_ELEM (board, x, y); unsigned pos = ELEM_POS (x, y); unsigned link_count = -1; @@ -586,7 +585,6 @@ board_place ( { if (board_meta_can_set (board, x, y, value)) { - struct board_element *elem = BOARD_ELEM (board, x, y); unsigned pos = ELEM_POS (x, y); /* Unmark all adjacent elements */ diff --git a/board.h b/board.h index d2ad6d4..f1042ac 100644 --- a/board.h +++ b/board.h @@ -203,6 +203,10 @@ board_unmark ( element_value value ); + +/** + * Removes a marking of a potential value of an element + */ bool elem_unmark ( struct board_element *elem, @@ -249,7 +253,7 @@ board_is_marked ( /** - * get whether or not a given element is marked with a particular value + * Get whether or not a given element is marked with a particular value */ bool elem_is_marked ( diff --git a/main.c b/main.c index 0c8c453..d2f1bab 100644 --- a/main.c +++ b/main.c @@ -319,6 +319,13 @@ first_potential_value (struct board_element *element, struct board *board, bool /** * Reduce away all elements on board with complexity=1 until none remain */ +#ifdef NOVERB +bool +simplify ( + struct boards_table *board_specs, + unsigned long long depth +) +#else bool simplify ( struct boards_table *board_specs, @@ -326,10 +333,12 @@ simplify ( unsigned long long *counter, unsigned verbosity ) +#endif { /* Get current table */ struct board *board = board_specs->board_specs[depth]; +#ifndef NOVERB if (verbosity > 0) { if (((*counter) & (0xFFFF >> (4 * (4 - verbosity)))) == 0) @@ -340,6 +349,7 @@ simplify ( } *counter += 1; } +#endif bool error; @@ -378,14 +388,14 @@ simplify ( struct board_element *elem = BOARD_ELEM (board, x, y); /* Find a simplest element on the board */ if ( - ! board_has_value (board, x, y) && + ! elem->has_value && elem->complexity == board->complexity ) for (element_value value = 0; value < 9; ++value) { /* Try speculative placement of each potential value and recurse */ tables_ensure_depth (board_specs, depth + 1); - if ((elem->potential & (1 << value)) != 0) + if (elem_is_marked (elem, value)) { struct board *board_spec = board_place_speculative ( @@ -399,19 +409,27 @@ simplify ( /* If speculative placement failed, try another value */ if (board_spec == NULL) { - if (! board_unmark (board, x, y, value)) + if (! elem_unmark (elem, value)) return false; continue; } /* Found solution */ + if ( +#ifdef NOVERB + simplify ( + board_specs, + depth + 1 + ) && +#else simplify ( board_specs, depth + 1, counter, verbosity ) && +#endif board_spec->complexity == 0) { board_copy (board_spec, board); @@ -419,7 +437,7 @@ simplify ( y = 9; value = 9; } - else if (! board_unmark (board, x, y, value)) + else if (! elem_unmark (elem, value)) return false; } }