hanoi-jane 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/hanoi/jane/cli.rb +12 -3
- data/lib/hanoi/jane/towers.rb +44 -0
- data/lib/hanoi/jane/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53bb5402aa446034d4307ad875c9ac4f5a1a104b
|
4
|
+
data.tar.gz: 278ecba723e14b8f88ff862633dcc90c48dc985d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77976b7bd533cfae55610fe7d28c52fe273a2afb2fbc60c1614dd7b08e52b990ea54ccc3ea9bdbad136f7757d3dae38cc0d5391a5be50215a3b82fcaaf1cebed
|
7
|
+
data.tar.gz: f5e32d1adf55bfd68e8f6af17066d175660c50d73730e1d2ad57918b789a911acd1726df0c6cafb874ba6483021226f0c9a7ec8cc68dea657590fe96f692197a
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ Yes, there are. This is very much a Solved Problem. However, I was inspired to i
|
|
27
27
|
|
28
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
|
29
29
|
|
30
|
-
hanoi --constrained
|
30
|
+
hanoi console --discs 6 --constrained
|
31
31
|
|
32
32
|
## API
|
33
33
|
|
data/lib/hanoi/jane/cli.rb
CHANGED
@@ -26,17 +26,26 @@ module Hanoi
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
desc 'console', 'Solve the towers
|
30
|
-
option :discs, type: :numeric, default: 3
|
29
|
+
desc 'console', 'Solve the towers on the console'
|
30
|
+
option :discs, type: :numeric, default: 3, minimum: 1
|
31
31
|
option :constrained, type: :boolean
|
32
32
|
def console
|
33
|
+
if options[:discs] < 1
|
34
|
+
puts "Solving for %d discs makes no sense" % options[:discs]
|
35
|
+
exit 1
|
36
|
+
end
|
37
|
+
|
33
38
|
towers = Towers.new options[:discs]
|
34
39
|
if options[:constrained]
|
35
40
|
towers = ConstrainedTowers.new options[:discs]
|
36
41
|
end
|
37
42
|
|
38
43
|
towers.each do |state|
|
39
|
-
|
44
|
+
system('clear')
|
45
|
+
puts state.rebased
|
46
|
+
puts
|
47
|
+
puts state.console
|
48
|
+
sleep 0.2
|
40
49
|
end
|
41
50
|
|
42
51
|
puts '%d moves to solve for %d discs' % [towers.total, options[:discs]]
|
data/lib/hanoi/jane/towers.rb
CHANGED
@@ -26,6 +26,19 @@ module Hanoi
|
|
26
26
|
Matrix.new self
|
27
27
|
end
|
28
28
|
|
29
|
+
def console
|
30
|
+
s = ''
|
31
|
+
x = self.stacks.clone
|
32
|
+
y = x.map { |s| s.clone }
|
33
|
+
|
34
|
+
(Towers.rotate y.map { |s| (Towers.pad s, @discs).reverse }).each do |stack|
|
35
|
+
s += stack.map { |s| Towers.make_disc s, (Towers.scale @discs) }.join ' '
|
36
|
+
s += "\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
s
|
40
|
+
end
|
41
|
+
|
29
42
|
def inspect
|
30
43
|
{
|
31
44
|
stacks: @stacks,
|
@@ -99,6 +112,37 @@ module Hanoi
|
|
99
112
|
def Towers.rebase value, base, width
|
100
113
|
'%0*d' % [width, value.to_s(base)]
|
101
114
|
end
|
115
|
+
|
116
|
+
def Towers.pad array, length
|
117
|
+
until array.length == length
|
118
|
+
array.push nil
|
119
|
+
end
|
120
|
+
|
121
|
+
array.reverse
|
122
|
+
end
|
123
|
+
|
124
|
+
def Towers.make_disc width, space, char = 'o'
|
125
|
+
unless width
|
126
|
+
return ' ' * space
|
127
|
+
end
|
128
|
+
|
129
|
+
count = Towers.scale width
|
130
|
+
padding = (space - count) / 2
|
131
|
+
|
132
|
+
'%s%s%s' % [
|
133
|
+
' ' * padding,
|
134
|
+
char * count,
|
135
|
+
' ' * padding
|
136
|
+
]
|
137
|
+
end
|
138
|
+
|
139
|
+
def Towers.scale size
|
140
|
+
(size * 2) + 1
|
141
|
+
end
|
142
|
+
|
143
|
+
def Towers.rotate stacks
|
144
|
+
stacks.map { |s| s.clone }.transpose.reverse
|
145
|
+
end
|
102
146
|
end
|
103
147
|
end
|
104
148
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pikesley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|