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 +4 -4
- data/README.md +22 -10
- data/lib/hanoi/jane/constrained_towers.rb +7 -18
- data/lib/hanoi/jane/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd5850a35a95c577a0d78b09181488465ba58cd9
|
4
|
+
data.tar.gz: a674323ec59602fd50e1d21cd89598e8ac5106ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
data/lib/hanoi/jane/version.rb
CHANGED