This is a follow up question from Tower of Hanoi: Recursive Algorithm
where the basic principle of this algorithm is well explained.
However I have a slightly different implementation of this algorithm here which I don't fully understand (code is from a university lecture, so I can't link to any source):
// k = nr. of discs, a = start peg, b = destination peg
public static void hanoi( int k, int a, int b)
{
if( k > 0)
{
hanoi( k - 1, a, 6 - a - b); // 1. move (k-1) discs to temporary peg
System.out.println("" + k + ": " + a + " => " + b); // 2. move k. disc from a to b
hanoi( k - 1, 6 - a - b, b); // 3. move (k-1) discs from temporary peg to peg b
}
}
The principle is the same as in the other topic (as far as I understand), but I don't understand where the '6' in '6 - a -b" is coming from, could someone explain ?
6 - a - b
comes from (1 + 2 + 3) = 6 and gives you index of "the other" peg:
a = 1, b = 3
than
6 - a - b = 2