solve_sudoku 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +8 -0
- data/README.md +39 -0
- data/bin/solve_sudoku +32 -0
- data/lib/solve_sudoku.rb +2 -0
- data/lib/sudoku/grid/cell.rb +24 -0
- data/lib/sudoku/grid.rb +56 -0
- data/lib/sudoku/solution_set/choice.rb +12 -0
- data/lib/sudoku/solution_set/constraints/box_must_contain_value.rb +18 -0
- data/lib/sudoku/solution_set/constraints/cell_must_be_filled.rb +16 -0
- data/lib/sudoku/solution_set/constraints/column_must_contain_value.rb +16 -0
- data/lib/sudoku/solution_set/constraints/row_must_contain_value.rb +16 -0
- data/lib/sudoku/solution_set/constraints.rb +5 -0
- data/lib/sudoku/solution_set/process/out_of_history.rb +9 -0
- data/lib/sudoku/solution_set/process/step.rb +63 -0
- data/lib/sudoku/solution_set/process/steps/delete_redundant_choices.rb +26 -0
- data/lib/sudoku/solution_set/process/steps/move_choice_into_included_set.rb +18 -0
- data/lib/sudoku/solution_set/process/steps/pick_choice_satisfying_constraint.rb +25 -0
- data/lib/sudoku/solution_set/process/steps/pick_unsatisfied_constraint.rb +31 -0
- data/lib/sudoku/solution_set/process/steps.rb +7 -0
- data/lib/sudoku/solution_set/process.rb +113 -0
- data/lib/sudoku/solution_set.rb +148 -0
- data/puzzles/blank.txt +10 -0
- data/puzzles/easy.txt +10 -0
- data/puzzles/expert.txt +10 -0
- data/puzzles/extreme.txt +10 -0
- data/puzzles/hard.txt +10 -0
- data/puzzles/multiple-solutions.txt +10 -0
- data/puzzles/standard.txt +10 -0
- data/puzzles/unsolvable.txt +10 -0
- metadata +76 -0
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (C) 2012 David Trasbo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Solve Sudoku
|
2
|
+
============
|
3
|
+
|
4
|
+
Accepts a Sudoku puzzle (a partially completed grid) and outputs all possible solutions. The algorithm is based on this article: http://garethrees.org/2007/06/10/zendoku-generation/
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
`solve_sudoku puzzle.txt`
|
10
|
+
|
11
|
+
RubyGems makes the executable available automatically. Otherwise,
|
12
|
+
the executable lives in `bin/`.
|
13
|
+
|
14
|
+
Examples in `puzzles/`. Use `puzzles/blank.txt` as a template for new
|
15
|
+
puzzles.
|
16
|
+
|
17
|
+
Installation
|
18
|
+
------------
|
19
|
+
|
20
|
+
As a gem:
|
21
|
+
|
22
|
+
`gem install solve_sudoku`
|
23
|
+
|
24
|
+
Alternatively, clone the repository for easier access to example
|
25
|
+
puzzles:
|
26
|
+
|
27
|
+
`git clone https://github.com/dtrasbo/solve_sudoku.git`
|
28
|
+
|
29
|
+
Compatibility
|
30
|
+
-------------
|
31
|
+
|
32
|
+
Developed and tested using Ruby 1.9.3-p125.
|
33
|
+
|
34
|
+
License
|
35
|
+
-------
|
36
|
+
|
37
|
+
Copyright (C) 2012 David Trasbo. Released under the MIT License. Please
|
38
|
+
refer to `LICENSE`.
|
39
|
+
|
data/bin/solve_sudoku
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems' # For 1.8.7 compatibility.
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'solve_sudoku'
|
6
|
+
|
7
|
+
input = ARGF.read
|
8
|
+
counter = 0
|
9
|
+
|
10
|
+
begin
|
11
|
+
Sudoku::SolutionSet.new(input).each do |solution|
|
12
|
+
puts "\e[37m#{'-' * 35}\e[0m"
|
13
|
+
puts "\e[1mSolution ##{counter += 1}\e[0m"
|
14
|
+
puts
|
15
|
+
puts solution
|
16
|
+
puts
|
17
|
+
puts
|
18
|
+
end
|
19
|
+
rescue Sudoku::SolutionSet::Process::OutOfHistory
|
20
|
+
print "\e[1m"
|
21
|
+
|
22
|
+
if counter.zero?
|
23
|
+
puts "\e[31mThe puzzle is unsolvable."
|
24
|
+
elsif counter == 1
|
25
|
+
puts "\e[32mThe input is a proper puzzle."
|
26
|
+
else
|
27
|
+
puts "\e[33mThe puzzle has multiple solutions.\e[0m"
|
28
|
+
end
|
29
|
+
|
30
|
+
print "\e[0m"
|
31
|
+
end
|
32
|
+
|
data/lib/solve_sudoku.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class Grid
|
3
|
+
class Cell
|
4
|
+
attr_reader :value
|
5
|
+
|
6
|
+
def initialize(grid, value)
|
7
|
+
@grid, @value = grid, value
|
8
|
+
end
|
9
|
+
|
10
|
+
def filled?
|
11
|
+
!(@value.nil? || @value.zero?)
|
12
|
+
end
|
13
|
+
|
14
|
+
def column
|
15
|
+
@grid.column(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def row
|
19
|
+
@grid.row(self)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/lib/sudoku/grid.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'sudoku/grid/cell'
|
2
|
+
|
3
|
+
module Sudoku
|
4
|
+
class Grid
|
5
|
+
def initialize(string=nil)
|
6
|
+
@rows = if string
|
7
|
+
string.split($/).collect { |row|
|
8
|
+
row.split(',').collect { |value| Cell.new(self, value.to_i) }
|
9
|
+
}
|
10
|
+
else
|
11
|
+
(1..9).collect { |n| [] }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(column, row, value)
|
16
|
+
@rows[row - 1][column - 1] = value
|
17
|
+
end
|
18
|
+
|
19
|
+
def cells
|
20
|
+
@rows.flatten
|
21
|
+
end
|
22
|
+
|
23
|
+
def filled_cells
|
24
|
+
cells.select { |c| c.filled? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def column(cell)
|
28
|
+
row = @rows.find { |r| r.include?(cell) }
|
29
|
+
row.index(cell) + 1
|
30
|
+
end
|
31
|
+
|
32
|
+
def row(cell)
|
33
|
+
row = @rows.find { |r| r.include?(cell) }
|
34
|
+
@rows.index(row) + 1
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
row_divider = (1..3).collect { "\e[37m---|---|---\e[0m" }.join('|')
|
39
|
+
row_divider = "\n#{row_divider}\n"
|
40
|
+
|
41
|
+
row_group_divider = (1..3).collect { '-' * 11 }.join('+')
|
42
|
+
row_group_divider = "\n#{row_group_divider}\n"
|
43
|
+
|
44
|
+
[0..2, 3..5, 6..8].collect { |row_group|
|
45
|
+
@rows[row_group].collect { |row|
|
46
|
+
[0..2, 3..5, 6..8].collect { |col_group|
|
47
|
+
col_group.collect { |column|
|
48
|
+
" #{row[column] || ' '} "
|
49
|
+
}.join("\e[37m|\e[0m")
|
50
|
+
}.join('|')
|
51
|
+
}.join(row_divider)
|
52
|
+
}.join(row_group_divider)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
module Constraints
|
4
|
+
class BoxMustContainValue
|
5
|
+
def initialize(box_x, box_y, value)
|
6
|
+
@box_x, @box_y, @value = box_x, box_y, value
|
7
|
+
end
|
8
|
+
|
9
|
+
def satisfied_by?(choice)
|
10
|
+
choice.column.between?(@box_x * 3 - 2, @box_x * 3) &&
|
11
|
+
choice.row.between?(@box_y * 3 - 2, @box_y * 3) &&
|
12
|
+
choice.value == @value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
module Constraints
|
4
|
+
class CellMustBeFilled
|
5
|
+
def initialize(column, row)
|
6
|
+
@column, @row = column, row
|
7
|
+
end
|
8
|
+
|
9
|
+
def satisfied_by?(choice)
|
10
|
+
choice.column == @column && choice.row == @row
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
module Constraints
|
4
|
+
class ColumnMustContainValue
|
5
|
+
def initialize(column, value)
|
6
|
+
@column, @value = column, value
|
7
|
+
end
|
8
|
+
|
9
|
+
def satisfied_by?(choice)
|
10
|
+
choice.column == @column && choice.value == @value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
module Constraints
|
4
|
+
class RowMustContainValue
|
5
|
+
def initialize(row, value)
|
6
|
+
@row, @value = row, value
|
7
|
+
end
|
8
|
+
|
9
|
+
def satisfied_by?(choice)
|
10
|
+
choice.row == @row && choice.value == @value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
class Process
|
4
|
+
class Step
|
5
|
+
attr_reader :output
|
6
|
+
|
7
|
+
def initialize(solution_set)
|
8
|
+
@solution_set = solution_set
|
9
|
+
end
|
10
|
+
|
11
|
+
def take(input=nil)
|
12
|
+
@input ||= input
|
13
|
+
@output = take!(*[@input].compact)
|
14
|
+
end
|
15
|
+
|
16
|
+
def take!(*args)
|
17
|
+
raise NotImplementedError, "#{self.class}#take! not implemented"
|
18
|
+
end
|
19
|
+
|
20
|
+
def revert
|
21
|
+
revert!
|
22
|
+
end
|
23
|
+
|
24
|
+
def revert!
|
25
|
+
raise NotImplementedError, "#{self.class}#revert! not implemented"
|
26
|
+
end
|
27
|
+
|
28
|
+
def retry?
|
29
|
+
@output
|
30
|
+
end
|
31
|
+
|
32
|
+
def constraints
|
33
|
+
@solution_set.constraints
|
34
|
+
end
|
35
|
+
|
36
|
+
def excluded_choices
|
37
|
+
@solution_set.excluded_choices
|
38
|
+
end
|
39
|
+
|
40
|
+
def included_choices
|
41
|
+
@solution_set.included_choices
|
42
|
+
end
|
43
|
+
|
44
|
+
def include_choice(choice)
|
45
|
+
@solution_set.include_choice(choice)
|
46
|
+
end
|
47
|
+
|
48
|
+
def exclude_choice(choice)
|
49
|
+
@solution_set.exclude_choice(choice)
|
50
|
+
end
|
51
|
+
|
52
|
+
def restore_excluded_choices(choices)
|
53
|
+
@solution_set.restore_excluded_choices(choices)
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_excluded_choices_if(&block)
|
57
|
+
@solution_set.delete_excluded_choices_if(&block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
class Process
|
4
|
+
module Steps
|
5
|
+
class DeleteRedundantChoices < Step
|
6
|
+
def take!(choice)
|
7
|
+
satisfied_constraints = constraints.select { |constraint|
|
8
|
+
constraint.satisfied_by?(choice)
|
9
|
+
}
|
10
|
+
|
11
|
+
delete_excluded_choices_if do |excluded_choice|
|
12
|
+
satisfied_constraints.any? { |satisfied_constraint|
|
13
|
+
satisfied_constraint.satisfied_by?(excluded_choice)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def revert!
|
19
|
+
restore_excluded_choices(output)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
class Process
|
4
|
+
module Steps
|
5
|
+
class PickChoiceSatisfyingConstraint < Step
|
6
|
+
def take!(constraint)
|
7
|
+
candidates = excluded_choices.select { |excluded_choice|
|
8
|
+
constraint.satisfied_by?(excluded_choice)
|
9
|
+
}
|
10
|
+
|
11
|
+
if retry?
|
12
|
+
candidates.slice!(0..candidates.index(output))
|
13
|
+
end
|
14
|
+
|
15
|
+
candidates.first
|
16
|
+
end
|
17
|
+
|
18
|
+
def revert!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Sudoku
|
2
|
+
class SolutionSet
|
3
|
+
class Process
|
4
|
+
module Steps
|
5
|
+
class PickUnsatisfiedConstraint < Step
|
6
|
+
def take!(input=nil)
|
7
|
+
candidates = constraints.select { |constraint|
|
8
|
+
included_choices.none? { |included_choice|
|
9
|
+
constraint.satisfied_by?(included_choice)
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
if candidates.any?
|
14
|
+
candidates.min_by { |constraint|
|
15
|
+
excluded_choices.select { |excluded_choice|
|
16
|
+
constraint.satisfied_by?(excluded_choice)
|
17
|
+
}.size
|
18
|
+
}
|
19
|
+
else
|
20
|
+
:solved
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def revert!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'sudoku/solution_set/process/step'
|
2
|
+
|
3
|
+
require 'sudoku/solution_set/process/steps/pick_unsatisfied_constraint'
|
4
|
+
require 'sudoku/solution_set/process/steps/pick_choice_satisfying_constraint'
|
5
|
+
require 'sudoku/solution_set/process/steps/move_choice_into_included_set'
|
6
|
+
require 'sudoku/solution_set/process/steps/delete_redundant_choices'
|
7
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'sudoku/solution_set/process/steps'
|
2
|
+
require 'sudoku/solution_set/process/out_of_history'
|
3
|
+
|
4
|
+
module Sudoku
|
5
|
+
class SolutionSet
|
6
|
+
class Process
|
7
|
+
STEPS = Hash[[
|
8
|
+
:pick_unsatisfied_constraint,
|
9
|
+
:pick_choice_satisfying_constraint,
|
10
|
+
:move_choice_into_included_set,
|
11
|
+
:delete_redundant_choices
|
12
|
+
].collect { |name|
|
13
|
+
[
|
14
|
+
name,
|
15
|
+
Steps.const_get(
|
16
|
+
name.to_s.split('_').collect(&:capitalize).join
|
17
|
+
)
|
18
|
+
]
|
19
|
+
}]
|
20
|
+
|
21
|
+
FALLBACKS = {
|
22
|
+
:pick_choice_satisfying_constraint => :pick_choice_satisfying_constraint
|
23
|
+
}
|
24
|
+
|
25
|
+
def initialize(solution_set)
|
26
|
+
@solution_set = solution_set
|
27
|
+
@queue, @history = [], []
|
28
|
+
end
|
29
|
+
|
30
|
+
def run!
|
31
|
+
loop do
|
32
|
+
if @queue.empty?
|
33
|
+
queue(STEPS.keys.first)
|
34
|
+
else
|
35
|
+
if proceed == :solved
|
36
|
+
prepare_next_run
|
37
|
+
return true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def queue(step, options={})
|
44
|
+
options = { :action => :take }.merge(options)
|
45
|
+
|
46
|
+
if step.is_a?(Symbol)
|
47
|
+
step = STEPS[step].new(@solution_set)
|
48
|
+
end
|
49
|
+
|
50
|
+
@queue << {
|
51
|
+
:action => options[:action],
|
52
|
+
:step => step,
|
53
|
+
:input => options[:input]
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def queue_backtrack(step)
|
58
|
+
@history.reverse_each do |item|
|
59
|
+
queue(item[:step], :action => :revert)
|
60
|
+
|
61
|
+
if item[:step].class == STEPS[step]
|
62
|
+
queue(item[:step])
|
63
|
+
return
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
raise OutOfHistory, "unable to backtrack to #{step}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def proceed
|
71
|
+
item = @queue.shift
|
72
|
+
|
73
|
+
case item[:action]
|
74
|
+
when :take
|
75
|
+
if output = item[:step].take(item[:input])
|
76
|
+
@history << item
|
77
|
+
|
78
|
+
if output == :solved
|
79
|
+
return :solved
|
80
|
+
end
|
81
|
+
|
82
|
+
queue(successor(item[:step]), :input => output)
|
83
|
+
else
|
84
|
+
queue_backtrack(fallback(item[:step]))
|
85
|
+
end
|
86
|
+
when :revert
|
87
|
+
item[:step].revert
|
88
|
+
@history.pop
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def prepare_next_run
|
93
|
+
queue_backtrack(:pick_choice_satisfying_constraint)
|
94
|
+
end
|
95
|
+
|
96
|
+
def successor(step)
|
97
|
+
names = STEPS.keys
|
98
|
+
classes = STEPS.values
|
99
|
+
|
100
|
+
if step.class == classes.last
|
101
|
+
names.first
|
102
|
+
else
|
103
|
+
names[classes.index(step.class) + 1]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def fallback(step)
|
108
|
+
FALLBACKS[STEPS.invert[step.class]]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'sudoku/grid'
|
2
|
+
require 'sudoku/solution_set/process'
|
3
|
+
require 'sudoku/solution_set/constraints'
|
4
|
+
require 'sudoku/solution_set/choice'
|
5
|
+
|
6
|
+
module Sudoku
|
7
|
+
class SolutionSet
|
8
|
+
attr_reader :constraints, :included_choices
|
9
|
+
|
10
|
+
def initialize(puzzle)
|
11
|
+
@puzzle = Grid.new(puzzle)
|
12
|
+
@process = Process.new(self)
|
13
|
+
|
14
|
+
initialize_constraints
|
15
|
+
initialize_excluded_choices
|
16
|
+
initialize_included_choices
|
17
|
+
delete_redundant_choices
|
18
|
+
end
|
19
|
+
|
20
|
+
def each
|
21
|
+
loop do
|
22
|
+
if @process.run!
|
23
|
+
yield to_grid
|
24
|
+
else
|
25
|
+
return
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def excluded_choices
|
31
|
+
@excluded_choices.sort_by! { |choice|
|
32
|
+
[choice.column, choice.row, choice.value]
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def include_choice(choice)
|
37
|
+
@excluded_choices.delete(choice)
|
38
|
+
@included_choices << choice
|
39
|
+
choice
|
40
|
+
end
|
41
|
+
|
42
|
+
def exclude_choice(choice)
|
43
|
+
@included_choices.delete(choice)
|
44
|
+
@excluded_choices << choice
|
45
|
+
choice
|
46
|
+
end
|
47
|
+
|
48
|
+
def delete_excluded_choices_if
|
49
|
+
deleted_choices = []
|
50
|
+
|
51
|
+
@excluded_choices.delete_if do |excluded_choice|
|
52
|
+
if yield excluded_choice
|
53
|
+
deleted_choices << excluded_choice
|
54
|
+
true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
deleted_choices
|
59
|
+
end
|
60
|
+
|
61
|
+
def restore_excluded_choices(choices)
|
62
|
+
choices.each do |choice|
|
63
|
+
@excluded_choices << choice
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_grid
|
68
|
+
grid = Grid.new
|
69
|
+
|
70
|
+
@included_choices.each do |choice|
|
71
|
+
grid[choice.column, choice.row] = choice.value
|
72
|
+
end
|
73
|
+
|
74
|
+
grid
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def initialize_constraints
|
80
|
+
@constraints = []
|
81
|
+
|
82
|
+
(1..9).each do |column|
|
83
|
+
(1..9).each do |row|
|
84
|
+
@constraints << Constraints::CellMustBeFilled.new(column, row)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
(1..9).each do |column|
|
89
|
+
(1..9).each do |value|
|
90
|
+
@constraints << Constraints::ColumnMustContainValue.new(column, value)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
(1..9).each do |row|
|
95
|
+
(1..9).each do |value|
|
96
|
+
@constraints << Constraints::RowMustContainValue.new(row, value)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
(1..3).each do |box_x|
|
101
|
+
(1..3).each do |box_y|
|
102
|
+
(1..9).each do |value|
|
103
|
+
@constraints << Constraints::BoxMustContainValue.new(box_x, box_y, value)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def initialize_excluded_choices
|
110
|
+
@excluded_choices = []
|
111
|
+
|
112
|
+
(1..9).each do |column|
|
113
|
+
(1..9).each do |row|
|
114
|
+
(1..9).each do |value|
|
115
|
+
@excluded_choices << Choice.new(column, row, value)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def initialize_included_choices
|
122
|
+
@included_choices = []
|
123
|
+
|
124
|
+
@puzzle.filled_cells.each do |cell|
|
125
|
+
include_choice(@excluded_choices.find { |choice|
|
126
|
+
choice.column == cell.column &&
|
127
|
+
choice.row == cell.row &&
|
128
|
+
choice.value == cell.value
|
129
|
+
})
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def delete_redundant_choices
|
134
|
+
satisfied_constraints = @constraints.select { |constraint|
|
135
|
+
@included_choices.any? { |excluded_choice|
|
136
|
+
constraint.satisfied_by?(excluded_choice)
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
@excluded_choices.delete_if do |excluded_choice|
|
141
|
+
satisfied_constraints.any? { |sc|
|
142
|
+
sc.satisfied_by?(excluded_choice)
|
143
|
+
}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
data/puzzles/blank.txt
ADDED
data/puzzles/easy.txt
ADDED
data/puzzles/expert.txt
ADDED
data/puzzles/extreme.txt
ADDED
data/puzzles/hard.txt
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solve_sudoku
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Trasbo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Accepts a Sudoku puzzle (a partially completed grid) and outputs all
|
15
|
+
possible solutions. The algorithm is based on this article: http://garethrees.org/2007/06/10/zendoku-generation/'
|
16
|
+
email: me@dtrasbo.com
|
17
|
+
executables:
|
18
|
+
- solve_sudoku
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- bin/solve_sudoku
|
25
|
+
- lib/solve_sudoku.rb
|
26
|
+
- lib/sudoku/grid.rb
|
27
|
+
- lib/sudoku/grid/cell.rb
|
28
|
+
- lib/sudoku/solution_set.rb
|
29
|
+
- lib/sudoku/solution_set/choice.rb
|
30
|
+
- lib/sudoku/solution_set/constraints.rb
|
31
|
+
- lib/sudoku/solution_set/constraints/box_must_contain_value.rb
|
32
|
+
- lib/sudoku/solution_set/constraints/cell_must_be_filled.rb
|
33
|
+
- lib/sudoku/solution_set/constraints/column_must_contain_value.rb
|
34
|
+
- lib/sudoku/solution_set/constraints/row_must_contain_value.rb
|
35
|
+
- lib/sudoku/solution_set/process.rb
|
36
|
+
- lib/sudoku/solution_set/process/out_of_history.rb
|
37
|
+
- lib/sudoku/solution_set/process/step.rb
|
38
|
+
- lib/sudoku/solution_set/process/steps.rb
|
39
|
+
- lib/sudoku/solution_set/process/steps/delete_redundant_choices.rb
|
40
|
+
- lib/sudoku/solution_set/process/steps/move_choice_into_included_set.rb
|
41
|
+
- lib/sudoku/solution_set/process/steps/pick_choice_satisfying_constraint.rb
|
42
|
+
- lib/sudoku/solution_set/process/steps/pick_unsatisfied_constraint.rb
|
43
|
+
- puzzles/blank.txt
|
44
|
+
- puzzles/easy.txt
|
45
|
+
- puzzles/expert.txt
|
46
|
+
- puzzles/extreme.txt
|
47
|
+
- puzzles/hard.txt
|
48
|
+
- puzzles/multiple-solutions.txt
|
49
|
+
- puzzles/standard.txt
|
50
|
+
- puzzles/unsolvable.txt
|
51
|
+
homepage: https://github.com/dtrasbo/solve_sudoku
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.8.16
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Accepts a Sudoku puzzle (a partially completed grid) and outputs all possible
|
75
|
+
solutions.
|
76
|
+
test_files: []
|