hanoi-jane 0.1.4 → 0.2.0

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: 332fd131e22d58d5814474ce82ec8e3dcf024e45
4
- data.tar.gz: 826b3da67992715185f25fbaa9910433af4711d4
3
+ metadata.gz: 53bb5402aa446034d4307ad875c9ac4f5a1a104b
4
+ data.tar.gz: 278ecba723e14b8f88ff862633dcc90c48dc985d
5
5
  SHA512:
6
- metadata.gz: f1309697bc6ec42d16e1a69d511d2452e83ee653bb9c0b65aca069b3bca49fb95610b1265ba5fc5419ae8eca40306bbde2ea31396c606d88b013446325b48e1f
7
- data.tar.gz: f5b481d155f881aec7d09c8bb7b78ead832c3509a85d13e3561968bd2febc2aac121948975540c1b073b4d95e47f6aac3a8a68f689b855fe93ed63dacd581f87
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
 
@@ -26,17 +26,26 @@ module Hanoi
26
26
  end
27
27
  end
28
28
 
29
- desc 'console', 'Solve the towers one the console'
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
- puts state
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]]
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = '0.1.4'
3
+ VERSION = '0.2.0'
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.4
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-25 00:00:00.000000000 Z
11
+ date: 2017-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler