hanoi-jane 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Guardfile +70 -0
- data/LICENSE.md +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/hanoi +4 -0
- data/bin/setup +8 -0
- data/hanoi-jane.gemspec +33 -0
- data/lib/hanoi/jane.rb +24 -0
- data/lib/hanoi/jane/cli.rb +49 -0
- data/lib/hanoi/jane/constrained_towers.rb +36 -0
- data/lib/hanoi/jane/matrix.rb +80 -0
- data/lib/hanoi/jane/towers.rb +89 -0
- data/lib/hanoi/jane/version.rb +5 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c49544f6ba3f25f061e296b69216675bdca3383b
|
4
|
+
data.tar.gz: 982b160b51f1615f11ec0aa15551327470909bb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c65f08629c3616617e11ce36a6c914dbc20d04108187714e3e29673fd2ba037859f68780a04f9072d7d51a0f867d038951ac0fa621ed62b44e6381cfd2d4344
|
7
|
+
data.tar.gz: b15f6ba0c8fe1e0c9a8e9fbe5bbc18bc7fd4ace2f0dfbc950794d364d9eeef2bfd5b5d015e99baf09264862b43aca0b4952f048311499e052aaa2bb3980afcab
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at github@orgraphone.org. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
## Copyright (c) 2017 pikesley
|
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,38 @@
|
|
1
|
+
[![Build Status](http://img.shields.io/travis/pikesley/hanoi-jane.svg?style=flat-square)](https://travis-ci.org/pikesley/hanoi-jane)
|
2
|
+
[![Dependency Status](http://img.shields.io/gemnasium/pikesley/hanoi-jane.svg?style=flat-square)](https://gemnasium.com/pikesley/hanoi-jane)
|
3
|
+
[![Coverage Status](http://img.shields.io/coveralls/pikesley/hanoi-jane.svg?style=flat-square)](https://coveralls.io/r/pikesley/hanoi-jane)
|
4
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/pikesley/hanoi-jane.svg?style=flat-square)](https://codeclimate.com/github/pikesley/hanoi-jane)
|
5
|
+
[![Gem Version](http://img.shields.io/gem/v/hanoi-jane.svg?style=flat-square)](https://rubygems.org/gems/hanoi-jane)
|
6
|
+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://pikesley.mit-license.org)
|
7
|
+
|
8
|
+
# Hanoi Jane
|
9
|
+
|
10
|
+
_Counting in binary to solve the Towers of Hanoi_
|
11
|
+
|
12
|
+
## Surely there are easier ways to do this?
|
13
|
+
|
14
|
+
Yes, there are. This is very much a Solved Problem. However, I was inspired to implement this solution after watching [3 Blue 1 Brown](https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw)'s [fascinating video](https://www.youtube.com/watch?v=2SUvWfNJSsM), in which Grant relates the Towers Of Hanoi to the Rhythm Of Counting In Binary:
|
15
|
+
|
16
|
+
[![Screenshot](https://i.imgur.com/mXsl57y.png)](https://www.youtube.com/watch?v=2SUvWfNJSsM)
|
17
|
+
|
18
|
+
## Running it
|
19
|
+
|
20
|
+
bundle
|
21
|
+
bundle exec rake
|
22
|
+
bundle exec ./bin/hanoi console
|
23
|
+
|
24
|
+
## Constrained version
|
25
|
+
|
26
|
+
There is a [constrained variant of the problem](https://www.youtube.com/watch?v=bdMfjfT0lKk), with the restriction that a disc may only move to an adjacent stack. I've also implemented the solution for this (which maps to the Rhythm Of Counting In Ternary) - you can run this with
|
27
|
+
|
28
|
+
hanoi --constrained
|
29
|
+
|
30
|
+
## pHAT
|
31
|
+
|
32
|
+
In order to over-engineer this, I've wrapped a [very thin Flask app](https://github.com/pikesley/pHAT-REST) around the [MicroDot pHAT](https://shop.pimoroni.com/products/microdot-phat). Try
|
33
|
+
|
34
|
+
hanoi phat --phat <address_of_your_pi> --constrained
|
35
|
+
|
36
|
+
to watch this all play out on the pHAT:
|
37
|
+
|
38
|
+
<blockquote class="instagram-media" data-instgrm-version="7" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"> <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:50.0% 0; text-align:center; width:100%;"> <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div></div><p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;"><a href="https://www.instagram.com/p/Bbrs3diAw1B/" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;" target="_blank">A post shared by Sam (@pikesley)</a> on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2017-11-19T16:04:50+00:00">Nov 19, 2017 at 8:04am PST</time></p></div></blockquote> <script async defer src="//platform.instagram.com/en_US/embeds.js"></script>
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hanoi/jane"
|
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__)
|
data/bin/hanoi
ADDED
data/bin/setup
ADDED
data/hanoi-jane.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hanoi/jane/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hanoi-jane'
|
8
|
+
spec.version = Hanoi::Jane::VERSION
|
9
|
+
spec.authors = ['pikesley']
|
10
|
+
spec.email = ['sam.pikesley@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Solve the Towers of Hanoi}
|
13
|
+
spec.description = %q{by counting in base 2 or 3}
|
14
|
+
spec.homepage = 'http://pikesley.org'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
28
|
+
spec.add_development_dependency 'guard', '~> 2.14'
|
29
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.7'
|
30
|
+
|
31
|
+
spec.add_dependency 'thor', '~> 0.19'
|
32
|
+
spec.add_dependency 'httparty', '~> 0.15'
|
33
|
+
end
|
data/lib/hanoi/jane.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
require 'hanoi/jane/version'
|
5
|
+
require 'hanoi/jane/towers'
|
6
|
+
require 'hanoi/jane/constrained_towers'
|
7
|
+
require 'hanoi/jane/matrix'
|
8
|
+
|
9
|
+
module Hanoi
|
10
|
+
module Jane
|
11
|
+
def self.hit_phat towers, phat
|
12
|
+
url = "http://#{phat}/lights"
|
13
|
+
payload = {
|
14
|
+
matrix: towers.matrix
|
15
|
+
}
|
16
|
+
headers = {
|
17
|
+
'Content-Type' => 'application/json',
|
18
|
+
'Accept' => 'application/json'
|
19
|
+
}
|
20
|
+
|
21
|
+
HTTParty.patch(url, body: payload.to_json, headers: headers)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'hanoi/jane'
|
2
|
+
|
3
|
+
module Hanoi
|
4
|
+
module Jane
|
5
|
+
class CLI < ::Thor
|
6
|
+
desc 'version', 'Print hanoi version'
|
7
|
+
def version
|
8
|
+
puts "hanoi version #{VERSION}"
|
9
|
+
end
|
10
|
+
map %w(-v --version) => :version
|
11
|
+
|
12
|
+
desc 'phat', "Solve the towers against the pHAT's webserver"
|
13
|
+
option :phat, type: :string, required: true
|
14
|
+
option :constrained, type: :boolean
|
15
|
+
option :interval, type: :numeric, default: 0.3
|
16
|
+
|
17
|
+
def phat
|
18
|
+
towers = Towers.new 5
|
19
|
+
if options[:constrained]
|
20
|
+
towers = ConstrainedTowers.new 5
|
21
|
+
end
|
22
|
+
|
23
|
+
until towers.solved do
|
24
|
+
Hanoi::Jane.hit_phat towers, options[:phat]
|
25
|
+
towers.move
|
26
|
+
sleep options[:interval]
|
27
|
+
end
|
28
|
+
Hanoi::Jane.hit_phat towers, options[:phat]
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'console', "Solve the towers one the console"
|
32
|
+
option :discs, type: :numeric, default: 3
|
33
|
+
option :constrained, type: :boolean
|
34
|
+
def console
|
35
|
+
towers = Towers.new options[:discs]
|
36
|
+
if options[:constrained]
|
37
|
+
towers = ConstrainedTowers.new options[:discs]
|
38
|
+
end
|
39
|
+
until towers.solved do
|
40
|
+
puts towers
|
41
|
+
towers.move
|
42
|
+
end
|
43
|
+
puts towers
|
44
|
+
|
45
|
+
puts "%d moves to solve for %d discs" % [towers.count, options[:discs]]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Hanoi
|
2
|
+
module Jane
|
3
|
+
class ConstrainedTowers < Towers
|
4
|
+
def initialize discs
|
5
|
+
super
|
6
|
+
@base = 3
|
7
|
+
|
8
|
+
@directions = {}
|
9
|
+
@stacks[0].each do |disc|
|
10
|
+
@directions[disc] = :right
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def ternary
|
15
|
+
rebased
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_stack
|
19
|
+
case @source
|
20
|
+
when 0
|
21
|
+
@directions[@disc] = :right
|
22
|
+
return 1
|
23
|
+
when 2
|
24
|
+
@directions[@disc] = :left
|
25
|
+
return 1
|
26
|
+
when 1
|
27
|
+
if @directions[@disc] == :right
|
28
|
+
return 2
|
29
|
+
else
|
30
|
+
return 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Hanoi
|
2
|
+
module Jane
|
3
|
+
class Matrix < Array
|
4
|
+
def initialize towers
|
5
|
+
@stacks = towers.stacks
|
6
|
+
@digits = towers.rebased
|
7
|
+
|
8
|
+
7.times do
|
9
|
+
self.push [0] * 45
|
10
|
+
end
|
11
|
+
|
12
|
+
offset = 0
|
13
|
+
@stacks.each do |stack|
|
14
|
+
count = 0
|
15
|
+
stack.each do |disc|
|
16
|
+
shim = ((5 - (disc + 1)) / 2).round
|
17
|
+
(disc + 1).times do |i|
|
18
|
+
self[6 - count][i + offset + shim] = 1
|
19
|
+
end
|
20
|
+
count += 1
|
21
|
+
end
|
22
|
+
offset += 8
|
23
|
+
end
|
24
|
+
|
25
|
+
@bit_offset = 24
|
26
|
+
@bit_side = :right
|
27
|
+
@digits.chars.each do |bit|
|
28
|
+
digit bit
|
29
|
+
|
30
|
+
if @bit_side == :right
|
31
|
+
@bit_side = :left
|
32
|
+
@bit_offset += 8
|
33
|
+
else
|
34
|
+
@bit_side = :right
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def digit value
|
40
|
+
digits = {
|
41
|
+
0 => [
|
42
|
+
[1, 1, 1],
|
43
|
+
[1, 0, 1],
|
44
|
+
[1, 1, 1]
|
45
|
+
],
|
46
|
+
|
47
|
+
1 => [
|
48
|
+
[0, 1, 0],
|
49
|
+
[0, 1, 0],
|
50
|
+
[0, 1, 0]
|
51
|
+
],
|
52
|
+
|
53
|
+
2 => [
|
54
|
+
[1, 1, 0],
|
55
|
+
[0, 1, 0],
|
56
|
+
[0, 1, 1]
|
57
|
+
]
|
58
|
+
}
|
59
|
+
|
60
|
+
@column = @bit_offset
|
61
|
+
@row = 0
|
62
|
+
if @bit_side == :right
|
63
|
+
@column += 2
|
64
|
+
@row = 4
|
65
|
+
end
|
66
|
+
|
67
|
+
insert digits[value.to_i]
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def insert grid
|
72
|
+
3.times do |i|
|
73
|
+
3.times do |j|
|
74
|
+
self[@row + i][@column + j] = grid[i][j]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Hanoi
|
2
|
+
module Jane
|
3
|
+
class Towers
|
4
|
+
attr_reader :count, :stacks
|
5
|
+
|
6
|
+
def initialize discs
|
7
|
+
@discs = discs
|
8
|
+
@count = 0
|
9
|
+
@base = 2
|
10
|
+
@stacks = [(0...discs).to_a.reverse, [], []]
|
11
|
+
end
|
12
|
+
|
13
|
+
def move
|
14
|
+
@disc = Towers.diff Towers.rebase(@count, @base, @discs),
|
15
|
+
Towers.rebase(@count += 1, @base, @discs)
|
16
|
+
@source = find_disc
|
17
|
+
|
18
|
+
@stacks[find_stack].push @stacks[@source].pop
|
19
|
+
end
|
20
|
+
|
21
|
+
def binary
|
22
|
+
rebased
|
23
|
+
end
|
24
|
+
|
25
|
+
def rebased
|
26
|
+
Towers.rebase @count, @base, @discs
|
27
|
+
end
|
28
|
+
|
29
|
+
def solved
|
30
|
+
rebased.chars.all? { |digit| digit.to_i == @base - 1 }
|
31
|
+
end
|
32
|
+
|
33
|
+
def matrix
|
34
|
+
Matrix.new self
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_disc
|
38
|
+
@stacks.each_with_index do |stack, index|
|
39
|
+
return index if stack.index @disc
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def find_stack #disc, source, stacks
|
44
|
+
# if the next stack is empty, move there
|
45
|
+
if @stacks[(@source + 1) % 3] == []
|
46
|
+
return (@source + 1) % 3
|
47
|
+
end
|
48
|
+
|
49
|
+
# if the next stack has a smaller top disc than our disc, go one more over
|
50
|
+
if @stacks[(@source + 1) % 3][-1] < @disc
|
51
|
+
return (@source + 2) % 3
|
52
|
+
end
|
53
|
+
|
54
|
+
# default to the next one
|
55
|
+
return (@source + 1) % 3
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
{
|
60
|
+
stacks: @stacks,
|
61
|
+
count: rebased
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_s
|
66
|
+
s = ''
|
67
|
+
@stacks.each do |stack|
|
68
|
+
s += stack.to_s
|
69
|
+
s += "\n"
|
70
|
+
end
|
71
|
+
s += '---'
|
72
|
+
|
73
|
+
s
|
74
|
+
end
|
75
|
+
|
76
|
+
def Towers.rebase value, base, width
|
77
|
+
'%0*d' % [width, value.to_s(base)]
|
78
|
+
end
|
79
|
+
|
80
|
+
def Towers.diff first, second
|
81
|
+
first.chars.reverse.each_with_index do |bit, index|
|
82
|
+
if bit < second.chars.reverse[index]
|
83
|
+
return index
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hanoi-jane
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pikesley
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-22 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.14'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.14'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.7'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.7'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: thor
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.19'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.19'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: httparty
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.15'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.15'
|
125
|
+
description: by counting in base 2 or 3
|
126
|
+
email:
|
127
|
+
- sam.pikesley@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- Guardfile
|
138
|
+
- LICENSE.md
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/hanoi
|
143
|
+
- bin/setup
|
144
|
+
- hanoi-jane.gemspec
|
145
|
+
- lib/hanoi/jane.rb
|
146
|
+
- lib/hanoi/jane/cli.rb
|
147
|
+
- lib/hanoi/jane/constrained_towers.rb
|
148
|
+
- lib/hanoi/jane/matrix.rb
|
149
|
+
- lib/hanoi/jane/towers.rb
|
150
|
+
- lib/hanoi/jane/version.rb
|
151
|
+
homepage: http://pikesley.org
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.6.8
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: Solve the Towers of Hanoi
|
175
|
+
test_files: []
|