Skip to main content

Algod/general/
hanoi.rs

1pub fn hanoi(n: i32, from: i32, to: i32, via: i32, moves: &mut Vec<(i32, i32)>) {
2    if n > 0 {
3        hanoi(n - 1, from, via, to, moves);
4        moves.push((from, to));
5        hanoi(n - 1, via, to, from, moves);
6    }
7}
8
9#[cfg(test)]
10mod tests {
11    use super::*;
12
13    #[test]
14    fn hanoi_simple() {
15        let correct_solution: Vec<(i32, i32)> =
16            vec![(1, 3), (1, 2), (3, 2), (1, 3), (2, 1), (2, 3), (1, 3)];
17        let mut our_solution: Vec<(i32, i32)> = Vec::new();
18        hanoi(3, 1, 3, 2, &mut our_solution);
19        assert_eq!(correct_solution, our_solution);
20    }
21}