-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowersOfHanoiSim.rb
55 lines (47 loc) · 1.11 KB
/
TowersOfHanoiSim.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Towers
def initialize(numDisks)
@numDisks = numDisks
@towers = [[],[],[]]
numDisks.downto(1) {|i| @towers[0].push(i)}
end
def print_towers()
3.times do |tower_num|
(@numDisks-@towers[tower_num].size).times do |i|
puts
end
(@towers[tower_num].size-1).downto(0) do |i|
print " "*((@numDisks-@towers[tower_num][i]))
puts '__'*@towers[tower_num][i]
end
puts
puts '------------------------------------------------'
end
end
def move(from, to)
disk = @towers[from].pop
if disk.nil?
raise "tower is empty"
elsif @towers[to].size > 0 and disk > @towers[to][-1]
raise "trying to stack #{disk} onto #{@towers[to][-1]}"
end
@towers[to].push(disk)
puts "\e[H\e[2J"
print_towers
sleep(0.005)
end
def numDisks(i)
@towers[i].size
end
end
def solve(t)
_solve(t, t.numDisks(0), 0, 2)
end
def _solve(t, n, from, to)
buf = Math.log2(7&~(2**to|2**from)).to_i
return if n == 0
_solve(t, n-1, from, buf)
t.move(from, to)
_solve(t, n-1, buf, to)
end
t = Towers.new(14)
solve(t)