sudoku_solver 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michel Belleville
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = sudoku_solver
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Michel Belleville. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sudoku_solver"
8
+ gem.summary = %Q{A pretty simple sudoku solver}
9
+ gem.description = %Q{A very simple sudoku problems solver}
10
+ gem.email = "michel.belleville@gmail.com"
11
+ gem.homepage = "http://github.com/Bastes/SudokuSolver"
12
+ gem.authors = ["Michel Belleville"]
13
+ gem.add_dependency "cellular_map", ">= 0.6.0"
14
+ gem.add_development_dependency "shoulda", ">= 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "sudoku_solver #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
54
+
55
+ desc 'Start an IRB session with all necessary files required.'
56
+ task :shell do |t|
57
+ chdir File.dirname(__FILE__)
58
+ exec 'irb -I lib/ -I lib/sudoku_solver -r solver'
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1 @@
1
+ require "sudoku_solver/solver"
@@ -0,0 +1,62 @@
1
+ require 'cellular_map'
2
+
3
+ module SudokuSolver
4
+ module Cell
5
+ attr_accessor :grid
6
+
7
+ def neighbours
8
+ sx = x - x % grid.dimension
9
+ sy = y - y % grid.dimension
10
+ neighbours_values = [
11
+ grid[grid.size, y],
12
+ grid[x, grid.size],
13
+ grid[sx..(sx + grid.dimension - 1), sy..(sy + grid.dimension - 1)] ].
14
+ inject([]) { |r, z| r + z.to_a }.flatten.uniq.reject { |d| d == self }
15
+ end
16
+ end
17
+
18
+ class Grid < CellularMap::Zone
19
+ attr_reader :dimension, :size, :possibilities
20
+
21
+ def initialize(dimension)
22
+ @dimension = dimension
23
+ @width = @dimension ** 2
24
+ @size = (0..(@width - 1))
25
+ @possibilities = (1..@width)
26
+ super(@size, @size, CellularMap::Map.new)
27
+ end
28
+
29
+ def [](x, y)
30
+ cell = super
31
+ if cell.is_a? CellularMap::Cell
32
+ cell.extend Cell
33
+ cell.grid = self
34
+ end
35
+ cell
36
+ end
37
+
38
+ def each # :nodoc:
39
+ super { |cell|
40
+ if cell.is_a? CellularMap::Cell
41
+ cell.extend Cell
42
+ cell.grid = self
43
+ end
44
+ yield cell
45
+ }
46
+ end
47
+
48
+ def solved?
49
+ ! self.detect { |c| c.content.nil? || c.content.length != 1 }
50
+ end
51
+
52
+ protected
53
+
54
+ def initialize_copy(other) # :nodoc:
55
+ @map = other.map.dup
56
+ @map.each { |c|
57
+ c.content = c.content.dup rescue nil
58
+ c.content = nil unless x === c.x && y === c.y
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,80 @@
1
+ require 'sudoku_solver/grid'
2
+
3
+ module SudokuSolver
4
+ class InvalidProblemError < Exception
5
+ attr_reader :problem
6
+ def initialize(problem)
7
+ @problem = problem
8
+ end
9
+ end
10
+
11
+ class Solver
12
+ def self.solve(problem)
13
+ self.new(problem).solve
14
+ end
15
+
16
+ def grid
17
+ Grid.new(@dimension).each { |c|
18
+ v = @problem[c.y][c.x]
19
+ unless v.nil? || @possibilities.to_a.include?(v)
20
+ raise InvalidProblemError.new(@problem)
21
+ end
22
+ c.content = v ? [v] : @possibilities.to_a
23
+ }
24
+ end
25
+
26
+ def initialize(problem)
27
+ @problem = problem
28
+ @width = @problem.length
29
+ if problem.any? { |l| l.length != @width }
30
+ raise InvalidProblemError.new(problem)
31
+ end
32
+ @dimension = Math.sqrt(@width).to_i
33
+ @size = (0..(@width -1))
34
+ @possibilities = (1..@width)
35
+ rescue
36
+ raise InvalidProblemError.new(problem)
37
+ end
38
+
39
+ def solve
40
+ self.step(self.grid).to_a.collect { |l| l.collect { |c|
41
+ c.content.length == 1 ? c.content.first : nil } }
42
+ end
43
+
44
+ def step(grid)
45
+ grid = self.reduce(grid)
46
+ return grid if grid.solved?
47
+ cell = grid.detect { |c| c.content.length > 1 }
48
+ cell.content.each { |value|
49
+ begin
50
+ new_grid = grid.dup
51
+ new_grid[cell.x, cell.y] = [value]
52
+ solution = step(new_grid)
53
+ return solution if solution.solved?
54
+ rescue InvalidProblemError
55
+ end
56
+ }
57
+ raise InvalidProblemError.new(@problem)
58
+ end
59
+
60
+ def reduce(grid)
61
+ unsolved = grid.reject { |c| c.content.length == 1 }
62
+ until unsolved.empty? do
63
+ changed = unsolved.reject { |c| restrict(c) }
64
+ break unless changed.length > 0
65
+ unsolved.reject! { |c| c.content.length == 1 }
66
+ end
67
+ grid
68
+ end
69
+
70
+ def restrict(cell)
71
+ taken = cell.neighbours.collect { |c| c.content }.
72
+ reject { |r| r.length > 1 }.flatten
73
+ remaining = (cell.content - taken).sort
74
+ if remaining.length == 0
75
+ raise InvalidProblemError.new(@problem)
76
+ end
77
+ (cell.content == remaining) || ! (cell.content = remaining)
78
+ end
79
+ end
80
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'sudoku_solver'
8
+
9
+ class Test::Unit::TestCase
10
+ end
data/test/test_grid.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'helper'
2
+
3
+ class TestGrid < Test::Unit::TestCase
4
+ context('An empty 2x2 grid') {
5
+ setup { @grid = SudokuSolver::Grid.new(2) }
6
+ should('have the proper dimensions') {
7
+ assert_equal 2, @grid.dimension
8
+ assert_equal 4, @grid.width
9
+ assert_equal 4, @grid.height
10
+ assert_equal (0..3), @grid.size
11
+ assert_equal (1..4), @grid.possibilities
12
+ }
13
+ }
14
+
15
+ context('An empty 3x3 grid') {
16
+ setup { @grid = SudokuSolver::Grid.new(3) }
17
+ should('have the proper dimensions') {
18
+ assert_equal 3, @grid.dimension
19
+ assert_equal 9, @grid.width
20
+ assert_equal 9, @grid.height
21
+ assert_equal (0..8), @grid.size
22
+ assert_equal (1..9), @grid.possibilities
23
+ }
24
+
25
+ context('considering one of its cells') {
26
+ setup {
27
+ @cell1 = @grid[2, 5]
28
+ @cell2 = @grid.detect { |c|
29
+ [c.x, c.y] == [2, 5] }
30
+ }
31
+ should('find the neighbours') {
32
+ expected, obtained1, obtained2 = [
33
+ [
34
+ @grid[0, 5],
35
+ @grid[1, 5],
36
+ @grid[3, 5],
37
+ @grid[4, 5],
38
+ @grid[5, 5],
39
+ @grid[6, 5],
40
+ @grid[7, 5],
41
+ @grid[8, 5],
42
+ @grid[2, 0],
43
+ @grid[2, 1],
44
+ @grid[2, 2],
45
+ @grid[2, 3],
46
+ @grid[2, 4],
47
+ @grid[2, 6],
48
+ @grid[2, 7],
49
+ @grid[2, 8],
50
+ @grid[0, 3],
51
+ @grid[1, 3],
52
+ @grid[0, 4],
53
+ @grid[1, 4]
54
+ ],
55
+ @cell1.neighbours,
56
+ @cell2.neighbours
57
+ ].collect { |a| a.sort }
58
+ assert_equal expected, obtained1
59
+ assert_equal expected, obtained2
60
+ }
61
+ }
62
+
63
+ context('cloned') {
64
+ setup {
65
+ @grid[4, 4] = [1,2,3]
66
+ @clone = @grid.dup
67
+ }
68
+ should("be equal") { assert_equal @grid, @clone }
69
+ should("not be the same object") { assert_not_same @grid, @clone }
70
+ should("evolve their own separate way") {
71
+ @grid[1, 2] = [1, 2, 3]
72
+ @clone[1, 2] = [5, 6, 7]
73
+ @clone[4, 4].content.reject! { |v| v == 3 }
74
+ assert_equal [1, 2, 3], @grid[1, 2].content
75
+ assert_equal [1, 2, 3], @grid[4, 4].content
76
+ assert_equal [5, 6, 7], @clone[1, 2].content
77
+ assert_equal [1, 2], @clone[4, 4].content
78
+ assert_not_equal @grid, @clone
79
+ }
80
+ }
81
+ }
82
+
83
+ context('An empty 4x4 grid') {
84
+ setup { @grid = SudokuSolver::Grid.new(4) }
85
+ should('have the proper dimensions') {
86
+ assert_equal 4, @grid.dimension
87
+ assert_equal 16, @grid.width
88
+ assert_equal 16, @grid.height
89
+ assert_equal (0..15), @grid.size
90
+ assert_equal (1..16), @grid.possibilities
91
+ }
92
+ should('know it\'s not solved already') {
93
+ assert !@grid.solved? }
94
+ }
95
+
96
+ context('A 2x2 grid, solved') {
97
+ setup {
98
+ @grid = SudokuSolver::Grid.new(2)
99
+ problem = [ [ 1, 2, 3, 4 ],
100
+ [ 3, 4, 1, 2 ],
101
+ [ 2, 1, 4, 3 ],
102
+ [ 4, 3, 2, 1 ] ]
103
+ @grid.each { |c| c.content = [problem[c.x][c.y]] }
104
+ }
105
+ should('know it\'s solved already') {
106
+ assert @grid.solved? }
107
+ }
108
+ end
@@ -0,0 +1,137 @@
1
+ require 'helper'
2
+
3
+ class TestSolver < Test::Unit::TestCase
4
+ [ :blah,
5
+ "truc",
6
+ 1234,
7
+ [ [2] ],
8
+ [ [1], [2] ],
9
+ [ [1, nil, nil, nil],
10
+ [1, nil, nil, nil],
11
+ [nil, nil, nil, nil],
12
+ [nil, nil, nil, nil] ] ].each { |problem|
13
+ context("This absurd problem #{problem.inspect}") {
14
+ should('be impossible to solve') {
15
+ assert_raise(SudokuSolver::InvalidProblemError) {
16
+ SudokuSolver::Solver.solve(problem) }
17
+ }
18
+ }
19
+ }
20
+
21
+ { [[nil]] => [[1]] }.each { |problem, solution|
22
+ context("The simplest problem in the world #{problem}") {
23
+ should('be easy to solve') {
24
+ assert_equal solution, SudokuSolver::Solver.solve(problem)
25
+ }
26
+ }
27
+ }
28
+
29
+ context('A 4x4 problem') {
30
+ setup {
31
+ @solution =
32
+ [ [ 1, 2, 3, 4],
33
+ [ 3, 4, 2, 1],
34
+ [ 2, 1, 4, 3],
35
+ [ 4, 3, 1, 2] ]
36
+ @problem = @solution.collect { |l| l.clone }
37
+ }
38
+ context('very simple') {
39
+ setup { @problem[0][0] = nil }
40
+ should('still be easy to solve') {
41
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
42
+ }
43
+ }
44
+ context('simple') {
45
+ setup {
46
+ @problem[1][2] = nil
47
+ @problem[2][3] = nil
48
+ }
49
+ should('still be easy to solve') {
50
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
51
+ }
52
+ }
53
+ context('less simple') {
54
+ setup {
55
+ @problem[0][2] = nil
56
+ @problem[1][2] = nil
57
+ @problem[2][3] = nil
58
+ @problem[3][3] = nil
59
+ }
60
+ should('still be easy to solve') {
61
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
62
+ }
63
+ }
64
+ context('complex') {
65
+ setup {
66
+ @problem[0][2] = nil
67
+ @problem[2][2] = nil
68
+ @problem[0][3] = nil
69
+ @problem[3][2] = nil
70
+ @problem[3][1] = nil
71
+ @problem[2][3] = nil
72
+ @problem[2][0] = nil
73
+ @problem[2][3] = nil
74
+ }
75
+ should('still be easy to solve') {
76
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
77
+ }
78
+ }
79
+ }
80
+
81
+ context("And now it's 3x3 time !") {
82
+ context("Easy for a start") {
83
+ setup {
84
+ @problem =
85
+ [ [ 7, nil, nil, 3, nil, nil, nil, 2, nil ],
86
+ [ 8, 4, nil, 2, 6, nil, nil, nil, 5 ],
87
+ [ nil, 5, 2, nil, 1, 9, nil, 8, 7 ],
88
+ [ nil, nil, nil, 9, nil, nil, nil, 3, 2 ],
89
+ [ 4, nil, 1, nil, nil, nil, 7, nil, 6 ],
90
+ [ 9, 2, nil, nil, nil, 6, nil, nil, nil ],
91
+ [ 2, 9, nil, 6, 8, nil, 1, 7, nil ],
92
+ [ 1, nil, nil, nil, 2, 4, nil, 6, 8 ],
93
+ [ nil, 7, nil, nil, nil, 3, nil, nil, 4 ] ]
94
+ @solution =
95
+ [ [ 7, 1, 6, 3, 5, 8, 4, 2, 9 ],
96
+ [ 8, 4, 9, 2, 6, 7, 3, 1, 5 ],
97
+ [ 3, 5, 2, 4, 1, 9, 6, 8, 7 ],
98
+ [ 5, 6, 7, 9, 4, 1, 8, 3, 2 ],
99
+ [ 4, 8, 1, 5, 3, 2, 7, 9, 6 ],
100
+ [ 9, 2, 3, 8, 7, 6, 5, 4, 1 ],
101
+ [ 2, 9, 4, 6, 8, 5, 1, 7, 3 ],
102
+ [ 1, 3, 5, 7, 2, 4, 9, 6, 8 ],
103
+ [ 6, 7, 8, 1, 9, 3, 2, 5, 4 ] ]
104
+ }
105
+ should("be easy to solve") {
106
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
107
+ }
108
+ }
109
+ context("And now a hard one") {
110
+ setup {
111
+ @problem =
112
+ [ [ 9, nil, nil, 3, nil, nil, nil, nil, 6 ],
113
+ [ nil, 8, nil, nil, nil, nil, nil, nil, 7 ],
114
+ [ nil, nil, 5, 2, nil, 8, 9, nil, nil ],
115
+ [ nil, nil, 8, nil, nil, 4, nil, 7, 3 ],
116
+ [ nil, 9, nil, nil, nil, nil, nil, 4, nil ],
117
+ [ 7, 6, nil, 1, nil, nil, 2, nil, nil ],
118
+ [ nil, nil, 7, 8, nil, 1, 4, nil, nil ],
119
+ [ 1, nil, nil, nil, nil, nil, nil, 5, nil ],
120
+ [ 8, nil, nil, nil, nil, 9, nil, nil, 1 ] ]
121
+ @solution =
122
+ [ [ 9, 2, 1, 3, 4, 7, 5, 8, 6 ],
123
+ [ 4, 8, 6, 9, 1, 5, 3, 2, 7 ],
124
+ [ 3, 7, 5, 2, 6, 8, 9, 1, 4 ],
125
+ [ 2, 1, 8, 5, 9, 4, 6, 7, 3 ],
126
+ [ 5, 9, 3, 6, 7, 2, 1, 4, 8 ],
127
+ [ 7, 6, 4, 1, 8, 3, 2, 9, 5 ],
128
+ [ 6, 5, 7, 8, 2, 1, 4, 3, 9 ],
129
+ [ 1, 4, 9, 7, 3, 6, 8, 5, 2 ],
130
+ [ 8, 3, 2, 4, 5, 9, 7, 6, 1 ] ]
131
+ }
132
+ should("be easy to solve") {
133
+ assert_equal @solution, SudokuSolver::Solver.solve(@problem)
134
+ }
135
+ }
136
+ }
137
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sudoku_solver
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michel Belleville
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-26 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: cellular_map
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: A very simple sudoku problems solver
36
+ email: michel.belleville@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/sudoku_solver.rb
52
+ - lib/sudoku_solver/grid.rb
53
+ - lib/sudoku_solver/solver.rb
54
+ - test/helper.rb
55
+ - test/test_grid.rb
56
+ - test/test_solver.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/Bastes/SudokuSolver
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A pretty simple sudoku solver
85
+ test_files:
86
+ - test/test_solver.rb
87
+ - test/test_grid.rb
88
+ - test/helper.rb