Skip to content

Instantly share code, notes, and snippets.

@TrevMcKendrick
Created October 11, 2013 15:19
Show Gist options
  • Save TrevMcKendrick/6936543 to your computer and use it in GitHub Desktop.
Save TrevMcKendrick/6936543 to your computer and use it in GitHub Desktop.
# tower_of_hanoi.rb
a = [1,2]
b = []
c = []
# Move the disks from one peg to another following the rules of Hanoi.
#
# number_of_disks - the total number of disks
# from - the starting peg
# to - the ending (goal) peg
# via - the remaining peg (b in this case)
#
def move_disk(number_of_disks,from,to,via)
if from == [] && via == []
return to
else
if move_count.odd? == true
if from.length.odd? == true
to << 1
else
via << 1
end
end
to << number_of_disks if from == [number_of_disks] && to == []
end
move_disk(number_of_disks,from,to,via)
to
end
puts move_disk(2, a, c, b).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment