namero 0.0.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: 0d21908e8458c2569f1584e9e3f8b55ffd5907abb713ede832d1432d2ffd6df2
4
+ data.tar.gz: 11d343b7e82c3bc8f679e8f60bf0eaed1986ea045b8681791da495b2d15bc459
5
+ SHA512:
6
+ metadata.gz: 6c6353da77e5906617a5fdbe110490ec4006d1cb1ab1bd171f6ff5ab06a9ddd2d2cfbc4d21dab3c7a1000b5336c0f11512bbb6da4ed68f4be225e9f37ceb803b
7
+ data.tar.gz: 64caf28995793c5a853d90f4a4ade297a761fc5b3ce10813ae3d1b215fe11e30b6e403b9cb02c93689cc9a09eb6d16cbe0ce1d9142841c37e5053c1522d3bf11
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
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 namero.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ # Namero
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/namero`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'namero'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install namero
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/namero.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.libs << 'test'
8
+ test.test_files = Dir['test/**/test_*.rb']
9
+ test.verbose = true
10
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "namero"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,9 @@
1
+ require "namero/version"
2
+ require 'namero/board'
3
+ require 'namero/solver'
4
+ require 'namero/value'
5
+
6
+ module Namero
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,101 @@
1
+ module Namero
2
+ class Board
3
+ attr_reader :n
4
+
5
+ # n: Integer
6
+ # values: Array<Integer>
7
+ def initialize(n:, values: Array.new(n**2))
8
+ raise ArgumentError if values.size != n**2
9
+ @n = n
10
+ @values = values
11
+ end
12
+
13
+ # x, y: 0 based index
14
+ # type: :single, :row, :column, :block, :index
15
+ def []=(idx, type = :index, value)
16
+ case type
17
+ when :index
18
+ @values[idx] = value
19
+ when :row
20
+ start = (idx / n) * n
21
+ n.times do |i|
22
+ @values[start+i] = value[i]
23
+ end
24
+ when :column
25
+ x = idx % n
26
+ n.times do |i|
27
+ @values[i * n + x] = value[i]
28
+ end
29
+ when :block
30
+ x = idx % n
31
+ y = idx / n
32
+ start_x = x / root_n * root_n
33
+ start_y = y / root_n * root_n
34
+
35
+ value_idx = 0
36
+ root_n.times do |y_offset|
37
+ root_n.times do |x_offset|
38
+
39
+ self[start_x + x_offset + (start_y + y_offset) * n] = value[value_idx]
40
+ value_idx += 1
41
+ end
42
+ end
43
+ else
44
+ raise "Unknown type: #{type}"
45
+ end
46
+ end
47
+
48
+ def [](idx, type = :index)
49
+ case type
50
+ when :index
51
+ @values[idx]
52
+ when :row
53
+ start = (idx / n ) * n
54
+ @values[start...start+@n]
55
+ when :column
56
+ x = idx % n
57
+ @values.each_slice(n).map{|row| row[x]}
58
+ when :block
59
+ x = idx % n
60
+ y = idx / n
61
+ start_x = x / root_n * root_n
62
+ start_y = y / root_n * root_n
63
+
64
+ [].tap do |res|
65
+ root_n.times do |y_offset|
66
+ root_n.times do |x_offset|
67
+
68
+ res << self[start_x + x_offset + (start_y + y_offset) * n]
69
+ end
70
+ end
71
+ end
72
+ else
73
+ raise "Unknown type: #{type}"
74
+ end
75
+ end
76
+
77
+ def dup
78
+ values = @values.map(&:dup)
79
+ Board.new(n: @n, values: values)
80
+ end
81
+
82
+ def each_values
83
+ return enum_for(__method__) unless block_given?
84
+ @values.each do |value|
85
+ yield value
86
+ end
87
+ end
88
+
89
+ def complete?
90
+ @values.all? do |v|
91
+ v.value
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def root_n
98
+ Integer.sqrt(n)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,65 @@
1
+ require 'set'
2
+
3
+ module Namero
4
+ class Solver
5
+ def initialize(board)
6
+ @board = board
7
+ @updated_candidate_queue = Set.new
8
+ end
9
+
10
+ def solve
11
+ fill_candidates
12
+ loop do
13
+ idx = @updated_candidate_queue.each.first
14
+ break unless idx
15
+ @updated_candidate_queue.delete idx
16
+ fill_one_candidate(idx)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :board, :updated_candidate_queue
23
+
24
+ def fill_one_candidate(idx)
25
+ v = board[idx]
26
+ return if v.candidates.size != 1
27
+ v.value = v.candidates.first
28
+ fill_candidate_for(v)
29
+ end
30
+
31
+ def fill_candidate_for(v)
32
+ board[v.index, :row].each do |v2|
33
+ unless v2.value
34
+ v2.candidates -= [v.value]
35
+ updated_candidate_queue << v2.index
36
+ end
37
+ end
38
+ board[v.index, :column].each do |v2|
39
+ unless v2.value
40
+ v2.candidates -= [v.value]
41
+ updated_candidate_queue << v2.index
42
+ end
43
+ end
44
+ board[v.index, :block].each do |v2|
45
+ unless v2.value
46
+ v2.candidates -= [v.value]
47
+ updated_candidate_queue << v2.index
48
+ end
49
+ end
50
+ v.candidates = [v.value]
51
+ end
52
+
53
+ def fill_candidates
54
+ (n ** 2).times do |idx|
55
+ v = board[idx]
56
+ next unless v.value
57
+ fill_candidate_for(v)
58
+ end
59
+ end
60
+
61
+ def n
62
+ board.n
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,16 @@
1
+ module Namero
2
+ class Value
3
+ attr_reader :index
4
+ attr_accessor :candidates, :value
5
+
6
+ def initialize(value:, candidates:, index:)
7
+ @value = value
8
+ @candidates = candidates
9
+ @index = index
10
+ end
11
+
12
+ def dup
13
+ self.new(value: value, candidates: candidates.dup)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Namero
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "namero/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "namero"
8
+ spec.version = Namero::VERSION
9
+ spec.authors = ["Masataka Pocke Kuwabara"]
10
+ spec.email = ["kuwabara@pocke.me"]
11
+
12
+ spec.summary = %q{}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/pocke/namero"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.11"
28
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masataka Pocke Kuwabara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-17 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: '5.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.11'
55
+ description: ''
56
+ email:
57
+ - kuwabara@pocke.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/namero.rb
69
+ - lib/namero/board.rb
70
+ - lib/namero/solver.rb
71
+ - lib/namero/value.rb
72
+ - lib/namero/version.rb
73
+ - namero.gemspec
74
+ homepage: https://github.com/pocke/namero
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: ''
97
+ test_files: []