Created
May 19, 2017 03:55
-
-
Save odarbelaeze/a7e8e68de5c1021a648b7058438162d3 to your computer and use it in GitHub Desktop.
A solution to the Hanoi tower problem.
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
/// One of the posts of the tower of Hanoi | |
#[derive(Debug, Clone, Copy)] | |
enum Post { | |
Left, Middle, Right, | |
} | |
/// Helper function to recursively resolve a Tower of Hanoi | |
fn recursive_hanoi(n: u64, from: Post, to: Post, auxiliar: Post) { | |
if n > 0 { | |
recursive_hanoi(n - 1, from, auxiliar, tp); | |
println!("{:?} => {:?}", from, to); | |
recursive_hanoi(n - 1, auxiliar, tp, from); | |
} | |
} | |
/// The height of the tower initially, everything starts in the left post | |
fn hanoi(n: u64) { | |
recursive_hanoi(n, Post::Left, Post::Right, Post::Middle) | |
} | |
fn main() { | |
println!("The tower of Hanoi!"); | |
hanoi(3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment