Created
October 25, 2019 00:20
-
-
Save Andersama/5f173ffd2e78499b942c610ea5b4798d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <algorithm> | |
| #include <deque> | |
| #include <vector> | |
| #include <array> | |
| #include <set> | |
| #include <thread> | |
| #include <future> | |
| #include <execution> | |
| #include <random> | |
| #ifdef _WIN32 | |
| #include <windows.h> | |
| #endif | |
| using namespace std; | |
| #ifdef max | |
| #define tmax max | |
| #undef max | |
| #endif | |
| inline std::ostream& yellow(std::ostream& s) | |
| { | |
| #ifdef _WIN32 | |
| HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
| SetConsoleTextAttribute(hStdout, | |
| FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); | |
| #endif | |
| return s; | |
| } | |
| inline std::ostream& white(std::ostream& s) | |
| { | |
| #ifdef _WIN32 | |
| HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
| SetConsoleTextAttribute(hStdout, | |
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); | |
| #endif | |
| return s; | |
| } | |
| void printSudoku(const vector<vector<int>> &state) | |
| { | |
| size_t puzzle_dim = state.size(); | |
| size_t block_dim = (uint32_t)std::sqrt(puzzle_dim); | |
| std::string out; | |
| out.reserve(puzzle_dim * puzzle_dim * 8); | |
| for (size_t i = 0; i < puzzle_dim; i++) { | |
| for (size_t j = 0; j < puzzle_dim; j++) { | |
| std::string block = "[_]"; | |
| if (state[i][j] >= 1 && state[i][j] <= puzzle_dim) | |
| block = "[" + std::to_string(state[i][j]) + "]"; | |
| if (j % block_dim == (block_dim - 1)) | |
| block += " "; | |
| out += block; | |
| } | |
| out += "\n"; | |
| if (i % block_dim == (block_dim - 1)) | |
| out += "\n"; | |
| } | |
| out += "\n"; | |
| std::cout << out; | |
| } | |
| void printChangedSudoku(const vector<vector<int>> &state, int row, int col) | |
| { | |
| size_t puzzle_dim = state.size(); | |
| size_t block_dim = (uint32_t)std::sqrt(puzzle_dim); | |
| for (size_t i = 0; i < puzzle_dim; i++) { | |
| for (size_t j = 0; j < puzzle_dim; j++) { | |
| std::string block = "[_]"; | |
| if (state[i][j] >= 1 && state[i][j] <= puzzle_dim) | |
| block = "[" + std::to_string(state[i][j]) + "]"; | |
| if (j % block_dim == (block_dim - 1)) | |
| block += " "; | |
| if (row == i && col == j) | |
| std::cout << yellow << block; | |
| else | |
| std::cout << white << block; | |
| } | |
| std::cout << white << endl; | |
| if (i % block_dim == (block_dim - 1)) | |
| std::cout << white << endl; | |
| } | |
| std::cout << endl; | |
| } | |
| using choice_type = vector<uint32_t>; | |
| struct sudoku_choice { | |
| uint8_t x; | |
| uint8_t y; | |
| uint8_t b; | |
| uint8_t c; | |
| uint32_t modifier; | |
| choice_type choices; | |
| }; | |
| using puzzle_type = vector<vector<int>>; | |
| using set_type = vector<uint8_t>; | |
| using tag_type = vector<set_type>; | |
| using stack_type = vector<sudoku_choice>; | |
| using solution_type = tuple<puzzle_type, stack_type, size_t>; | |
| void printStack(const stack_type& stack) { | |
| string out; | |
| out.reserve(stack.size() * (16 + 8)); | |
| for (int i = 0; i < stack.size(); i++) { | |
| out += "[x: "+ to_string(stack[i].x) +" y: " + to_string(stack[i].y) +" m: " + to_string(stack[i].modifier)+"]\n"; | |
| } | |
| cout << out; | |
| } | |
| constexpr bool within_range(int value, int width) { | |
| return value > 0 && value <= width; | |
| } | |
| void get_block_head(int& x, int& y, int sq) { | |
| x /= sq; | |
| y /= sq; | |
| x *= sq; | |
| y *= sq; | |
| } | |
| int get_block(int x, int y, int sq) { | |
| x /= sq; | |
| y /= sq; | |
| return y * sq + x; | |
| } | |
| bool stack_ordering(const sudoku_choice& lhs, const sudoku_choice& rhs) { | |
| return lhs.choices.size() < rhs.choices.size(); | |
| } | |
| bool heap_ordering(const sudoku_choice& lhs, const sudoku_choice& rhs) { | |
| return lhs.choices.size() > rhs.choices.size(); | |
| } | |
| set_type set_group(set_type &a, set_type& b, set_type& c) { | |
| set_type ret; | |
| ret.reserve(std::max(a.size(), std::max(b.size(), c.size()))); | |
| for (size_t i = 0; i < a.size(); i++) | |
| ret[i] = a[i]; | |
| for (size_t i = 0; i < b.size(); i++) | |
| ret[i] += b[i]; | |
| for (size_t i = 0; i < c.size(); i++) | |
| ret[i] += c[i]; | |
| return ret; | |
| } | |
| template<class T, class Iter> | |
| void set_diff(T& cont, Iter first, Iter last) | |
| { | |
| auto new_end = std::set_difference( // (1) | |
| cont.begin(), cont.end(), first, last, cont.begin()); | |
| cont.erase(new_end, cont.end()); | |
| } | |
| constexpr int incremental_sum(int i) { | |
| int sum = 0; | |
| for (int c = 1; c <= i; c++) | |
| sum += c; | |
| return sum; | |
| } | |
| bool puzzle_validator(const puzzle_type &puzzle) { | |
| int size = puzzle.size(); | |
| int sq = std::sqrt(size); | |
| int checksum = incremental_sum(size); | |
| vector<int> row_sum(size, 0); | |
| vector<int> col_sum(size, 0); | |
| vector<int> blk_sum(size, 0); | |
| for (size_t y = 0; y < size; y++) { | |
| for (size_t x = 0; x < size; x++) { | |
| row_sum[y] += puzzle[y][x]; | |
| col_sum[x] += puzzle[y][x]; | |
| int b = get_block(x, y, sq); | |
| blk_sum[b] += puzzle[y][x]; | |
| } | |
| } | |
| for (size_t i = 0; i < size; i++) { | |
| if (row_sum[i] != checksum || | |
| col_sum[i] != checksum || | |
| blk_sum[i] != checksum | |
| ) | |
| return false; | |
| } | |
| return true; | |
| } | |
| bool stack_validator(const puzzle_type& puzzle, const stack_type& stack) { | |
| for (sudoku_choice c : stack) { | |
| auto it = std::find(c.choices.begin(), c.choices.end(), puzzle[c.y][c.x]); | |
| if (it == c.choices.end()) | |
| return false; | |
| } | |
| return true; | |
| } | |
| bool stack_has_no_options(const stack_type& stack) { | |
| for (sudoku_choice c : stack) { | |
| if (c.choices.size() > 1) | |
| return false; | |
| } | |
| return true; | |
| } | |
| /* Basic check across rows, columns and the block */ | |
| choice_type simple_choices(const sudoku_choice &s, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags) { | |
| choice_type ret; | |
| ret.reserve(row_tags.size()); | |
| for (int i = 1; i <= row_tags.size(); i++) { | |
| if ((row_tags[s.y][i] | col_tags[s.x][i] | blk_tags[s.b][i]) == 0) | |
| ret.push_back(i); | |
| } | |
| return ret; | |
| } | |
| std::random_device r; | |
| std::mt19937_64 mt_gen(r()); | |
| choice_type random_choices(const sudoku_choice& s, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags) { | |
| choice_type ret; | |
| ret.reserve(row_tags.size()); | |
| for (int i = 1; i <= row_tags.size(); i++) { | |
| if ((row_tags[s.y][i] | col_tags[s.x][i] | blk_tags[s.b][i]) == 0) | |
| ret.push_back(i); | |
| } | |
| std::shuffle(ret.begin(), ret.end(), mt_gen); | |
| return ret; | |
| } | |
| void basic_simplify(int index, stack_type& stack, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags, uint32_t modifier = 1) { | |
| int end = stack.size(); | |
| for (int i = index; i < end; i++) { | |
| stack[i].choices = simple_choices(stack[i], row_tags, col_tags, blk_tags); | |
| stack[i].c = 0; | |
| stack[i].modifier = stack[i].choices.size() == 1 ? modifier : 0; | |
| } | |
| } | |
| void basic_random_simplify(int index, stack_type& stack, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags, uint32_t modifier = 1) { | |
| int end = stack.size(); | |
| for (int i = index; i < end; i++) { | |
| stack[i].choices = random_choices(stack[i], row_tags, col_tags, blk_tags); | |
| stack[i].c = 0; | |
| stack[i].modifier = stack[i].choices.size() == 1 ? modifier : 0; | |
| } | |
| } | |
| void basic_set_choice(int index, stack_type& stack) { | |
| const int end = stack.size(); | |
| for (int i = index; i < end; i++) | |
| stack[i].c = stack[i].choices.size() - 1; | |
| } | |
| void rbasic_simplify(int index, stack_type& stack, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags, uint32_t modifier = 1) { | |
| const int end = stack.size(); | |
| for (int i = index; i < end; i++) { | |
| stack[i].choices = simple_choices(stack[i], row_tags, col_tags, blk_tags); | |
| stack[i].modifier = stack[i].choices.size() == 1 ? modifier : 0; | |
| } | |
| } | |
| void space_simplify(int index, stack_type& stack, const puzzle_type& puzzle, | |
| const tag_type& row_tags, const tag_type& col_tags, vector<int> &filled, uint32_t modifier = 2) { | |
| int end = stack.size(); | |
| int size = puzzle.size(); | |
| int sq = std::sqrt(puzzle.size()); | |
| std::vector<bool> nfilled(size, false); | |
| /* Prefill and mark which spaces are open for which blocks | |
| requires a bit of shuffling around | |
| */ | |
| #if 0 | |
| std::vector<bool> filled(size*size, false); | |
| std::vector<int> idxs(size, 0); | |
| //filled.reserve(size * size); | |
| for (int y = 0; y < size; y++) { | |
| for (int x = 0; x < size; x++) { | |
| int b = get_block(x, y, sq); | |
| int i = idxs[b]; | |
| idxs[b]++; | |
| filled[b * size + i] = within_range(puzzle[y][x], size); | |
| } | |
| } | |
| #else | |
| //std::vector<bool> filled; | |
| filled.clear(); | |
| filled.reserve((size_t)size * (size_t)size); | |
| for (int b = 0; b < size; b++) { | |
| for (int y = 0; y < sq; y++) { | |
| for (int x = 0; x < sq; x++) { | |
| int bx = b % sq; | |
| int by = b / sq; | |
| filled.push_back(within_range(puzzle[by * sq + y][bx * sq + x], size)); | |
| } | |
| } | |
| } | |
| #endif | |
| for (int i = index; i < end; i++) { | |
| int bx = stack[i].x; | |
| int by = stack[i].y; | |
| get_block_head(bx, by, sq); | |
| int choices = stack[i].choices.size(); | |
| #if 0 | |
| std::for_each(std::execution::par_unseq, stack[i].choices.begin(), stack[i].choices.end(), [&filled, &size, &stack, &i, | |
| &row_tags, &col_tags, &bx, &by, &sq, &modifier, &choices](uint32_t &v) { | |
| auto it = std::find(stack[i].choices.begin(), stack[i].choices.end(), v); | |
| int current = std::distance(stack[i].choices.begin(), it); | |
| std::vector<bool> nfilled; | |
| nfilled.reserve(size); | |
| std::copy_n(filled.begin() + (size * stack[i].b), size, nfilled.begin()); | |
| /*markup the spaces taken up by the value we're looking at*/ | |
| for (int t = 0; t < sq; t++) { | |
| if (row_tags[t + by][v] > 0) { | |
| for (int x = 0; x < sq; x++) { | |
| nfilled[t * sq + x] = true; | |
| } | |
| } | |
| if (col_tags[t + bx][v] > 0) { | |
| for (int y = 0; y < sq; y++) { | |
| nfilled[y * sq + t] = true; | |
| } | |
| } | |
| } | |
| /*count the number of open spaces*/ | |
| //int open_spots = std::count(nfilled.begin(), nfilled.end(), false); | |
| int open_spots = 0; | |
| for (int idx = 0; idx < filled.size() && open_spots < 2; idx++) { | |
| if (!filled[idx]) | |
| open_spots++; | |
| } | |
| /*if 0 open spaces, this number can't fit here, mark this number for deletion*/ | |
| if (open_spots == 0) { | |
| stack[i].modifier = modifier; | |
| stack[i].choices[current] = 0; | |
| /*if 1 open space, this number must fit here, mark others for deletion*/ | |
| } | |
| else if (open_spots == 1) { | |
| stack[i].modifier = modifier; | |
| int t = 0; | |
| for (; t < current; t++) | |
| stack[i].choices[t] = 0; | |
| t++; | |
| for (; t < choices; t++) | |
| stack[i].choices[t] = 0; | |
| } | |
| }); | |
| #else | |
| for (int current = 0; current < choices; current++) { | |
| int v = stack[i].choices[current]; | |
| /*copy over the filled in section we were examining*/ | |
| std::copy_n(filled.begin() + (size * stack[i].b), size, nfilled.begin()); | |
| /*markup the spaces taken up by the value we're looking at*/ | |
| for (int t = 0; t < sq; t++) { | |
| if (row_tags[t + by][v] > 0) { | |
| for (int x = 0; x < sq; x++) { | |
| nfilled[t * sq + x] = true; | |
| } | |
| } | |
| if (col_tags[t + bx][v] > 0) { | |
| for (int y = 0; y < sq; y++) { | |
| nfilled[y * sq + t] = true; | |
| } | |
| } | |
| } | |
| /*count the number of open spaces*/ | |
| //int open_spots = std::count(nfilled.begin(), nfilled.end(), false); | |
| int open_spots = 0; | |
| for (int idx = 0; idx < filled.size() && open_spots < 2; idx++) { | |
| if (!filled[idx]) | |
| open_spots++; | |
| } | |
| /*if 0 open spaces, this number can't fit here, mark this number for deletion*/ | |
| if (open_spots == 0) { | |
| stack[i].modifier = modifier; | |
| stack[i].choices[current] = 0; | |
| /*if 1 open space, this number must fit here, mark others for deletion*/ | |
| } else if (open_spots == 1) { | |
| stack[i].modifier = modifier; | |
| int t = 0; | |
| for (; t < current; t++) | |
| stack[i].choices[t] = 0; | |
| t++; | |
| for (; t < choices; t++) | |
| stack[i].choices[t] = 0; | |
| } | |
| } | |
| #endif | |
| /*delete options marked as 0*/ | |
| stack[i].choices.erase(std::remove_if(stack[i].choices.begin(), stack[i].choices.end(), [](uint32_t& v) { | |
| return v == 0; | |
| }), stack[i].choices.end()); | |
| } | |
| } | |
| void set_simplify(int index, stack_type& stack, const puzzle_type& puzzle, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags, uint32_t modifier = 3) { | |
| const int end = stack.size(); | |
| array<vector<int>, 3> incl; | |
| array<vector<int>, 3> excl; | |
| for (int i = 0; i < 3; i++) { | |
| incl[i].reserve(puzzle.size()); | |
| excl[i].reserve(puzzle.size()); | |
| } | |
| for (int lhs = index; lhs < end; lhs++) { | |
| sudoku_choice& s_lhs = stack[lhs]; | |
| /* | |
| if (s_lhs.choices.size() < 2) | |
| continue; | |
| */ | |
| for (int rhs = index; rhs < end; rhs++) { | |
| sudoku_choice& s_rhs = stack[rhs]; | |
| bool row_common = s_lhs.y == s_rhs.y; | |
| bool col_common = s_lhs.x == s_rhs.x; | |
| bool blk_common = s_lhs.b == s_rhs.b; | |
| if (!(row_common | col_common | blk_common)) | |
| continue; | |
| //||s_lhs.choices.size() < s_rhs.choices.size() | |
| //lhs must have more or equal numbers of items | |
| bool includes = std::includes(s_lhs.choices.begin(), | |
| s_lhs.choices.end(), s_rhs.choices.begin(), s_rhs.choices.end()); | |
| if (includes) { | |
| if (row_common) | |
| incl[0].push_back(rhs); | |
| if (col_common) | |
| incl[1].push_back(rhs); | |
| if (blk_common) | |
| incl[2].push_back(rhs); | |
| } else { | |
| if (row_common) | |
| excl[0].push_back(rhs); | |
| if (col_common) | |
| excl[1].push_back(rhs); | |
| if (blk_common) | |
| excl[2].push_back(rhs); | |
| } | |
| if (incl[0].size() > s_lhs.choices.size() && | |
| incl[1].size() > s_lhs.choices.size() && | |
| incl[2].size() > s_lhs.choices.size()) | |
| break; | |
| } | |
| //the set we've found exactly matches in size to the # of choices | |
| //we can now remove these choices from the excl list | |
| for (int i = 0; i < 3; i++) { | |
| if (incl[i].size() == s_lhs.choices.size()) { | |
| for (int j = 0; j < excl[i].size(); j++) { | |
| sudoku_choice& s_rhs = stack[excl[i][j]]; | |
| s_rhs.modifier = modifier; | |
| set_diff(s_rhs.choices, | |
| s_lhs.choices.begin(), | |
| s_lhs.choices.end()); | |
| } | |
| } | |
| //start over | |
| incl[i].clear(); | |
| excl[i].clear(); | |
| } | |
| } | |
| } | |
| void graph_simplify(int index, stack_type& stack, const puzzle_type& puzzle, | |
| const tag_type& row_tags, const tag_type& col_tags, const tag_type& blk_tags, uint32_t modifier = 4) | |
| { | |
| array<vector<int>, 3> incl; | |
| array<vector<int>, 3> excl; | |
| for (int i = 0; i < 3; i++) { | |
| incl[i].reserve(puzzle.size()); | |
| excl[i].reserve(puzzle.size()); | |
| } | |
| array<vector<vector<int>>, 3> graphs; | |
| for (int i = 0; i < 3; i++) { | |
| graphs[i].reserve(puzzle.size()*2 + 1); | |
| for (int j = 0; j < puzzle.size()*2 + 1; j++) { | |
| graphs[i].push_back(vector<int>(puzzle.size()*2 + 1, 0)); | |
| } | |
| } | |
| auto connect_nodes = [&puzzle](const sudoku_choice &pick, vector<vector<int>> &graph, int n) { | |
| int offset = n + puzzle.size() + 1; | |
| for (int v : pick.choices) { | |
| graph[offset][v] = 1; | |
| graph[v][offset] = 1; | |
| } | |
| graph[0][offset] = 0; | |
| graph[offset][0] = 0; | |
| }; | |
| auto connected_nodes = [](vector<vector<int>>& graph, int v) { | |
| return std::count(graph[v].begin(), graph[v].end(), 1); | |
| }; | |
| for (int lhs = index; lhs < stack.size(); lhs++) { | |
| sudoku_choice& s_lhs = stack[lhs]; | |
| //collect things | |
| int n0 = 1; | |
| int n1 = 1; | |
| int n2 = 1; | |
| for (int rhs = index; rhs < stack.size(); rhs++) { | |
| sudoku_choice& s_rhs = stack[rhs]; | |
| bool row_common = s_lhs.y == s_rhs.y; | |
| bool col_common = s_lhs.x == s_rhs.x; | |
| bool blk_common = s_lhs.b == s_rhs.b; | |
| if (!(row_common | col_common | blk_common)) | |
| continue; | |
| if (row_common) { | |
| connect_nodes(s_rhs, graphs[0], n0++); | |
| incl[0].push_back(rhs); | |
| } | |
| if (col_common) { | |
| connect_nodes(s_rhs, graphs[1], n1++); | |
| incl[1].push_back(rhs); | |
| } | |
| if (blk_common) { | |
| connect_nodes(s_rhs, graphs[2], n2++); | |
| incl[2].push_back(rhs); | |
| } | |
| } | |
| //solve things | |
| //repeat | |
| for (int i = 0; i < 3; i++) { | |
| for (int j = 0; j < puzzle.size() + 1; j++) { | |
| std::fill(graphs[i][j].begin(), graphs[i][j].end(), 0); | |
| } | |
| } | |
| } | |
| } | |
| //#define VISUALIZE_SUDOKU_SOLVE 1 | |
| std::tuple<puzzle_type, stack_type, size_t> solver(puzzle_type state, stack_type stack = {}) | |
| { | |
| tag_type row_tags; | |
| tag_type col_tags; | |
| tag_type blk_tags; | |
| vector<uint32_t> filled; | |
| vector<int> taken; | |
| //reserve enough space for how many steps we may at most take* | |
| taken.reserve(state.size() * state.size()); | |
| filled.reserve(state.size() * state.size()); | |
| stack.reserve(state.size() * state.size()); | |
| row_tags.reserve(state.size()); | |
| col_tags.reserve(state.size()); | |
| blk_tags.reserve(state.size()); | |
| size_t w_sq = std::sqrt(state.size()); | |
| for (size_t i = 0; i < state.size(); i++) { | |
| filled.push_back(i + 1); | |
| //to keep things simple we count how many of a # at a particular spot there are | |
| //so we reserve an extra spot to ignore 0* | |
| row_tags.push_back(set_type(state.size() + 1, 0)); | |
| col_tags.push_back(set_type(state.size() + 1, 0)); | |
| blk_tags.push_back(set_type(state.size() + 1, 0)); | |
| } | |
| auto simplify = [&stack, &state, &row_tags, &col_tags, &blk_tags, &taken](int idx) { | |
| basic_simplify(idx, stack, row_tags, col_tags, blk_tags, 1); | |
| space_simplify(idx, stack, state, row_tags, col_tags, taken, 2); | |
| set_simplify(idx, stack, state, row_tags, col_tags, blk_tags, 3); | |
| //std::make_heap(stack.begin() + idx, stack.end(), heap_ordering); | |
| auto it = std::min_element(stack.begin() + idx, stack.end(), stack_ordering); | |
| size_t d = std::distance(stack.begin(), it); | |
| std::swap(stack[idx], stack[d]); | |
| }; | |
| //are we already solved? | |
| int index; | |
| if (stack.size() && puzzle_validator(state) && stack_validator(state, stack)) { | |
| sudoku_choice& picked = stack.back(); | |
| //clear a previously made choice | |
| int previous = state[picked.y][picked.x]; | |
| row_tags[picked.y][previous]--; | |
| col_tags[picked.x][previous]--; | |
| blk_tags[picked.b][previous]--; | |
| state[picked.y][picked.x] = 0; | |
| index = stack.size() - 2; | |
| filled.clear(); | |
| } | |
| else { | |
| stack.clear(); | |
| for (size_t y = 0; y < state.size(); y++) { | |
| for (size_t x = 0; x < state.size(); x++) { | |
| const int v = state[y][x]; | |
| const int b = get_block(x, y, w_sq); | |
| if (!within_range(v, state.size())) { | |
| stack.push_back({ (uint8_t)x, | |
| (uint8_t)y, | |
| (uint8_t)b, (uint8_t)0, 0, }); | |
| } | |
| else { | |
| row_tags[y][v]++; | |
| col_tags[x][v]++; | |
| blk_tags[b][v]++; | |
| } | |
| } | |
| } | |
| filled.clear(); | |
| simplify(0); | |
| index = 0; | |
| } | |
| //do solve | |
| const int size = stack.size(); | |
| for (; index >= 0 && index < size;) { | |
| sudoku_choice& picked = stack[index]; | |
| //clear a previously made choice | |
| pick_next: | |
| int prev = state[picked.y][picked.x]; | |
| if (prev > 0) { | |
| row_tags[picked.y][prev]--; | |
| col_tags[picked.x][prev]--; | |
| blk_tags[picked.b][prev]--; | |
| state[picked.y][picked.x] = 0; | |
| } | |
| if (picked.c < picked.choices.size()) { | |
| //make choice | |
| int next = picked.choices[picked.c]; | |
| int next_index = index + 1; | |
| row_tags[picked.y][next]++; | |
| col_tags[picked.x][next]++; | |
| blk_tags[picked.b][next]++; | |
| state[picked.y][picked.x] = next; | |
| picked.c++; | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printChangedSudoku(state, picked.y, picked.x); | |
| #endif | |
| //simplify next | |
| if (next_index < size) { | |
| simplify(next_index); | |
| //peek ahead, if no possible choice loop on self | |
| if (!stack[next_index].choices.size()) | |
| goto pick_next; | |
| } | |
| index++; | |
| } | |
| else { | |
| index--; | |
| } | |
| } | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printSudoku(state); | |
| #endif | |
| return { state, stack, index }; | |
| } | |
| std::tuple<puzzle_type, stack_type, size_t> rsolver(puzzle_type state, stack_type stack = {}) | |
| { | |
| tag_type row_tags; | |
| tag_type col_tags; | |
| tag_type blk_tags; | |
| vector<uint32_t> filled; | |
| vector<int> taken; | |
| taken.reserve(state.size() * state.size()); | |
| stack.reserve(state.size() * state.size()); | |
| row_tags.reserve(state.size()); | |
| col_tags.reserve(state.size()); | |
| blk_tags.reserve(state.size()); | |
| size_t w_sq = std::sqrt(state.size()); | |
| for (size_t i = 0; i < state.size(); i++) { | |
| filled.push_back(i + 1); | |
| row_tags.push_back(set_type(state.size() + 1, 0)); | |
| col_tags.push_back(set_type(state.size() + 1, 0)); | |
| blk_tags.push_back(set_type(state.size() + 1, 0)); | |
| } | |
| auto simplify = [&stack, &state, &row_tags, &col_tags, &blk_tags, &taken](int idx) { | |
| rbasic_simplify(idx, stack, row_tags, col_tags, blk_tags, 1); | |
| space_simplify(idx, stack, state, row_tags, col_tags, taken, 2); | |
| set_simplify(idx, stack, state, row_tags, col_tags, blk_tags, 3); | |
| basic_set_choice(idx, stack); | |
| //std::make_heap(stack.begin() + idx, stack.end(), heap_ordering); | |
| auto it = std::min_element(stack.begin() + idx, stack.end(), stack_ordering); | |
| size_t d = std::distance(stack.begin(), it); | |
| std::swap(stack[idx], stack[d]); | |
| }; | |
| //are we already solved? | |
| int index; | |
| if (stack.size() && puzzle_validator(state) && stack_validator(state, stack)) { | |
| sudoku_choice& picked = stack.back(); | |
| //clear a previously made choice | |
| int previous = state[picked.y][picked.x]; | |
| row_tags[picked.y][previous]--; | |
| col_tags[picked.x][previous]--; | |
| blk_tags[picked.b][previous]--; | |
| state[picked.y][picked.x] = 0; | |
| index = stack.size() - 2; | |
| } | |
| else { | |
| size_t l = state.size() - 1; | |
| stack.clear(); | |
| for (size_t y = 0; y < state.size(); y++) { | |
| for (size_t x = 0; x < state.size(); x++) { | |
| const int v = state[y][x]; | |
| const int b = get_block(x, y, w_sq); | |
| if (!within_range(v, state.size())) { | |
| stack.push_back({ (uint8_t)x, | |
| (uint8_t)y, | |
| (uint8_t)b, (uint8_t)l, 0, filled }); | |
| } | |
| else { | |
| row_tags[y][v]++; | |
| col_tags[x][v]++; | |
| blk_tags[b][v]++; | |
| } | |
| } | |
| } | |
| simplify(0); | |
| index = 0; | |
| } | |
| //do solve | |
| const int size = stack.size(); | |
| for (; index >= 0 && index < size;) { | |
| sudoku_choice& picked = stack[index]; | |
| //clear a previously made choice | |
| pick_next: | |
| int prev = state[picked.y][picked.x]; | |
| if (prev > 0) { | |
| row_tags[picked.y][prev]--; | |
| col_tags[picked.x][prev]--; | |
| blk_tags[picked.b][prev]--; | |
| state[picked.y][picked.x] = 0; | |
| } | |
| if (picked.c < picked.choices.size()) { | |
| //make choice | |
| int next = picked.choices[picked.c]; | |
| int next_index = index + 1; | |
| row_tags[picked.y][next]++; | |
| col_tags[picked.x][next]++; | |
| blk_tags[picked.b][next]++; | |
| state[picked.y][picked.x] = next; | |
| picked.c--; | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printChangedSudoku(state, picked.y, picked.x); | |
| #endif | |
| //simplify next | |
| if (next_index < size) { | |
| simplify(next_index); | |
| //peek ahead, if no possible choice loop on self | |
| if (!stack[next_index].choices.size()) | |
| goto pick_next; | |
| } | |
| index++; | |
| } | |
| else { | |
| index--; | |
| } | |
| } | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printSudoku(state); | |
| #endif | |
| return { state, stack, index }; | |
| } | |
| std::tuple<puzzle_type, stack_type, size_t> randomsolver(puzzle_type state, stack_type stack = {}) | |
| { | |
| tag_type row_tags; | |
| tag_type col_tags; | |
| tag_type blk_tags; | |
| vector<uint32_t> filled; | |
| vector<int> taken; | |
| //reserve enough space for how many steps we may at most take* | |
| taken.reserve(state.size() * state.size()); | |
| filled.reserve(state.size() * state.size()); | |
| stack.reserve(state.size() * state.size()); | |
| row_tags.reserve(state.size()); | |
| col_tags.reserve(state.size()); | |
| blk_tags.reserve(state.size()); | |
| size_t w_sq = std::sqrt(state.size()); | |
| for (size_t i = 0; i < state.size(); i++) { | |
| filled.push_back(i + 1); | |
| //to keep things simple we count how many of a # at a particular spot there are | |
| //so we reserve an extra spot to ignore 0* | |
| row_tags.push_back(set_type(state.size() + 1, 0)); | |
| col_tags.push_back(set_type(state.size() + 1, 0)); | |
| blk_tags.push_back(set_type(state.size() + 1, 0)); | |
| } | |
| auto simplify = [&stack, &state, &row_tags, &col_tags, &blk_tags, &taken](int idx) { | |
| basic_random_simplify(idx, stack, row_tags, col_tags, blk_tags, 1); | |
| space_simplify(idx, stack, state, row_tags, col_tags, taken, 2); | |
| set_simplify(idx, stack, state, row_tags, col_tags, blk_tags, 3); | |
| //std::make_heap(stack.begin() + idx, stack.end(), heap_ordering); | |
| auto it = std::min_element(stack.begin() + idx, stack.end(), stack_ordering); | |
| size_t d = std::distance(stack.begin(), it); | |
| std::swap(stack[idx], stack[d]); | |
| }; | |
| //are we already solved? | |
| int index; | |
| if (stack.size() && puzzle_validator(state) && stack_validator(state, stack)) { | |
| sudoku_choice& picked = stack.back(); | |
| //clear a previously made choice | |
| int previous = state[picked.y][picked.x]; | |
| row_tags[picked.y][previous]--; | |
| col_tags[picked.x][previous]--; | |
| blk_tags[picked.b][previous]--; | |
| state[picked.y][picked.x] = 0; | |
| index = stack.size() - 2; | |
| filled.clear(); | |
| } | |
| else { | |
| stack.clear(); | |
| for (size_t y = 0; y < state.size(); y++) { | |
| for (size_t x = 0; x < state.size(); x++) { | |
| const int v = state[y][x]; | |
| const int b = get_block(x, y, w_sq); | |
| if (!within_range(v, state.size())) { | |
| stack.push_back({ (uint8_t)x, | |
| (uint8_t)y, | |
| (uint8_t)b, (uint8_t)0, 0, }); | |
| } | |
| else { | |
| row_tags[y][v]++; | |
| col_tags[x][v]++; | |
| blk_tags[b][v]++; | |
| } | |
| } | |
| } | |
| filled.clear(); | |
| simplify(0); | |
| index = 0; | |
| } | |
| //do solve | |
| const int size = stack.size(); | |
| for (; index >= 0 && index < size;) { | |
| sudoku_choice& picked = stack[index]; | |
| //clear a previously made choice | |
| pick_next: | |
| int prev = state[picked.y][picked.x]; | |
| if (prev > 0) { | |
| row_tags[picked.y][prev]--; | |
| col_tags[picked.x][prev]--; | |
| blk_tags[picked.b][prev]--; | |
| state[picked.y][picked.x] = 0; | |
| } | |
| if (picked.c < picked.choices.size()) { | |
| //make choice | |
| int next = picked.choices[picked.c]; | |
| int next_index = index + 1; | |
| row_tags[picked.y][next]++; | |
| col_tags[picked.x][next]++; | |
| blk_tags[picked.b][next]++; | |
| state[picked.y][picked.x] = next; | |
| picked.c++; | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printChangedSudoku(state, picked.y, picked.x); | |
| #endif | |
| //simplify next | |
| if (next_index < size) { | |
| simplify(next_index); | |
| //peek ahead, if no possible choice loop on self | |
| if (!stack[next_index].choices.size()) | |
| goto pick_next; | |
| } | |
| index++; | |
| } | |
| else { | |
| index--; | |
| } | |
| } | |
| #if VISUALIZE_SUDOKU_SOLVE | |
| printSudoku(state); | |
| #endif | |
| return { state, stack, index }; | |
| } | |
| bool simplification_satisfies(const puzzle_type& state) { | |
| auto equal_puzzle = [](solution_type& lhs, solution_type& rhs) { | |
| return std::equal(std::get<0>(lhs).begin(), std::get<0>(lhs).end(), std::get<0>(rhs).begin()); | |
| }; | |
| auto simple_validator = [](solution_type& lhs) { | |
| return std::get<1>(lhs).size() == std::get<2>(lhs); | |
| }; | |
| solution_type l_node = solver(state); | |
| if (simple_validator(l_node)) { | |
| solution_type r_node = rsolver(state); | |
| if (!equal_puzzle(l_node, r_node)) | |
| return true; | |
| return stack_has_no_options(std::get<1>(l_node)) | |
| && stack_has_no_options(std::get<1>(r_node)); | |
| } | |
| return true; | |
| } | |
| std::vector<puzzle_type> multi_solver(const puzzle_type &state) { | |
| /* | |
| auto equal_puzzle = [](solution_type& lhs, solution_type& rhs) { | |
| return std::equal(std::get<0>(lhs).begin(), std::get<0>(lhs).end(), std::get<0>(rhs).begin()); | |
| }; | |
| */ | |
| auto simple_validator = [](solution_type& lhs) { | |
| return std::get<1>(lhs).size() == std::get<2>(lhs); | |
| }; | |
| solution_type l_node = solver(state, {}); | |
| //solution_type r_node = rsolver(state, {}); | |
| std::vector<puzzle_type> ret; | |
| while (simple_validator(l_node)) { | |
| ret.push_back(std::get<0>(l_node)); | |
| /* | |
| if (equal_puzzle(l_node, r_node)) | |
| return ret; | |
| */ | |
| l_node = solver(std::get<0>(l_node), std::get<1>(l_node)); | |
| } | |
| return ret; | |
| } | |
| std::vector<puzzle_type> multi_solver_tree(const puzzle_type& state) { | |
| auto equal_puzzle = [](solution_type& lhs, solution_type& rhs) { | |
| return std::equal(std::get<0>(lhs).begin(), std::get<0>(lhs).end(), std::get<0>(rhs).begin()); | |
| }; | |
| auto simple_validator = [](solution_type& lhs) { | |
| return std::get<1>(lhs).size() == std::get<2>(lhs); | |
| }; | |
| solution_type l_node = solver(state, {}); | |
| solution_type r_node = rsolver(state, {}); | |
| std::vector<puzzle_type> ret; | |
| if (simple_validator(l_node)) { | |
| if (equal_puzzle(l_node, r_node)) { | |
| ret.push_back(std::get<0>(l_node)); | |
| return ret; | |
| } | |
| } | |
| auto walk_tree = [&ret](solution_type& lhs, solution_type& rhs, int offset) { | |
| stack_type& lhs_s = std::get<1>(lhs); | |
| puzzle_type& lhs_p = std::get<0>(lhs); | |
| stack_type& rhs_s = std::get<1>(rhs); | |
| puzzle_type& rhs_p = std::get<0>(rhs); | |
| while (lhs_s.size() && lhs_s.back().choices.size() <= 1) { | |
| lhs_p[lhs_s.back().y][lhs_s.back().x] = 0; | |
| lhs_s.pop_back(); | |
| } | |
| while (rhs_s.size() && rhs_s.back().choices.size() <= 1) { | |
| rhs_p[rhs_s.back().y][rhs_s.back().x] = 0; | |
| rhs_s.pop_back(); | |
| } | |
| //c.c = 2 (2 is next going right) | |
| //[0, 1, 2, 3] | |
| bool found = false; | |
| if (lhs_s.size() && lhs_s.back().choices.size() > offset) { | |
| int count; | |
| if (offset) | |
| count = lhs_s.back().choices.size() - lhs_s.back().c; | |
| else | |
| count = lhs_s.back().choices.size(); | |
| for (int i = 0; i < count; i++) { | |
| lhs_p[lhs_s.back().y][lhs_s.back().x] = lhs_s.back().choices.back(); | |
| solution_type n_lhs = solver(lhs_p, {}); | |
| if (std::get<1>(n_lhs).size() == std::get<2>(n_lhs)) { | |
| found = true; | |
| if (!ret.size()) | |
| ret.push_back(std::get<0>(n_lhs)); | |
| else if (!std::equal(std::get<0>(n_lhs).begin(), std::get<0>(n_lhs).end(), ret.back().begin())) | |
| ret.push_back(std::get<0>(n_lhs)); | |
| else | |
| return false; | |
| } | |
| lhs_s.back().choices.pop_back(); | |
| } | |
| } | |
| //c.c = 2 (1 is next going left) | |
| //[0, 1, 2, 3] | |
| //c.c = 0 | |
| //[0, 1] | |
| if (rhs_s.size() && rhs_s.back().choices.size() > offset) { | |
| int count; | |
| if (offset) | |
| count = (rhs_s.back().c + 1); | |
| else | |
| count = rhs_s.back().choices.size(); | |
| for (int i = 0; i < count; i++) { | |
| rhs_p[rhs_s.back().y][rhs_s.back().x] = rhs_s.back().choices.front(); | |
| solution_type n_rhs = solver(rhs_p, {}); | |
| if (std::get<1>(n_rhs).size() == std::get<2>(n_rhs)) { | |
| found = true; | |
| if (!ret.size()) | |
| ret.push_back(std::get<0>(n_rhs)); | |
| else if (!std::equal(std::get<0>(n_rhs).begin(), std::get<0>(n_rhs).end(), ret.back().begin())) | |
| ret.push_back(std::get<0>(n_rhs)); | |
| else | |
| return false; | |
| } | |
| rhs_s.back().choices.erase(rhs_s.back().choices.begin()); | |
| } | |
| } | |
| return found; | |
| }; | |
| while (walk_tree(l_node, r_node, 1)); | |
| return ret; | |
| } | |
| std::tuple<std::vector<puzzle_type>, size_t> threaded_solver(const puzzle_type &state) { | |
| auto equal_puzzle = [](solution_type& lhs, solution_type& rhs) { | |
| return std::equal(std::get<0>(lhs).begin(), std::get<0>(lhs).end(), std::get<0>(rhs).begin()); | |
| }; | |
| auto simple_validator = [](solution_type& lhs) { | |
| return std::get<1>(lhs).size() == std::get<2>(lhs); | |
| }; | |
| auto new_stack_choices = [](solution_type& lhs) { | |
| uint64_t ret = 1; | |
| stack_type& stack = std::get<1>(lhs); | |
| for (sudoku_choice c : stack) { | |
| ret *= c.choices.size() - (c.c - 1); | |
| if (ret > 2048) | |
| return ret; | |
| } | |
| return ret; | |
| }; | |
| solution_type l_node = solver(state, {}); | |
| solution_type r_node = rsolver(state, {}); | |
| std::vector<puzzle_type> ret; | |
| size_t extra = 0; | |
| size_t branches = new_stack_choices(l_node); | |
| ret.reserve(branches); | |
| vector<future<vector<puzzle_type>>> futures; | |
| uint32_t max_threads = std::thread::hardware_concurrency(); | |
| if (max_threads > 2) | |
| max_threads = max_threads - 2; | |
| futures.reserve(max_threads); | |
| if (simple_validator(l_node)) { | |
| if (equal_puzzle(l_node, r_node)) { | |
| ret.push_back(std::get<0>(l_node)); | |
| return { ret, extra }; | |
| } | |
| puzzle_type current = std::get<0>(l_node); | |
| stack_type stack = std::get<1>(l_node); | |
| size_t current_idx = 0; | |
| auto work = [&futures, &max_threads, ¤t, &ret, ¤t_idx, &extra]() { | |
| if (futures.size() >= max_threads) { | |
| vector<puzzle_type> c = futures[current_idx].get(); | |
| futures[current_idx] = std::async(multi_solver, current); | |
| if (ret.size() + c.size() < ret.capacity()) | |
| ret.insert(ret.end(), c.begin(), c.end()); | |
| else | |
| extra += c.size(); | |
| current_idx = (current_idx + 1) % futures.size(); | |
| } | |
| else { | |
| futures.push_back(std::async(multi_solver, current)); | |
| } | |
| }; | |
| //backup the stack to the first major choice | |
| while (stack.size() && stack.back().choices.size() <= 1) { | |
| sudoku_choice& pick = stack.back(); | |
| current[pick.y][pick.x] = 0; | |
| stack.pop_back(); | |
| } | |
| //c.c = 2 (c.c is left at the "next" index) | |
| //[0, 1, 2, 3] | |
| //eg we solved by using 1 | |
| //make sure to include this point w/ <= in the multi solver | |
| //stack.back().choices.size() > 1 | |
| while (stack.size() && stack.back().choices.size() > 0 && (stack.back().c-1) <= stack.back().choices.size()) { | |
| sudoku_choice& pick = stack.back(); | |
| current[pick.y][pick.x] = pick.choices.back(); | |
| pick.choices.pop_back(); | |
| work(); | |
| } | |
| do { | |
| while (stack.size() && stack.back().choices.size() <= 1) { | |
| sudoku_choice& pick = stack.back(); | |
| current[pick.y][pick.x] = 0; | |
| stack.pop_back(); | |
| } | |
| //don't include self in solver | |
| while (stack.size() && stack.back().choices.size() > 1 && stack.back().c <= stack.back().choices.size()) { | |
| sudoku_choice& pick = stack.back(); | |
| current[pick.y][pick.x] = pick.choices.back(); | |
| pick.choices.pop_back(); | |
| work(); | |
| } | |
| } while (stack.size()); | |
| for (size_t i = current_idx; i < futures.size(); i++) { | |
| vector<puzzle_type> c = futures[i].get(); | |
| if (ret.size() + c.size() < ret.capacity()) | |
| ret.insert(ret.end(), c.begin(), c.end()); | |
| else | |
| extra += c.size(); | |
| } | |
| for (size_t i = 0; i < current_idx; i++) { | |
| vector<puzzle_type> c = futures[i].get(); | |
| if (ret.size() + c.size() < ret.capacity()) | |
| ret.insert(ret.end(), c.begin(), c.end()); | |
| else | |
| extra += c.size(); | |
| } | |
| futures.clear(); | |
| } | |
| return { ret, extra }; | |
| } | |
| vector<vector<int>> make_puzzle(vector<int>& puzzle) { | |
| int sq = std::sqrt(puzzle.size()); | |
| vector<vector<int>> ret; | |
| ret.reserve(sq); | |
| for (int y = 0; y < sq; y++) { | |
| ret.push_back({}); | |
| ret[y].reserve(sq); | |
| for (int x = 0; x < sq; x++) { | |
| ret[y].push_back(puzzle[y * sq + x]); | |
| } | |
| } | |
| return ret; | |
| } | |
| vector<vector<int>> flat_puzzle(const string_view s) { | |
| vector<int> flat; | |
| flat.reserve(s.size()); | |
| int sq = std::sqrt(s.size()); | |
| auto custom_clamp = [&sq](int v) { | |
| return (v > sq || v < 0) ? 0 : v; | |
| }; | |
| for (size_t i = 0; i < s.size(); i++) { | |
| int v = s[i] - '0'; | |
| flat.push_back(custom_clamp(v)); | |
| } | |
| return make_puzzle(flat); | |
| } | |
| #ifdef tmax | |
| #define max tmax | |
| #undef tmax | |
| #endif | |
| int main(void) | |
| { | |
| vector<vector<int>> empty(9, vector<int>(9, 0)); | |
| //generate 10000 well formed sudoku puzzles | |
| for (int i = 0; i < 10000; i++) { | |
| std::tuple<puzzle_type, stack_type, size_t> result = randomsolver(empty); | |
| printSudoku(std::get<0>(result)); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment