Skip to content

Instantly share code, notes, and snippets.

@odarbelaeze
Created May 19, 2017 03:55
Show Gist options
  • Save odarbelaeze/a7e8e68de5c1021a648b7058438162d3 to your computer and use it in GitHub Desktop.
Save odarbelaeze/a7e8e68de5c1021a648b7058438162d3 to your computer and use it in GitHub Desktop.
A solution to the Hanoi tower problem.
/// 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