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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c49544f6ba3f25f061e296b69216675bdca3383b
4
- data.tar.gz: 982b160b51f1615f11ec0aa15551327470909bb0
3
+ metadata.gz: 18a7024f33d514778591e76528439a5f1bbfc6c2
4
+ data.tar.gz: cb2e4809487b082cbb7f10d7a0ad9dc494628005
5
5
  SHA512:
6
- metadata.gz: 3c65f08629c3616617e11ce36a6c914dbc20d04108187714e3e29673fd2ba037859f68780a04f9072d7d51a0f867d038951ac0fa621ed62b44e6381cfd2d4344
7
- data.tar.gz: b15f6ba0c8fe1e0c9a8e9fbe5bbc18bc7fd4ace2f0dfbc950794d364d9eeef2bfd5b5d015e99baf09264862b43aca0b4952f048311499e052aaa2bb3980afcab
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 ./bin/hanoi console
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
- <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>
56
+ [![Video](https://i.imgur.com/QILZYgx.png)](https://www.youtube.com/watch?v=PAQY5XtdNO8)
@@ -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 = 'exe'
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
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'
@@ -20,12 +20,10 @@ module Hanoi
20
20
  towers = ConstrainedTowers.new 5
21
21
  end
22
22
 
23
- until towers.solved do
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
- until towers.solved do
40
- puts towers
41
- towers.move
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.count, options[:discs]]
42
+ puts "%d moves to solve for %d discs" % [towers.total, options[:discs]]
46
43
  end
47
44
  end
48
45
  end
@@ -14,7 +14,18 @@ module Hanoi
14
14
  def ternary
15
15
  rebased
16
16
  end
17
-
17
+
18
+ def inspect
19
+ i = super
20
+
21
+ i.delete :binary
22
+ i[:ternary] = rebased
23
+
24
+ i
25
+ end
26
+
27
+ private
28
+
18
29
  def find_stack
19
30
  case @source
20
31
  when 0
@@ -11,13 +11,13 @@ module Hanoi
11
11
 
12
12
  offset = 0
13
13
  @stacks.each do |stack|
14
- count = 0
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 - count][i + offset + shim] = 1
18
+ self[6 - total][i + offset + shim] = 1
19
19
  end
20
- count += 1
20
+ total += 1
21
21
  end
22
22
  offset += 8
23
23
  end
@@ -1,31 +1,23 @@
1
1
  module Hanoi
2
2
  module Jane
3
3
  class Towers
4
- attr_reader :count, :stacks
4
+ include Enumerable
5
+
6
+ attr_reader :total, :stacks
5
7
 
6
8
  def initialize discs
7
9
  @discs = discs
8
- @count = 0
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
- @disc = Towers.diff Towers.rebase(@count, @base, @discs),
15
- Towers.rebase(@count += 1, @base, @discs)
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
- return index if stack.index @disc
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 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"
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
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-22 00:00:00.000000000 Z
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: