guava_sudoku_solver 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: daa34677a0e601753f2922ec82d28d7355a46b49
4
+ data.tar.gz: ea660522a2d3a427ce624a8cf90792ccf165bc2c
5
+ SHA512:
6
+ metadata.gz: e646c9fd758f17419691c2ffd0e9e20ac876760c1607533456c8a8bc662ff6afd3cecc843fa1dde19e81a184bfdfbd82e656d6d939d5a06fcdb3364b9fc99ea2
7
+ data.tar.gz: cb44525638956ae2a9b2eb89be340a8c5462cdd7e17cc01a02a15640bd8f3157c511b32eb669c1951ba2d2381a6ced70ea86c21f98127793325bea90173d43aa
data/.gitignore ADDED
@@ -0,0 +1,64 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
16
+ *.rbc
17
+ /.config
18
+ /coverage/
19
+ /InstalledFiles
20
+ /pkg/
21
+ /spec/reports/
22
+ /spec/examples.txt
23
+ /test/tmp/
24
+ /test/version_tmp/
25
+ /tmp/
26
+
27
+ # Used by dotenv library to load environment variables.
28
+ # .env
29
+
30
+ ## Specific to RubyMotion:
31
+ .dat*
32
+ .repl_history
33
+ build/
34
+ *.bridgesupport
35
+ build-iPhoneOS/
36
+ build-iPhoneSimulator/
37
+
38
+ ## Specific to RubyMotion (use of CocoaPods):
39
+ #
40
+ # We recommend against adding the Pods directory to your .gitignore. However
41
+ # you should judge for yourself, the pros and cons are mentioned at:
42
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
43
+ #
44
+ # vendor/Pods/
45
+
46
+ ## Documentation cache and generated files:
47
+ /.yardoc/
48
+ /_yardoc/
49
+ /doc/
50
+ /rdoc/
51
+
52
+ ## Environment normalization:
53
+ /.bundle/
54
+ /vendor/bundle
55
+ /lib/bundler/man/
56
+
57
+ # for a library or gem, you might want to ignore these files since the code is
58
+ # intended to run in multiple environments; otherwise, check them in:
59
+ # Gemfile.lock
60
+ # .ruby-version
61
+ # .ruby-gemset
62
+
63
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
64
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sudoku_solver.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Vu Tran
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Victor Tran
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SudokuSolver
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sudoku_solver'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sudoku_solver
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/sudoku_solver/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/**/*.rb"
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
11
+
@@ -0,0 +1,40 @@
1
+ require "sudoku_solver/version"
2
+ require "sudoku_solver/brute_force/bruteforcable"
3
+ require "sudoku_solver/table"
4
+ require "sudoku_solver/variable"
5
+ require "sudoku_solver/brute_force_corpus"
6
+ require "sudoku_solver/brute_force_solver"
7
+
8
+ # SudokuSolver provides convenient API to solve a sudoku puzzle:
9
+ # `SudokuSolver.solve(rows)`
10
+ #
11
+ # Parameter:
12
+ # `rows` should be 2D array like object which can be accessed via `each` to get
13
+ # each row and `each` on the row to get individual sudoku elements.
14
+ #
15
+ # Each element should has value from 1 to 9 except elements that need to figure
16
+ # out its value should be `nil` in order to be recognized by SudokuSolver.
17
+ #
18
+ # Example:
19
+ #
20
+ # ```ruby
21
+ # require 'sudoku_solver'
22
+ #
23
+ # rows = []
24
+ # (0..8).each
25
+ # rows << gets.split(' ').map do |element|
26
+ # element.to_i == 0 ? nil : element.to_i
27
+ # end
28
+ # end
29
+ #
30
+ # solved_sudoku = SudokuSolver.solve(rows)
31
+ #
32
+ # solved_sudoku.each { |row| puts row.join(' ') }
33
+ # ```
34
+ #
35
+ module SudokuSolver
36
+ def SudokuSolver.solve(rows)
37
+ final_table = BruteForceSolver.new(rows).solve
38
+ final_table.to_a
39
+ end
40
+ end
@@ -0,0 +1,72 @@
1
+ # Bruteforcable module provides conventional brute-force search mechanism
2
+ # for any classes, objects that can handle message `build_brute_force_corpus`.
3
+ #
4
+ # By including Bruteforcable, the class is responsible for calling
5
+ # `setup_for_brute_force(corpus, initial_state)` before issuing brute force
6
+ # process by calling `brute_force`.
7
+ #
8
+ # Brute force basically try to assign available value to every variables and apply
9
+ # to the state to see if it's a stop state.
10
+ #
11
+ # The process stops as soon as it reach a stop state.
12
+ #
13
+ # *Duck typing requirements:*
14
+ #
15
+ # `corpus` is supposed to handle these messages:
16
+ # 1. `no_variables` which usually means number of variables
17
+ # 2. `available_values(index)` which returns a set of available values for
18
+ # variable number index
19
+ # 3. `variable_at(index)` returns variable instance number index
20
+ # 4. `sort_variable(&comp_func)` (optional) sort all variables as compare
21
+ # function passed as a block
22
+ # 5. `acquire(variable, value)`, strategy to handle value before assigning
23
+ # it to the variable
24
+ # 6. `release(variable, value)`, strategy to handle value before assigning
25
+ # the variable to another value
26
+ #
27
+ # `initial_state` is supposed to handle these messages:
28
+ # 1. `final_state?` which returns true if it's a stop state, false otherwise
29
+ # 2. `apply(variable, value)` which apply a variable with specified value
30
+ # to itself
31
+ #
32
+ module Bruteforcable
33
+ attr_accessor :corpus
34
+ attr_accessor :state
35
+
36
+ def brute_force
37
+ if self.corpus.no_variables > 0
38
+ try(corpus, 0, self.state)
39
+ end
40
+ return (self.state.final_state? ? self.state : nil)
41
+ end
42
+
43
+ def try(corpus, index, state)
44
+ variable = corpus.variable_at(index)
45
+ corpus.available_values(variable).each do |value|
46
+ corpus.acquire(variable, value)
47
+ state.apply(corpus.variable_at(index), value)
48
+ if index == corpus.no_variables - 1
49
+ return true if state.final_state?
50
+ else
51
+ return true if try(corpus, index + 1, state)
52
+ end
53
+ corpus.release(variable, value)
54
+ end
55
+ return false
56
+ end
57
+
58
+ def setup_for_brute_force(corpus, initial_state)
59
+ self.corpus = corpus
60
+ self.state = initial_state
61
+ end
62
+
63
+ def sort_variables(&comp_func)
64
+ if comp_func
65
+ self.corpus.sort_variables do |a, b|
66
+ comp_func.call(a, b)
67
+ end
68
+ else
69
+ self.curpus.sort_variables
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,55 @@
1
+ require 'set'
2
+
3
+ module SudokuSolver
4
+ class BruteForceCorpus
5
+
6
+ def initialize
7
+ @variables = []
8
+ @available_in_row = Array.new(9) { Set.new 1..9 }
9
+ @available_in_col = Array.new(9) { Set.new 1..9 }
10
+ @available_in_sq = Array.new(3) do
11
+ Array.new(3) { Set.new 1..9 }
12
+ end
13
+ end
14
+
15
+ def no_variables
16
+ @variables.size
17
+ end
18
+
19
+ def add_variable(row, col)
20
+ @variables << Variable.new(row, col, self)
21
+ end
22
+
23
+ def variable_at(index)
24
+ @variables[index]
25
+ end
26
+
27
+ def acquire(variable, value)
28
+ @available_in_row[variable.row].delete(value)
29
+ @available_in_col[variable.col].delete(value)
30
+ @available_in_sq[variable.row/3][variable.col/3].delete(value)
31
+ end
32
+
33
+ def release(variable, value)
34
+ @available_in_row[variable.row].add(value)
35
+ @available_in_col[variable.col].add(value)
36
+ @available_in_sq[variable.row/3][variable.col/3].add(value)
37
+ end
38
+
39
+ def available_values(variable)
40
+ @available_in_row[variable.row] &
41
+ @available_in_col[variable.col] &
42
+ @available_in_sq[variable.row/3][variable.col/3]
43
+ end
44
+
45
+ def sort_variables(&comp_func)
46
+ if comp_func
47
+ @variables.sort! do |a, b|
48
+ comp_func.call(a, b)
49
+ end
50
+ else
51
+ @variables.sort!
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,18 @@
1
+ module SudokuSolver
2
+ class BruteForceSolver
3
+ include Bruteforcable
4
+
5
+ def initialize(rows)
6
+ @rows = rows
7
+ end
8
+
9
+ def solve
10
+ table = Table.new(@rows)
11
+ setup_for_brute_force(table.build_brute_force_corpus, table)
12
+ sort_variables do |a, b|
13
+ a.available_values_size <=> b.available_values_size
14
+ end
15
+ brute_force
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,78 @@
1
+ module SudokuSolver
2
+ class Table
3
+ def initialize(rows)
4
+ @data = Array.new(9) { Array.new(9, nil) }
5
+ rows.each_with_index do |row, row_index|
6
+ row.each_with_index do |element, col_index|
7
+ @data[row_index][col_index] = element
8
+ end
9
+ end
10
+ end
11
+
12
+ def build_brute_force_corpus
13
+ corpus = BruteForceCorpus.new
14
+ (0..8).each do |row|
15
+ (0..8).each do |col|
16
+ if @data[row][col] == nil
17
+ corpus.add_variable(row, col)
18
+ else
19
+ corpus.acquire(Variable.new(row, col, corpus), @data[row][col])
20
+ end
21
+ end
22
+ end
23
+ corpus
24
+ end
25
+
26
+ def apply(variable, value)
27
+ @data[variable.row][variable.col] = value
28
+ end
29
+
30
+ def final_state?
31
+ # check if every row is valid
32
+ (0..8).each do |row|
33
+ elements = []
34
+ (0..8).each do |col|
35
+ elements << @data[row][col]
36
+ end
37
+ return false unless valid?(elements)
38
+ end
39
+
40
+ # check if every column is valid
41
+ (0..8).each do |col|
42
+ elements = []
43
+ (0..8).each do |row|
44
+ elements << @data[row][col]
45
+ end
46
+ return false unless valid?(elements)
47
+ end
48
+
49
+ # check if every square is valid
50
+ (0..2).each do |sq_row|
51
+ (0..2).each do |sq_col|
52
+ elements = []
53
+ (0..2).each do |row|
54
+ (0..2).each do |col|
55
+ elements << @data[sq_row * 3 + row][sq_col * 3 + col]
56
+ end
57
+ end
58
+ return false unless valid?(elements)
59
+ end
60
+ end
61
+ return true
62
+ end
63
+
64
+ def to_a
65
+ @data
66
+ end
67
+
68
+ private
69
+ def valid?(elements)
70
+ masks = Array.new(9, nil)
71
+ elements.each do |element|
72
+ return false if element == nil || masks[element - 1] != nil
73
+ masks[element - 1] = 1
74
+ end
75
+ return true
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,15 @@
1
+ module SudokuSolver
2
+ class Variable
3
+ attr_accessor :row, :col, :corpus
4
+
5
+ def initialize(row, col, corpus)
6
+ self.row = row
7
+ self.col = col
8
+ self.corpus = corpus
9
+ end
10
+
11
+ def available_values_size
12
+ corpus.available_values(self).size
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SudokuSolver
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sudoku_solver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "guava_sudoku_solver"
8
+ spec.version = SudokuSolver::VERSION
9
+ spec.authors = ["victor"]
10
+ spec.email = ["vu.tran54@gmail.com"]
11
+ spec.summary = %q{Solver for sudoku 9x9}
12
+ spec.description = %q{Provide convenient way to solve a sudoku challenge}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 10.0"
24
+ end
@@ -0,0 +1,67 @@
1
+ require 'minitest/autorun'
2
+ require 'sudoku_solver'
3
+
4
+ # We are going to demonstrate using bruteforcable to solve an equation
5
+ # of: x^3 + y^3 = 2000
6
+ # where x, y are positive integer
7
+ class DumpCorpus
8
+ def no_variables
9
+ 2
10
+ end
11
+
12
+ def available_values(index)
13
+ 1..10
14
+ end
15
+
16
+ def acquire(variable, value)
17
+ end
18
+
19
+ def release(variable, value)
20
+ end
21
+
22
+ def variable_at(index)
23
+ if index == 0
24
+ "x"
25
+ elsif index == 1
26
+ "y"
27
+ end
28
+ end
29
+ end
30
+
31
+ class DumpEquationState
32
+ attr_accessor :x, :y
33
+
34
+ def final_state?
35
+ self.x ** 3 + self.y ** 3 == 2000
36
+ end
37
+
38
+ def apply(variable, value)
39
+ if variable == "x"
40
+ self.x = value
41
+ elsif variable == "y"
42
+ self.y = value
43
+ end
44
+ end
45
+ end
46
+
47
+ class EquationSolver
48
+ include Bruteforcable
49
+
50
+ def initialize
51
+ corpus = DumpCorpus.new
52
+ initial_state = DumpEquationState.new
53
+ setup_for_brute_force(corpus, initial_state)
54
+ end
55
+
56
+ def solve
57
+ brute_force
58
+ end
59
+ end
60
+
61
+ class BruteforcableTest < Minitest::Test
62
+ def test_bruteforcable_provide_a_brute_force_process_to_any_problem
63
+ solver = EquationSolver.new
64
+ result = solver.solve
65
+ assert(result.x == 10 && result.y == 10)
66
+ end
67
+ end
@@ -0,0 +1,87 @@
1
+ require 'minitest/autorun'
2
+ require 'sudoku_solver'
3
+
4
+ class BruteForceCorpusTest < Minitest::Test
5
+ def test_no_variables_is_0_at_initialization
6
+ corpus = SudokuSolver::BruteForceCorpus.new
7
+ assert(corpus.no_variables == 0)
8
+ end
9
+
10
+ def test_no_variables_is_increased_after_adding_variables
11
+ corpus = SudokuSolver::BruteForceCorpus.new
12
+ corpus.add_variable(1, 1)
13
+ assert(corpus.no_variables == 1)
14
+ end
15
+
16
+ def test_add_variable_adds_variable_instance_to_variable_list
17
+ corpus = SudokuSolver::BruteForceCorpus.new
18
+ corpus.add_variable(1, 1)
19
+ assert(corpus.variable_at(0).is_a?(SudokuSolver::Variable))
20
+ end
21
+
22
+ def test_variable_at_gets_variable_at_specified_index
23
+ corpus = SudokuSolver::BruteForceCorpus.new
24
+ corpus.add_variable(1, 1)
25
+ corpus.add_variable(1, 2)
26
+ corpus.add_variable(3, 4)
27
+ variable = corpus.variable_at(2)
28
+ assert(variable.row == 3 && variable.col == 4)
29
+ end
30
+
31
+ def test_acquire_remove_value_from_row_col_and_square_that_the_variable_belongs_to
32
+ corpus = SudokuSolver::BruteForceCorpus.new
33
+ corpus.acquire(SudokuSolver::Variable.new(1, 1, corpus), 1)
34
+ value_for_row_1 = corpus.available_values(
35
+ SudokuSolver::Variable.new(1, 2, corpus))
36
+ assert(!value_for_row_1.include?(1))
37
+ value_for_col_1 = corpus.available_values(
38
+ SudokuSolver::Variable.new(2, 1, corpus))
39
+ assert(!value_for_col_1.include?(1))
40
+ value_for_sq = corpus.available_values(
41
+ SudokuSolver::Variable.new(2, 2, corpus))
42
+ assert(!value_for_sq.include?(1))
43
+ end
44
+
45
+ def test_release_add_value_to_row_col_and_square_that_the_variable_belongs_to
46
+ corpus = SudokuSolver::BruteForceCorpus.new
47
+ corpus.acquire(SudokuSolver::Variable.new(1, 1, corpus), 1)
48
+ corpus.release(SudokuSolver::Variable.new(1, 1, corpus), 1)
49
+ value_for_row_1 = corpus.available_values(
50
+ SudokuSolver::Variable.new(1, 2, corpus))
51
+ assert(value_for_row_1.include?(1))
52
+ value_for_col_1 = corpus.available_values(
53
+ SudokuSolver::Variable.new(2, 1, corpus))
54
+ assert(value_for_col_1.include?(1))
55
+ value_for_sq = corpus.available_values(
56
+ SudokuSolver::Variable.new(2, 2, corpus))
57
+ assert(value_for_sq.include?(1))
58
+ end
59
+
60
+ def test_available_values_returns_values_that_the_variable_can_be_assigned
61
+ corpus = SudokuSolver::BruteForceCorpus.new
62
+ corpus.acquire(SudokuSolver::Variable.new(1, 1, corpus), 1)
63
+ corpus.acquire(SudokuSolver::Variable.new(1, 2, corpus), 2)
64
+ corpus.acquire(SudokuSolver::Variable.new(1, 3, corpus), 3)
65
+ corpus.acquire(SudokuSolver::Variable.new(1, 4, corpus), 4)
66
+ corpus.acquire(SudokuSolver::Variable.new(1, 5, corpus), 5)
67
+ corpus.acquire(SudokuSolver::Variable.new(1, 6, corpus), 6)
68
+ corpus.acquire(SudokuSolver::Variable.new(1, 7, corpus), 7)
69
+ corpus.acquire(SudokuSolver::Variable.new(1, 8, corpus), 8)
70
+
71
+ value_for_row = corpus.available_values(
72
+ SudokuSolver::Variable.new(1, 0, corpus))
73
+ assert(value_for_row.size == 1)
74
+ assert(value_for_row.to_a[0] == 9)
75
+ end
76
+
77
+ def test_sort_variables_sorts_variable_by_a_block
78
+ corpus = SudokuSolver::BruteForceCorpus.new
79
+ corpus.add_variable(1, 1)
80
+ corpus.add_variable(0, 1)
81
+ corpus.sort_variables do |v1, v2|
82
+ v1.row <=> v2.row
83
+ end
84
+ assert(corpus.variable_at(0).row == 0)
85
+ assert(corpus.variable_at(1).row == 1)
86
+ end
87
+ end
@@ -0,0 +1,110 @@
1
+ require 'minitest/autorun'
2
+
3
+ class SudokuSolverTest < Minitest::Test
4
+ def test_outter_most_solve_method
5
+ input = [
6
+ [6, 2, nil, 8, 1, 3, 7, 5, 4],
7
+ [7, 8, 3, 9, 5, 4, 6, 2, 1],
8
+ [5, 1, 4, 6, 7, 2, 3, 9, 8],
9
+ [1, 7, 5, 2, 3, 8, 9, 4, 6],
10
+ [9, 3, 2, 4, 6, 1, 8, 7, 5],
11
+ [4, 6, 8, 7, 9, 5, 1, 3, 2],
12
+ [3, 9, 1, 5, 4, 6, 2, 8, 7],
13
+ [8, 4, 6, 3, 2, 7, 5, 1, 9],
14
+ [2, 5, 7, nil, 8, 9, 4, 6, 3]
15
+ ]
16
+
17
+ output = SudokuSolver.solve(input)
18
+ assert(SudokuSolver::Table.new(output).final_state?)
19
+ end
20
+
21
+ def test_solver
22
+ input = [
23
+ [6, 2, nil, 8, 1, 3, 7, 5, 4],
24
+ [7, 8, 3, 9, 5, 4, 6, 2, 1],
25
+ [5, 1, 4, 6, 7, 2, 3, 9, 8],
26
+ [1, 7, 5, 2, 3, 8, 9, 4, 6],
27
+ [9, 3, 2, 4, 6, 1, 8, 7, 5],
28
+ [4, 6, 8, 7, 9, 5, 1, 3, 2],
29
+ [3, 9, 1, 5, 4, 6, 2, 8, 7],
30
+ [8, 4, 6, 3, 2, 7, 5, 1, 9],
31
+ [2, 5, 7, nil, 8, 9, 4, 6, 3]
32
+ ]
33
+
34
+ solver = SudokuSolver::BruteForceSolver.new(input)
35
+ output = solver.solve
36
+ assert(output.final_state?, "Failed! The result is not valid")
37
+ end
38
+
39
+ def test_solver_many_slots
40
+ input = [
41
+ [nil, nil, nil, nil, 1, 3, nil, 5, nil],
42
+ [7, nil, nil, 9, 5, nil, 6, nil, nil],
43
+ [5, nil, nil, nil, nil, 2, nil, 9, 8],
44
+ [nil, nil, nil, 2, nil, 8, 9, nil, 6],
45
+ [nil, nil, nil, nil, 6, nil, nil, nil, nil],
46
+ [4, nil, 8, 7, nil, 5, nil, nil, nil],
47
+ [3, 9, nil, 5, nil, nil, nil, nil, 7],
48
+ [nil, nil, 6, nil, 2, 7, nil, nil, 9],
49
+ [nil, 5, nil, 1, 8, nil, nil, nil, nil],
50
+ ]
51
+
52
+ solver = SudokuSolver::BruteForceSolver.new(input)
53
+ output = solver.solve
54
+ assert(output.final_state?, "Failed! The result is not valid")
55
+ end
56
+
57
+ def test_solver_many_slots_2
58
+ input = [
59
+ [9, nil, nil, nil, nil, 7, nil, nil, nil],
60
+ [nil, 6, 2, nil, 3, nil, nil, nil, 7],
61
+ [nil, nil, nil, nil, nil, 8, 2, nil, nil],
62
+ [nil, 2, 5, nil, nil, nil, 4, nil, 1],
63
+ [nil, 7, 6, 3, nil, 5, 9, 2, nil],
64
+ [1, nil, 8, nil, nil, nil, 5, 7, nil],
65
+ [nil, nil, 9, 5, nil, nil, nil, nil, nil],
66
+ [2, nil, nil, nil, 9, nil, 8, 3, nil],
67
+ [nil, nil, nil, 7, nil, nil, nil, nil, 5],
68
+ ]
69
+
70
+ solver = SudokuSolver::BruteForceSolver.new(input)
71
+ output = solver.solve
72
+ assert(output.final_state?, "Failed! The result is not valid")
73
+ end
74
+
75
+ # def test_solver_many_slots_3
76
+ # input = [
77
+ # [nil, 8, nil, nil, nil, nil, nil, nil, 5],
78
+ # [nil, nil, nil, nil, 4, nil, nil, nil, nil],
79
+ # [5, nil, nil, nil, nil, nil, nil, nil, 6],
80
+ # [nil, 4, 1, nil, nil, nil, 8, nil, nil],
81
+ # [nil, nil, nil, 3, nil, 8, nil, nil, nil],
82
+ # [nil, 9, nil, nil, nil, nil, nil, nil, nil],
83
+ # [nil, nil, nil, nil, nil, 5, nil, nil, nil],
84
+ # [3, nil, nil, 6, nil, 7, nil, nil, nil],
85
+ # [nil, nil, nil, nil, nil, nil, 4, 1, nil],
86
+ # ]
87
+
88
+ # solver = SudokuSolver.new(input)
89
+ # output = solver.solve
90
+ # assert(output.final_state?, "Failed! The result is not valid")
91
+ # end
92
+
93
+ def test_solver_all_empty
94
+ input = [
95
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
96
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
97
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
98
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
99
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
100
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
101
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
102
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
103
+ [nil, nil, nil, nil, nil, nil, nil, nil, nil],
104
+ ]
105
+
106
+ solver = SudokuSolver::BruteForceSolver.new(input)
107
+ output = solver.solve
108
+ assert(output.final_state?, "Failed! The result is not valid")
109
+ end
110
+ end
@@ -0,0 +1,56 @@
1
+ require 'minitest/autorun'
2
+ require 'sudoku_solver'
3
+
4
+ class TableTest < Minitest::Test
5
+ def test_final_state_true
6
+ table = SudokuSolver::Table.new([
7
+ [6, 2, 9, 8, 1, 3, 7, 5, 4],
8
+ [7, 8, 3, 9, 5, 4, 6, 2, 1],
9
+ [5, 1, 4, 6, 7, 2, 3, 9, 8],
10
+ [1, 7, 5, 2, 3, 8, 9, 4, 6],
11
+ [9, 3, 2, 4, 6, 1, 8, 7, 5],
12
+ [4, 6, 8, 7, 9, 5, 1, 3, 2],
13
+ [3, 9, 1, 5, 4, 6, 2, 8, 7],
14
+ [8, 4, 6, 3, 2, 7, 5, 1, 9],
15
+ [2, 5, 7, 1, 8, 9, 4, 6, 3]]
16
+ )
17
+
18
+ assert(table.final_state?, "Failed! Expected the final_state? returns true")
19
+ end
20
+
21
+ def test_final_state_false_because_of_missing_slots
22
+ table = SudokuSolver::Table.new([
23
+ [6, 2, nil, 8, 1, 3, 7, 5, 4],
24
+ [7, 8, 3, 9, 5, 4, 6, 2, 1],
25
+ [5, 1, 4, 6, 7, 2, 3, 9, 8],
26
+ [1, 7, 5, 2, 3, 8, 9, 4, 6],
27
+ [9, 3, 2, 4, 6, 1, 8, 7, 5],
28
+ [4, 6, 8, 7, 9, 5, 1, 3, 2],
29
+ [3, 9, 1, 5, 4, 6, 2, 8, 7],
30
+ [8, 4, 6, 3, 2, 7, 5, 1, 9],
31
+ [2, 5, 7, nil, 8, 9, 4, 6, 3]]
32
+ )
33
+
34
+ assert(
35
+ !table.final_state?,
36
+ "Failed! Expected the final_state? returns false if any slots are missing")
37
+ end
38
+
39
+ def test_final_state_false_because_of_dup_value
40
+ table = SudokuSolver::Table.new([
41
+ [6, 2, 9, 8, 1, 3, 7, 5, 4],
42
+ [7, 8, 3, 9, 5, 4, 6, 2, 1],
43
+ [5, 1, 4, 6, 7, 2, 3, 9, 8],
44
+ [1, 7, 5, 2, 3, 8, 9, 4, 6],
45
+ [9, 3, 2, 4, 6, 1, 8, 7, 5],
46
+ [4, 6, 8, 7, 9, 5, 1, 3, 2],
47
+ [3, 9, 1, 5, 4, 6, 2, 8, 7],
48
+ [8, 4, 6, 3, 2, 7, 5, 2, 9],
49
+ [2, 5, 7, 1, 8, 9, 4, 6, 3]]
50
+ )
51
+
52
+ assert(
53
+ !table.final_state?,
54
+ "Failed! Expected the final_state? returns false if any slots are duplicated")
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guava_sudoku_solver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - victor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Provide convenient way to solve a sudoku challenge
56
+ email:
57
+ - vu.tran54@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/sudoku_solver.rb
69
+ - lib/sudoku_solver/brute_force/bruteforcable.rb
70
+ - lib/sudoku_solver/brute_force_corpus.rb
71
+ - lib/sudoku_solver/brute_force_solver.rb
72
+ - lib/sudoku_solver/table.rb
73
+ - lib/sudoku_solver/variable.rb
74
+ - lib/sudoku_solver/version.rb
75
+ - sudoku_solver.gemspec
76
+ - test/brute_force/test_broteforcable.rb
77
+ - test/test_brute_force_corpus.rb
78
+ - test/test_sudoku_solver.rb
79
+ - test/test_table.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.7
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Solver for sudoku 9x9
104
+ test_files:
105
+ - test/brute_force/test_broteforcable.rb
106
+ - test/test_brute_force_corpus.rb
107
+ - test/test_sudoku_solver.rb
108
+ - test/test_table.rb
109
+ has_rdoc: