hanoi-jane 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18a7024f33d514778591e76528439a5f1bbfc6c2
4
- data.tar.gz: cb2e4809487b082cbb7f10d7a0ad9dc494628005
3
+ metadata.gz: dd5850a35a95c577a0d78b09181488465ba58cd9
4
+ data.tar.gz: a674323ec59602fd50e1d21cd89598e8ac5106ab
5
5
  SHA512:
6
- metadata.gz: e85d9650604ae1bff0ddf45a5bcdf5ee5c1eda42fd423f77af08e7e7a403c1ddf3f6de91345f653e3ca791adc2e70ee6dc75817a91a528783e77a855e293410d
7
- data.tar.gz: 440b0ec941455136734c5790e6100469e410290dfd684903ffeb21cbb013337c92dd1fcfd192deb2d981e921a9e4d215ab192c0b183c2d1fea5bc377697fdd88
6
+ metadata.gz: bf1edcc3017504af0e62ec4c07af84433a43dec0d5c7bbb42a419aa69bbfd9fd1c65614884b7d5d4c3af40e2490ad086bb260f4545224be24be348a7c870ff82
7
+ data.tar.gz: 2806fb5f677b649f593893aa69bfb481be1e617c4183709c2f23f7d27b7382789f01b4d6c99ed42bba5708ce3be4f2771e8811a2a7184ec303d8897d563d2215
data/README.md CHANGED
@@ -21,6 +21,8 @@ Yes, there are. This is very much a Solved Problem. However, I was inspired to i
21
21
  bundle exec rake install
22
22
  hanoi console
23
23
 
24
+ (or just `gem install hanoi-jane`, of course)
25
+
24
26
  ## Constrained version
25
27
 
26
28
  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
@@ -32,16 +34,26 @@ There is a [constrained variant of the problem](https://www.youtube.com/watch?v=
32
34
  To use it in your own code, try something like:
33
35
 
34
36
  ```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>
37
+ require 'hanoi/jane'
38
+
39
+ towers = Hanoi::Jane::ConstrainedTowers.new 2
40
+ towers.each do |state|
41
+ puts state.inspect
42
+ end
43
+ ```
44
+
45
+ which will give you:
46
+
47
+ ```ruby
48
+ {:stacks=>[[1, 0], [], []], :moves=>0, :flipped=>nil, :ternary=>"00"}
49
+ {:stacks=>[[1], [0], []], :moves=>1, :flipped=>0, :ternary=>"01"}
50
+ {:stacks=>[[1], [], [0]], :moves=>2, :flipped=>0, :ternary=>"02"}
51
+ {:stacks=>[[], [1], [0]], :moves=>3, :flipped=>1, :ternary=>"10"}
52
+ {:stacks=>[[], [1, 0], []], :moves=>4, :flipped=>0, :ternary=>"11"}
53
+ {:stacks=>[[0], [1], []], :moves=>5, :flipped=>0, :ternary=>"12"}
54
+ {:stacks=>[[0], [], [1]], :moves=>6, :flipped=>1, :ternary=>"20"}
55
+ {:stacks=>[[], [0], [1]], :moves=>7, :flipped=>0, :ternary=>"21"}
56
+ {:stacks=>[[], [], [1, 0]], :moves=>8, :flipped=>0, :ternary=>"22"}
45
57
  ```
46
58
  where `flipped` is the disc that was moved last
47
59
 
@@ -4,11 +4,6 @@ module Hanoi
4
4
  def initialize discs
5
5
  super
6
6
  @base = 3
7
-
8
- @directions = {}
9
- @stacks[0].each do |disc|
10
- @directions[disc] = :right
11
- end
12
7
  end
13
8
 
14
9
  def ternary
@@ -17,30 +12,24 @@ module Hanoi
17
12
 
18
13
  def inspect
19
14
  i = super
20
-
21
- i.delete :binary
22
- i[:ternary] = rebased
23
-
15
+ i[:ternary] = i.delete :binary
24
16
  i
25
17
  end
26
18
 
27
19
  private
28
20
 
29
21
  def find_stack
30
- case @source
31
- when 0
32
- @directions[@disc] = :right
33
- return 1
34
- when 2
35
- @directions[@disc] = :left
36
- return 1
37
- when 1
38
- if @directions[@disc] == :right
22
+ # if we're in the middle
23
+ if @source == 1
24
+ # we always move to the right on an even total
25
+ if @total % 2 == 0
39
26
  return 2
40
27
  else
41
28
  return 0
42
29
  end
43
30
  end
31
+ # otherwise we're at the edges and can only move to the middle
32
+ 1
44
33
  end
45
34
  end
46
35
  end
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanoi-jane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley