sudoku_gem 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,171 @@
1
+ /*
2
+ * Sudoku Base - a library for solving Sudoku puzzles
3
+ * Copyright (C) 2013 Neal Patel <nealp9084@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ #ifndef VALIDATOR_H
20
+ #define VALIDATOR_H
21
+
22
+ #include <cstdint>
23
+ #include <cstddef>
24
+
25
+ #include "grid.h"
26
+
27
+ class Validator
28
+ {
29
+ public:
30
+ /**
31
+ * @brief Tells you whether a Sudoku puzzle has been solved.
32
+ *
33
+ * A board is defined as "good" if every row has the digits 1-n used exactly once, if every column
34
+ * has the digits 1-n used exactly once, and if every 3x3 block has the digits 1-n used exactly
35
+ * once. Basically, this function is just checking whether the given board is a solution to some
36
+ * Sudoku puzzle.
37
+ *
38
+ * @param cur_board A Sudoku puzzle board.
39
+ * @return bool Whether that puzzle has been solved.
40
+ **/
41
+ static bool is_good_board(Grid const& cur_grid);
42
+ private:
43
+ /**
44
+ * @brief Helper function for the above task.
45
+ *
46
+ * @param cur_board A Sudoku puzzle board.
47
+ * @param y The index for a particular row.
48
+ * @return bool Whether the 1*n row contains no repeated or undetermined elements.
49
+ **/
50
+ static bool is_good_row(Grid const& cur_grid, std::size_t y);
51
+ /**
52
+ * @brief Helper function for the above task.
53
+ *
54
+ * @param cur_board A Sudoku puzzle board.
55
+ * @param x The index for a particular column.
56
+ * @return bool Whether the n*1 column contains no repeated or undetermined elements.
57
+ **/
58
+ static bool is_good_column(Grid const& cur_grid, std::size_t x);
59
+ /**
60
+ * @brief Helper function for the above task.
61
+ *
62
+ * @param cur_board A Sudoku puzzle board.
63
+ * @param x The index for a particular starting row. Must be a multiple of 3.
64
+ * @param y The index for a particular starting column. Must be a multiple of 3.
65
+ * @return bool Whether the sqrt(n)*sqrt(n) block contains no repeated or undetermined elements.
66
+ **/
67
+ static bool is_good_block(Grid const& cur_grid, std::size_t x, std::size_t y);
68
+
69
+ public:
70
+ /**
71
+ * @brief Tells you whether you can color a certain node in a certain way (i.e., is it okay to use
72
+ * a particular number in this Sudoku cell?)
73
+ *
74
+ * This validation will tell you whether you can use a specific color, provided that it does not
75
+ * appear in the same row, in the same column, or the same 3x3 block.
76
+ *
77
+ * @param cur_board A Sudoku puzzle board.
78
+ * @param x The x position of the cell.
79
+ * @param y The y position of the cell.
80
+ * @param i The color you want to use (i.e., the number to assign the cell).
81
+ * @return bool Whether the node can be colored that way.
82
+ **/
83
+ static bool is_good_color(Grid const& cur_grid, std::size_t x, std::size_t y, int i);
84
+
85
+ /**
86
+ * @brief Tells you which colors a certain node may use (i.e., which numbers can I put in this
87
+ * Sudoku cell?).
88
+ *
89
+ * This validation will tell you whether you can use a specific color, provided that it does not
90
+ * appear in the same row, in the same column, or the same 3x3 block. The result is encoded into a
91
+ * 64-bit unsigned integer, with the least significant bit corresponding to whether you can use
92
+ * the color 1, the next bit corresponding to whether you can use the number 2, and so on.
93
+ *
94
+ * @param cur_board A Sudoku puzzle board.
95
+ * @param x The x position of the cell.
96
+ * @param y The y position of the cell.
97
+ * @return uint_fast64_t The various colors (numbers) you may use, encoded using the above scheme.
98
+ **/
99
+ static std::uint_fast64_t good_colors(Grid const& cur_grid, std::size_t x, std::size_t y);
100
+ private:
101
+ /**
102
+ * @brief Helper function for the above task. Tells you which colors have been used.
103
+ *
104
+ * @param cur_board A Sudoku puzzle board.
105
+ * @param y The index for a particular row.
106
+ * @return int Which colors are used by that particular 1*n row.
107
+ **/
108
+ static std::uint_fast64_t row_colors(Grid const& cur_grid, std::size_t y);
109
+ /**
110
+ * @brief Helper function for the above task. Tells you which colors have been used.
111
+ *
112
+ * @param cur_board A Sudoku puzzle board.
113
+ * @param x The index for a particular column.
114
+ * @return int Which colors are used by that particular n*1 column.
115
+ **/
116
+ static std::uint_fast64_t column_colors(Grid const& cur_grid, std::size_t x);
117
+ /**
118
+ * @brief Helper function for the above task. Tells you which colors have been used.
119
+ *
120
+ * @param cur_board A Sudoku puzzle board.
121
+ * @param x The index for a particular starting row. Must be a multiple of 3.
122
+ * @param y The index for a particular starting column. Must be a multiple of 3.
123
+ * @return int Which colors are used by that particular sqrt(n)*sqrt(n) block.
124
+ **/
125
+ static std::uint_fast64_t block_colors(Grid const& cur_grid, std::size_t x, std::size_t y);
126
+
127
+ public:
128
+ /**
129
+ * @brief Tells you if a given Sudoku board has a solution (if it has no repeats, then it does).
130
+ *
131
+ * A partially-completed board is defined as "good" if every row has the digits 1-n used at most
132
+ * once, if every column has the digits 1-n used at most once, and if every 3x3 block has the digits
133
+ * 1-n used at most once. Basically, this function is just checking whether the given board is a
134
+ * solvable Sudoku puzzle.
135
+ *
136
+ * This function can be used when solving a Sudoku puzzle with the graph n-colorability method, and
137
+ * many other solution methods.
138
+ *
139
+ * @param cur_board A Sudoku puzzle board.
140
+ * @return bool Whether the given board has any solutions.
141
+ **/
142
+ static bool is_good_partial_board(Grid const& cur_grid);
143
+ private:
144
+ /**
145
+ * @brief Helper function for the above task.
146
+ *
147
+ * @param cur_board A Sudoku puzzle board.
148
+ * @param y The index for a particular row.
149
+ * @return bool Whether the given 1*n row has any repeated elements.
150
+ **/
151
+ static bool is_good_partial_row(Grid const& cur_grid, std::size_t y);
152
+ /**
153
+ * @brief Helper function for the above task.
154
+ *
155
+ * @param cur_board A Sudoku puzzle board.
156
+ * @param x The index for a particular column.
157
+ * @return bool Whether the given n*1 column has any repeated elements.
158
+ **/
159
+ static bool is_good_partial_column(Grid const& cur_grid, std::size_t x);
160
+ /**
161
+ * @brief Helper function for the above task.
162
+ *
163
+ * @param cur_board A Sudoku puzzle board.
164
+ * @param x The index for a particular starting row. Must be a multiple of 3.
165
+ * @param y The index for a particular starting column. Must be a multiple of 3.
166
+ * @return bool Whether the given sqrt(n)*sqrt(n) block has any repeated elements.
167
+ **/
168
+ static bool is_good_partial_block(Grid const& cur_grid, std::size_t x, std::size_t y);
169
+ };
170
+
171
+ #endif // VALIDATOR_H
data/lib/sudoku_gem.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'sudoku_gem/sudoku_gem'
2
+
3
+ class SudokuGem
4
+ def self.solution_for(puzzle)
5
+ text = self.solve(puzzle)
6
+
7
+ if text
8
+ return text.split("\n").map { |x| x.split(' ').map{ |y| y.to_i } }
9
+ else
10
+ return nil
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sudoku_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Neal Patel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This ruby gem is an interface to a fast C++ sudoku solver library
14
+ email: nap7jz@virginia.edu
15
+ executables: []
16
+ extensions:
17
+ - ext/sudoku_gem/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sudoku_gem.rb
21
+ - ext/sudoku_gem/sudoku_gem.c
22
+ - ext/sudoku_gem/grid.h
23
+ - ext/sudoku_gem/validator.h
24
+ - ext/sudoku_gem/sudoku.h
25
+ - ext/sudoku_gem/main.cpp
26
+ - ext/sudoku_gem/sudoku.cpp
27
+ - ext/sudoku_gem/grid.cpp
28
+ - ext/sudoku_gem/validator.cpp
29
+ - ext/sudoku_gem/extconf.rb
30
+ homepage: https://www.epicdomain.name
31
+ licenses:
32
+ - GPL-3
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Ruby interface to a C++ sudoku solver
54
+ test_files: []