kudosu 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dcd5f234f4d11bf92d05c11e677f410fbbc42a8bc9fe66285a8f358ef03443cf
4
+ data.tar.gz: b588e3b09170e3e475a039f4312b84dcef017216a2657469460736612eaba431
5
+ SHA512:
6
+ metadata.gz: adf64a762b560d2a983fc9d60fba77ddf5bd6a503f99cc9cad35fbcfb5f3e7d36bc95e6d43c9df122381e9109e3ce4f955ec30c52332fc5a279406f8fbbe59f1
7
+ data.tar.gz: 838032a7c1b0a5b03b1f20a271af052105055e5729084c0bad0a5024d5e6b38dee239c03c9292c05da6467c5dff9637ca6ddf2c1d66916b7e38ac00f727fa200
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in kudosu.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kudosu (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ kudosu!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.8)
33
+
34
+ BUNDLED WITH
35
+ 1.16.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Macario
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Kudosu
2
+
3
+ Sudoku generator and solver
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install kudosu
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Generate a sudoku puzzle
14
+
15
+ ```
16
+ $ kudosu generate
17
+ ```
18
+
19
+ Generate a JSON representation of a sudoku puzzle
20
+
21
+ ```
22
+ $ kudosu generate-json
23
+ ```
24
+
25
+ Solve a JSON representation of a sudoku puzzle
26
+
27
+ ```
28
+ $ kudosu generate-json | kudosu solve
29
+ ```
30
+
31
+
32
+ ## TODO
33
+
34
+ - optimize
35
+ - ncurses interface
36
+
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maca/kudosu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kudosu"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kudosu"
5
+ require "json"
6
+
7
+
8
+ def print_usage
9
+ puts <<-STR
10
+ Usage: kudosu COMMAND
11
+ Commands:
12
+ generate prints a generated sudoku board
13
+ generate-json prints a json representation of a generated sudoku board
14
+ solve NUMBERS solves a puzzle, number should be a json array of sudoku numbers
15
+ STR
16
+
17
+ exit
18
+ end
19
+
20
+ command = ARGV.shift
21
+
22
+
23
+ print_usage unless %w(generate generate-json solve).include?(command)
24
+
25
+
26
+ case command
27
+ when "generate"
28
+ print_usage unless ARGV.none?
29
+ Kudosu.generate.print
30
+
31
+ when "generate-json"
32
+ print_usage unless ARGV.none?
33
+ puts Kudosu.generate.values.to_json
34
+
35
+ when "solve"
36
+ numbers = JSON.parse(STDIN.tty? ? ARGV.first : $stdin.read)
37
+ Kudosu.solve(numbers).print
38
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "kudosu/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kudosu"
8
+ spec.version = Kudosu::VERSION
9
+ spec.authors = ["Macario"]
10
+ spec.email = ["maca@aelita.io"]
11
+
12
+ spec.summary = %q{Sudoku solver and generator}
13
+ spec.description = %q{Sudoku solver and generator}
14
+ spec.homepage = "https://github.com/maca/kudosu"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "bin"
32
+ spec.executables = "kudosu"
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.8"
38
+ end
@@ -0,0 +1,17 @@
1
+ require "kudosu/version"
2
+ require "kudosu/markup_range_utils"
3
+ require "kudosu/board"
4
+ require "kudosu/cell"
5
+ require "kudosu/printer"
6
+
7
+ module Kudosu
8
+ module_function
9
+
10
+ def solve(values)
11
+ Board.new(values).solve
12
+ end
13
+
14
+ def generate
15
+ solve([[nil] * 9] * 9).shuffle
16
+ end
17
+ end
@@ -0,0 +1,84 @@
1
+ module Kudosu
2
+ class Board
3
+ attr_accessor :cells
4
+
5
+ def initialize(ary)
6
+ self.cells = ary.flatten.map { |val| Cell.new(val) }
7
+ end
8
+
9
+ def solve
10
+ board = attempt_solution || fork_board(values) until board
11
+ self.cells = board.cells
12
+ self
13
+ end
14
+
15
+ def values
16
+ cells.map(&:value)
17
+ end
18
+
19
+ def print
20
+ Printer.new(self).print
21
+ end
22
+
23
+ def rows
24
+ cells.each_slice(9).to_a
25
+ end
26
+
27
+ def columns
28
+ rows.transpose
29
+ end
30
+
31
+ def boxes
32
+ sliced_cols = rows.map { |row| row.each_slice(3).to_a }.transpose
33
+ sliced_cols.flatten.each_slice(9).to_a
34
+ end
35
+
36
+ def shuffle
37
+ boxes.each { |box| box.shuffle.take(rand(5..7)).each(&:reset) }
38
+ self
39
+ end
40
+
41
+ def solved?
42
+ cells.all?(&:solved?)
43
+ end
44
+
45
+ def valid?
46
+ MarkupRangeUtils.ranges_valid?(markup_ranges)
47
+ end
48
+
49
+ protected
50
+
51
+ def unsolved_cells
52
+ cells.reject(&:solved?)
53
+ end
54
+
55
+ def attempt_solution
56
+ cells.each(&:reset_markup)
57
+
58
+ until solved?
59
+ MarkupRangeUtils.update_ranges_markup(markup_ranges)
60
+ return if cells.none?(&:solve)
61
+ end
62
+
63
+ self
64
+ end
65
+
66
+ private
67
+
68
+ def markup_ranges
69
+ @ranges ||= rows + columns + boxes
70
+ end
71
+
72
+ def fork_board(values)
73
+ board = Board.new(values)
74
+
75
+ board.unsolved_cells.each do |cell|
76
+ return board if board.attempt_solution
77
+ cell.pick_random
78
+ break unless board.valid?
79
+ end
80
+
81
+ fork_board(values)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,34 @@
1
+ module Kudosu
2
+ class Cell
3
+ attr_accessor :value, :markup
4
+
5
+ def initialize(value)
6
+ self.value = value
7
+ end
8
+
9
+ def solved?
10
+ !value.nil?
11
+ end
12
+
13
+ def solve
14
+ self.value = markup.pop if markup.one? unless solved?
15
+ end
16
+
17
+ def remove_from_markup(values)
18
+ self.markup -= values
19
+ end
20
+
21
+ def pick_random
22
+ self.value = markup.sample
23
+ markup.delete(value)
24
+ end
25
+
26
+ def reset
27
+ self.value = nil
28
+ end
29
+
30
+ def reset_markup
31
+ self.markup = (1..9).to_a
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ module Kudosu
2
+ module MarkupRangeUtils
3
+ module_function
4
+
5
+ def range_values(range)
6
+ range.map(&:value).compact
7
+ end
8
+
9
+ def update_ranges_markup(ranges)
10
+ ranges.each do |range|
11
+ values = range_values(range)
12
+ range.each { |cell| cell.remove_from_markup(values) }
13
+ end
14
+ end
15
+
16
+ def ranges_valid?(ranges)
17
+ ranges.all? do |range|
18
+ values = range_values(range)
19
+ values.uniq == values
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module Kudosu
2
+ class Printer
3
+ attr_accessor :board
4
+
5
+ def initialize(board)
6
+ self.board = board
7
+ end
8
+
9
+ def print
10
+ board.rows.each do |row|
11
+ print_separation
12
+ puts '| ' + row.map{ |cell| cell.value || " " }.join(' | ') + ' |'
13
+ end
14
+
15
+ print_separation
16
+ end
17
+
18
+ private
19
+
20
+ def print_separation
21
+ puts '+' + '---+' * 9
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Kudosu
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kudosu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Macario
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-10 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description: Sudoku solver and generator
56
+ email:
57
+ - maca@aelita.io
58
+ executables:
59
+ - kudosu
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/kudosu
71
+ - bin/setup
72
+ - kudosu.gemspec
73
+ - lib/kudosu.rb
74
+ - lib/kudosu/board.rb
75
+ - lib/kudosu/cell.rb
76
+ - lib/kudosu/markup_range_utils.rb
77
+ - lib/kudosu/printer.rb
78
+ - lib/kudosu/version.rb
79
+ homepage: https://github.com/maca/kudosu
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.7
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Sudoku solver and generator
103
+ test_files: []