Skip to content

Instantly share code, notes, and snippets.

@Yorgg
Created March 9, 2018 04:33
Show Gist options
  • Save Yorgg/ac2e9a9d2f6d544b8439debc46529e82 to your computer and use it in GitHub Desktop.
Save Yorgg/ac2e9a9d2f6d544b8439debc46529e82 to your computer and use it in GitHub Desktop.
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