Pages

Towers of Hanoi Notes

public class Analysis
{
    public static void main(String[] args)
    {
        final int n = Integer.parseInt(args[0]);
        final int d = Integer.parseInt(args[1]);
        System.out.print(Puzzle.minimum(n, d));
    }
}
class Puzzle
{
    private static int count(int n, int d)
    {
        if (n == 0)
            return 0;
        else if (n == 1)
            return d;
        else if (d == 1)
            return 2 * count(n-1, 2) + 1;
        else // (d == 2)
            return 2 * count(n-1, 2) + count(n-1, 1) + 2;
    }
    public static int minimum(int n, int d)
    {
        if (n < 0) n = 0;
        if (d < 0) d = -d;
        if (d > 2) d = d % 3;
        return count(n, d);
    }
}