Recursive solution to Ogres of Hanoi
This file contains 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
(defn move [from to] | |
(.say this "move" (to-array [from to]))) | |
(defn hanoi [num from to via] | |
(if (= num 1) | |
(move from to) | |
(do | |
(hanoi (dec num) from via to) | |
(move from to) | |
(hanoi (dec num) via to from)))) | |
(hanoi 5 2 1 3) |
This file contains 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
var that = this; | |
function hanoi(n, from, to, via) { | |
if (n === 1) { | |
that.say("move", [from, to]); | |
} else { | |
hanoi(n-1, from, via, to); | |
that.say("move", [from, to]); | |
hanoi(n-1, via, to, from); | |
} | |
} | |
hanoi(5, 2, 3, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment