Created
March 9, 2018 04:33
-
-
Save Yorgg/ac2e9a9d2f6d544b8439debc46529e82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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