Skip to content

Instantly share code, notes, and snippets.

@Yorgg

Yorgg/hanoi-ruby.rb

Created Mar 9, 2018
Embed
What would you like to do?
def _log(a,b)
"move top disk from peg #{a} to peg #{b} \n"
end
def hanoi(n, s={from: 1, free: 2, to: 3})
return _log(s[:from], s[:to]) if n == 1
hanoi(n-1, {from: s[:from], free: s[:to], to: s[:free]}) +
_log(s[:from], s[:to]) +
hanoi(n-1, {from: s[:free], free: s[:from], to: s[:to]})
end
puts hanoi(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment