kudo 0.2.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
+ SHA256:
3
+ metadata.gz: 9a70a096d1feca4a8d023437c21c9964582bf5d40dde850fb1af9ca9229a4152
4
+ data.tar.gz: 5dfb3cfe1fb74ff04349bfe177a01650bb722d2fa379a075798e6aa4e6bb6a8a
5
+ SHA512:
6
+ metadata.gz: ba48e564feb52227c126e2b51b6b472e74517b96bf88bcc8a495014e0ef5bc248750efc50e953c9cdfb2016987f9a3a5dc5db875e1bb46dbb72e1a38b861ad1d
7
+ data.tar.gz: af589d27f6881182d70e1b2c20f8728d4ea37f5d7510a15322612ab3ae8e1ece515c8b627565e2cd09596736bcb5858a4948fcb1685d5d07013a4952f6759879
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # rubocop
14
+
15
+ .rubocop.yml
16
+ .rubocop_todo.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kudo.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kudo (0.1.0)
5
+ thor
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ aruba (2.0.0)
11
+ bundler (>= 1.17, < 3.0)
12
+ childprocess (>= 2.0, < 5.0)
13
+ contracts (>= 0.16.0, < 0.18.0)
14
+ cucumber (>= 4.0, < 8.0)
15
+ rspec-expectations (~> 3.4)
16
+ thor (~> 1.0)
17
+ builder (3.2.4)
18
+ childprocess (4.1.0)
19
+ contracts (0.16.1)
20
+ cucumber (7.0.0)
21
+ builder (~> 3.2, >= 3.2.4)
22
+ cucumber-core (~> 10.0, >= 10.0.1)
23
+ cucumber-create-meta (~> 6.0, >= 6.0.1)
24
+ cucumber-cucumber-expressions (~> 12.1, >= 12.1.1)
25
+ cucumber-gherkin (~> 20.0, >= 20.0.1)
26
+ cucumber-html-formatter (~> 16.0, >= 16.0.1)
27
+ cucumber-messages (~> 17.0, >= 17.0.1)
28
+ cucumber-wire (~> 6.0, >= 6.0.1)
29
+ diff-lcs (~> 1.4, >= 1.4.4)
30
+ mime-types (~> 3.3, >= 3.3.1)
31
+ multi_test (~> 0.1, >= 0.1.2)
32
+ sys-uname (~> 1.2, >= 1.2.2)
33
+ cucumber-core (10.0.1)
34
+ cucumber-gherkin (~> 20.0, >= 20.0.1)
35
+ cucumber-messages (~> 17.0, >= 17.0.1)
36
+ cucumber-tag-expressions (~> 3.0, >= 3.0.1)
37
+ cucumber-create-meta (6.0.1)
38
+ cucumber-messages (~> 17.0, >= 17.0.1)
39
+ sys-uname (~> 1.2, >= 1.2.2)
40
+ cucumber-cucumber-expressions (12.1.2)
41
+ cucumber-gherkin (20.0.1)
42
+ cucumber-messages (~> 17.0, >= 17.0.1)
43
+ cucumber-html-formatter (16.0.1)
44
+ cucumber-messages (~> 17.0, >= 17.0.1)
45
+ cucumber-messages (17.0.1)
46
+ cucumber-tag-expressions (3.0.1)
47
+ cucumber-wire (6.1.0)
48
+ cucumber-core (~> 10.0, >= 10.0.1)
49
+ cucumber-cucumber-expressions (~> 12.1, >= 12.1.1)
50
+ cucumber-messages (~> 17.0, >= 17.0.1)
51
+ diff-lcs (1.4.4)
52
+ ffi (1.15.3)
53
+ mime-types (3.3.1)
54
+ mime-types-data (~> 3.2015)
55
+ mime-types-data (3.2021.0704)
56
+ multi_test (0.1.2)
57
+ rake (13.0.6)
58
+ rspec (3.10.0)
59
+ rspec-core (~> 3.10.0)
60
+ rspec-expectations (~> 3.10.0)
61
+ rspec-mocks (~> 3.10.0)
62
+ rspec-core (3.10.1)
63
+ rspec-support (~> 3.10.0)
64
+ rspec-expectations (3.10.1)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.10.0)
67
+ rspec-mocks (3.10.2)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.10.0)
70
+ rspec-support (3.10.2)
71
+ sys-uname (1.2.2)
72
+ ffi (~> 1.1)
73
+ thor (1.1.0)
74
+
75
+ PLATFORMS
76
+ x86_64-darwin-19
77
+
78
+ DEPENDENCIES
79
+ aruba
80
+ cucumber
81
+ kudo!
82
+ rake (~> 13.0)
83
+ rspec (~> 3.0)
84
+
85
+ BUNDLED WITH
86
+ 2.2.25
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Nguyen Viet Tuang
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.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Kudo
2
+
3
+ "Yet another sudoku solver" - *some one famous, 2021*
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kudo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kudo
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tuang3142/sudoku.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "kudo"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/exe/kudo ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kudo/cli"
4
+
5
+ Kudo::CLI.start
data/kudo.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kudo/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kudo"
7
+ spec.version = Kudo::VERSION
8
+ spec.authors = ["Tuan Nguyen"]
9
+ spec.email = ["hi@tuanguyen.com"]
10
+
11
+ spec.summary = "Yet another sudoku solver"
12
+ spec.description = "A command-line interface to solve sudoku quickly."
13
+ spec.homepage = "https://github.com/tuang3142/sudoku"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "thor"
32
+ spec.add_development_dependency "cucumber"
33
+ spec.add_development_dependency "aruba"
34
+ end
data/lib/kudo.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "kudo/version"
4
+ require_relative "kudo/ping"
5
+ require_relative "kudo/solver"
6
+ require_relative "kudo/converter"
7
+ require_relative "kudo/operator"
8
+ require_relative "kudo/validator"
9
+
10
+ module Kudo
11
+ module_function
12
+
13
+ def solve(sudoku_string)
14
+ grid = Converter.convert_to_array(sudoku_string)
15
+ raise "Sudoku is invalid!" unless Validator.check_valid(grid)
16
+
17
+ Converter.convert_to_string(Solver.solve(grid))
18
+ end
19
+ end
data/lib/kudo/cli.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "kudo"
5
+
6
+ module Kudo
7
+ class CLI < Thor
8
+ desc "ping", "pong!"
9
+ def ping
10
+ puts Kudo.ping
11
+ end
12
+
13
+ desc "solve INPUT", "Solves the sudoku given in the input file"
14
+ def solve(file_name)
15
+ sudoku = File.read(file_name)
16
+
17
+ puts Kudo.solve(sudoku)
18
+ rescue StandardError => e
19
+ puts e
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kudo
4
+ class Converter
5
+ def self.convert_to_array(sudoku_string)
6
+ sudoku_string
7
+ .split("")
8
+ .select { |char| "0" <= char && char <= "9" }
9
+ .map(&:to_i)
10
+ .each_slice(9)
11
+ .to_a
12
+ end
13
+
14
+ def self.convert_to_string(sudoku_grid)
15
+ sudoku_grid
16
+ .map { |row| row.join(" ") }
17
+ .join("\n")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Kudo::Operator
4
+ class << self
5
+ def get_empty_cells(sudoku)
6
+ empty_cells = []
7
+ 9.times do |i|
8
+ 9.times do |j|
9
+ empty_cells.append([i, j]) if sudoku[i][j].zero?
10
+ end
11
+ end
12
+
13
+ empty_cells
14
+ end
15
+
16
+ def get_row(sudoku, i)
17
+ sudoku[i]
18
+ end
19
+
20
+ def get_column(sudoku, j)
21
+ sudoku.map { |row| row[j] }
22
+ end
23
+
24
+ def get_subgrid(sudoku, i, j)
25
+ row = i / 3 * 3
26
+ col = j / 3 * 3
27
+ subgrid = []
28
+ (row..row + 2).each do |r|
29
+ (col..col + 2).each do |c|
30
+ subgrid << sudoku[r][c]
31
+ end
32
+ end
33
+ subgrid
34
+ end
35
+ end
36
+ end
data/lib/kudo/ping.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kudo
4
+ module_function
5
+
6
+ def ping
7
+ "pong!"
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kudo
4
+ class Solver
5
+ class << self
6
+ def solve(sudoku)
7
+ empty_cells = Operator.get_empty_cells(sudoku)
8
+ try_solve(sudoku, empty_cells)
9
+ end
10
+
11
+ private
12
+
13
+ def try_solve(sudoku, empty_cells)
14
+ return sudoku if empty_cells.empty?
15
+
16
+ i, j = empty_cells.last
17
+ (1..9).each do |num|
18
+ next if Operator.get_row(sudoku, i).include?(num) ||
19
+ Operator.get_column(sudoku, j).include?(num) ||
20
+ Operator.get_subgrid(sudoku, i, j).include?(num)
21
+ sudoku[i][j] = num
22
+ empty_cells.pop
23
+ result = try_solve(sudoku, empty_cells)
24
+ return result if result
25
+ sudoku[i][j] = 0
26
+ empty_cells.append([i, j])
27
+ end
28
+
29
+ nil
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Kudo
6
+ class Validator
7
+ class << self
8
+ def check_solved(sudoku)
9
+ check_complete(sudoku) && check_valid(sudoku)
10
+ end
11
+
12
+ def check_complete(sudoku)
13
+ Operator.get_empty_cells(sudoku).empty?
14
+ end
15
+
16
+ def check_valid(sudoku)
17
+ check_rows(sudoku) && check_columns(sudoku) && check_subgrids(sudoku)
18
+ end
19
+
20
+ private
21
+
22
+ SUDOKU_RANGE = (0..8)
23
+
24
+ def check_rows(sudoku)
25
+ SUDOKU_RANGE.all? { |i| check_unique(Operator.get_row(sudoku, i)) }
26
+ end
27
+
28
+ def check_columns(sudoku)
29
+ SUDOKU_RANGE.all? { |i| check_unique(Operator.get_column(sudoku, i)) }
30
+ end
31
+
32
+ def check_subgrids(sudoku)
33
+ SUDOKU_RANGE.all? do |i|
34
+ SUDOKU_RANGE.all? do |j|
35
+ check_unique(Operator.get_subgrid(sudoku, i, j))
36
+ end
37
+ end
38
+ end
39
+
40
+ def check_unique(array)
41
+ non_zero_arr = array.reject(&:zero?)
42
+ non_zero_arr.size == non_zero_arr.to_set.size
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kudo
4
+ VERSION = "0.2.1"
5
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kudo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Tuan Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cucumber
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A command-line interface to solve sudoku quickly.
56
+ email:
57
+ - hi@tuanguyen.com
58
+ executables:
59
+ - kudo
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/kudo
73
+ - kudo.gemspec
74
+ - lib/kudo.rb
75
+ - lib/kudo/cli.rb
76
+ - lib/kudo/converter.rb
77
+ - lib/kudo/operator.rb
78
+ - lib/kudo/ping.rb
79
+ - lib/kudo/solver.rb
80
+ - lib/kudo/validator.rb
81
+ - lib/kudo/version.rb
82
+ homepage: https://github.com/tuang3142/sudoku
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org
87
+ homepage_uri: https://github.com/tuang3142/sudoku
88
+ source_code_uri: https://github.com/tuang3142/sudoku
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.4.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.6.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Yet another sudoku solver
109
+ test_files: []