hanoi-jane 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -4
- data/hanoi-jane.gemspec +2 -2
- data/lib/hanoi/jane/cli.rb +5 -8
- data/lib/hanoi/jane/constrained_towers.rb +12 -1
- data/lib/hanoi/jane/matrix.rb +3 -3
- data/lib/hanoi/jane/towers.rb +53 -38
- data/lib/hanoi/jane/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18a7024f33d514778591e76528439a5f1bbfc6c2
|
4
|
+
data.tar.gz: cb2e4809487b082cbb7f10d7a0ad9dc494628005
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e85d9650604ae1bff0ddf45a5bcdf5ee5c1eda42fd423f77af08e7e7a403c1ddf3f6de91345f653e3ca791adc2e70ee6dc75817a91a528783e77a855e293410d
|
7
|
+
data.tar.gz: 440b0ec941455136734c5790e6100469e410290dfd684903ffeb21cbb013337c92dd1fcfd192deb2d981e921a9e4d215ab192c0b183c2d1fea5bc377697fdd88
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
[![Build Status](http://img.shields.io/travis/pikesley/hanoi-jane.svg?style=flat-square)](https://travis-ci.org/pikesley/hanoi-jane)
|
2
2
|
[![Dependency Status](http://img.shields.io/gemnasium/pikesley/hanoi-jane.svg?style=flat-square)](https://gemnasium.com/pikesley/hanoi-jane)
|
3
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
4
|
[![Gem Version](http://img.shields.io/gem/v/hanoi-jane.svg?style=flat-square)](https://rubygems.org/gems/hanoi-jane)
|
6
5
|
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://pikesley.mit-license.org)
|
7
6
|
|
@@ -19,7 +18,8 @@ Yes, there are. This is very much a Solved Problem. However, I was inspired to i
|
|
19
18
|
|
20
19
|
bundle
|
21
20
|
bundle exec rake
|
22
|
-
bundle exec
|
21
|
+
bundle exec rake install
|
22
|
+
hanoi console
|
23
23
|
|
24
24
|
## Constrained version
|
25
25
|
|
@@ -27,12 +27,30 @@ There is a [constrained variant of the problem](https://www.youtube.com/watch?v=
|
|
27
27
|
|
28
28
|
hanoi --constrained
|
29
29
|
|
30
|
+
## API
|
31
|
+
|
32
|
+
To use it in your own code, try something like:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
➔ irb
|
36
|
+
irb(main):001:0> require 'hanoi/jane'
|
37
|
+
=> true
|
38
|
+
irb(main):002:0> towers = Hanoi::Jane::ConstrainedTowers.new 3
|
39
|
+
=> {:stacks=>[[2, 1, 0], [], []], :moves=>0, :flipped=>nil, :ternary=>"000"}
|
40
|
+
irb(main):003:0> towers.each { |state| puts state.inspect }
|
41
|
+
{:stacks=>[[2, 1, 0], [], []], :moves=>0, :flipped=>nil, :ternary=>"000"}
|
42
|
+
{:stacks=>[[2, 1], [0], []], :moves=>1, :flipped=>0, :ternary=>"001"}
|
43
|
+
{:stacks=>[[2, 1], [], [0]], :moves=>2, :flipped=>0, :ternary=>"002"}
|
44
|
+
<snip>
|
45
|
+
```
|
46
|
+
where `flipped` is the disc that was moved last
|
47
|
+
|
30
48
|
## pHAT
|
31
49
|
|
32
50
|
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
51
|
|
34
52
|
hanoi phat --phat <address_of_your_pi> --constrained
|
35
53
|
|
36
|
-
to watch this all play out on the pHAT:
|
54
|
+
to watch this all [play out on the pHAT](https://www.youtube.com/watch?v=PAQY5XtdNO8):
|
37
55
|
|
38
|
-
|
56
|
+
[![Video](https://i.imgur.com/QILZYgx.png)](https://www.youtube.com/watch?v=PAQY5XtdNO8)
|
data/hanoi-jane.gemspec
CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
19
19
|
end
|
20
|
-
spec.bindir = '
|
21
|
-
spec.executables = spec.files.grep(%r{^
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.14'
|
data/lib/hanoi/jane/cli.rb
CHANGED
@@ -20,12 +20,10 @@ module Hanoi
|
|
20
20
|
towers = ConstrainedTowers.new 5
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
towers.each do |state|
|
24
24
|
Hanoi::Jane.hit_phat towers, options[:phat]
|
25
|
-
towers.move
|
26
25
|
sleep options[:interval]
|
27
26
|
end
|
28
|
-
Hanoi::Jane.hit_phat towers, options[:phat]
|
29
27
|
end
|
30
28
|
|
31
29
|
desc 'console', "Solve the towers one the console"
|
@@ -36,13 +34,12 @@ module Hanoi
|
|
36
34
|
if options[:constrained]
|
37
35
|
towers = ConstrainedTowers.new options[:discs]
|
38
36
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
|
38
|
+
towers.each do |state|
|
39
|
+
puts state
|
42
40
|
end
|
43
|
-
puts towers
|
44
41
|
|
45
|
-
puts "%d moves to solve for %d discs" % [towers.
|
42
|
+
puts "%d moves to solve for %d discs" % [towers.total, options[:discs]]
|
46
43
|
end
|
47
44
|
end
|
48
45
|
end
|
data/lib/hanoi/jane/matrix.rb
CHANGED
@@ -11,13 +11,13 @@ module Hanoi
|
|
11
11
|
|
12
12
|
offset = 0
|
13
13
|
@stacks.each do |stack|
|
14
|
-
|
14
|
+
total = 0
|
15
15
|
stack.each do |disc|
|
16
16
|
shim = ((5 - (disc + 1)) / 2).round
|
17
17
|
(disc + 1).times do |i|
|
18
|
-
self[6 -
|
18
|
+
self[6 - total][i + offset + shim] = 1
|
19
19
|
end
|
20
|
-
|
20
|
+
total += 1
|
21
21
|
end
|
22
22
|
offset += 8
|
23
23
|
end
|
data/lib/hanoi/jane/towers.rb
CHANGED
@@ -1,31 +1,23 @@
|
|
1
1
|
module Hanoi
|
2
2
|
module Jane
|
3
3
|
class Towers
|
4
|
-
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :total, :stacks
|
5
7
|
|
6
8
|
def initialize discs
|
7
9
|
@discs = discs
|
8
|
-
@
|
10
|
+
@total = 0
|
9
11
|
@base = 2
|
10
12
|
@stacks = [(0...discs).to_a.reverse, [], []]
|
11
13
|
end
|
12
14
|
|
13
15
|
def move
|
14
|
-
|
15
|
-
|
16
|
-
@source = find_disc
|
17
|
-
|
16
|
+
diff
|
17
|
+
find_disc
|
18
18
|
@stacks[find_stack].push @stacks[@source].pop
|
19
19
|
end
|
20
20
|
|
21
|
-
def binary
|
22
|
-
rebased
|
23
|
-
end
|
24
|
-
|
25
|
-
def rebased
|
26
|
-
Towers.rebase @count, @base, @discs
|
27
|
-
end
|
28
|
-
|
29
21
|
def solved
|
30
22
|
rebased.chars.all? { |digit| digit.to_i == @base - 1 }
|
31
23
|
end
|
@@ -34,9 +26,47 @@ module Hanoi
|
|
34
26
|
Matrix.new self
|
35
27
|
end
|
36
28
|
|
29
|
+
def inspect
|
30
|
+
{
|
31
|
+
stacks: @stacks,
|
32
|
+
moves: @total,
|
33
|
+
binary: rebased,
|
34
|
+
flipped: @disc
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def each
|
39
|
+
yield self if @total == 0
|
40
|
+
until solved
|
41
|
+
move
|
42
|
+
yield self
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
s = ''
|
48
|
+
@stacks.each do |stack|
|
49
|
+
s += stack.to_s
|
50
|
+
s += "\n"
|
51
|
+
end
|
52
|
+
s += '---'
|
53
|
+
|
54
|
+
s
|
55
|
+
end
|
56
|
+
|
57
|
+
def binary
|
58
|
+
rebased
|
59
|
+
end
|
60
|
+
|
61
|
+
def rebased
|
62
|
+
Towers.rebase @total, @base, @discs
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
37
67
|
def find_disc
|
38
68
|
@stacks.each_with_index do |stack, index|
|
39
|
-
|
69
|
+
@source = index if stack.index @disc
|
40
70
|
end
|
41
71
|
end
|
42
72
|
|
@@ -55,35 +85,20 @@ module Hanoi
|
|
55
85
|
return (@source + 1) % 3
|
56
86
|
end
|
57
87
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
s = ''
|
67
|
-
@stacks.each do |stack|
|
68
|
-
s += stack.to_s
|
69
|
-
s += "\n"
|
88
|
+
def diff
|
89
|
+
this = binary
|
90
|
+
@total += 1
|
91
|
+
that = binary
|
92
|
+
this.chars.reverse.each_with_index do |bit, index|
|
93
|
+
if bit < that.chars.reverse[index]
|
94
|
+
@disc = index
|
95
|
+
end
|
70
96
|
end
|
71
|
-
s += '---'
|
72
|
-
|
73
|
-
s
|
74
97
|
end
|
75
98
|
|
76
99
|
def Towers.rebase value, base, width
|
77
100
|
'%0*d' % [width, value.to_s(base)]
|
78
101
|
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
102
|
end
|
88
103
|
end
|
89
104
|
end
|
data/lib/hanoi/jane/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanoi-jane
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pikesley
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -125,7 +125,10 @@ dependencies:
|
|
125
125
|
description: by counting in base 2 or 3
|
126
126
|
email:
|
127
127
|
- sam.pikesley@gmail.com
|
128
|
-
executables:
|
128
|
+
executables:
|
129
|
+
- console
|
130
|
+
- hanoi
|
131
|
+
- setup
|
129
132
|
extensions: []
|
130
133
|
extra_rdoc_files: []
|
131
134
|
files:
|