Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 2, 2013 03:16
Show Gist options
  • Save ahimmelstoss/6788651 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6788651 to your computer and use it in GitHub Desktop.
Towers of Hanoi
a = [1,2,3,4]
b = []
c = []
def move_disk(number_of_disks,from,to,via)
orig_from = Array.new(from)
orig_to = Array.new(to)
orig_via = Array.new(via)
if number_of_disks == 1
to.unshift(from.shift)
puts "Moved #{number_of_disks} disks from #{orig_from} to #{orig_to} via #{orig_via}; from is now #{from}; to is now #{to}."
return to
end
move_disk(number_of_disks-1, from, via, to) # step 1
move_disk(1, from, to, via) # step 2
move_disk(number_of_disks-1, via, to, from) # step 3
puts "Moved #{number_of_disks} disks from #{orig_from} to #{orig_to} via #{orig_via}; from is now #{from}; to is now #{to}."
end
puts move_disk(4, a, c, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment