%%%%%%% 
% randompancakeflipper.m
% Author: Lance Harden, Davidson College
% June 2006
% 
% Description: This original, user-friendly version of the pancake
% simulator models all variations of problem, accepting inputs for the
% number of spatulas and whether the pancakes are signed. The output is a
% histogram that displays the number of random flips it took in each trial
% for the stack to be sorted into the home, [1 2 3 4 5], permutation.

Results = [];
s = input('Number of spatulas: ')

if (s ~= 1 & s ~= 2)
    disp('How many arms do you have!')
    return
end

t = input('Signed or unsigned permutation? (Enter s or u): ', 's')
if (t ~= 's' & t ~= 'u')
    disp('Error: You must enter "s" or "u".  Try again by re-running the program.')
    return
end

if t == 's'
    I = input('Enter your desired initial permutation as a row vector (you MUST use brackets), e.g. [2 -3 4 -1]: ')
elseif t == 'u'
    I = input('Enter your desired initial permutation as a row vector (you MUST use brackets), e.g. [2 3 4 1]: ')
end

k = length(I);
if k < 2
    disp('Error: You must enter your initial permutation as a row vector with at least two entries.')
    return
end

B = sort(abs(I));

if (B ~= 1:k & t == 's')
    disp('Error: Your initial permutation must consist of signed consecutive integers starting at 1, e.g. [3 -2 -4 1] is OK, but [8 3 5 1] and [0 -2 1 -3] are not.')
    return
end
if (B ~= 1:k & t == 'u')
    disp('Error: Your initial permutation must consist of consecutive integers starting at 1, e.g. [3 1 2 4] is OK, but [2 8 0 5] is not.')
    return
end

n = input('Number of trials: ')
slots = k+1;
choices = nchoosek(1:slots,2);
choices(:,2) = choices(:,2) - 1;
numchoices = length(choices);

tic
if s == 1
    if t == 's'
        for i = 1:n
        G = I;
        count = 0;
        
            while ( any(G ~= 1:k) || any(G ~= -k:-1) )
            count = count + 1;
            P = G;
            a = ceil(k*rand);
            G = [fliplr(P(1:a)) P(a+1:k)];
              for j = 1:a
                  G(j) = -G(j);
              end
            end
            Results(i) = count;
        end
    else
        for i = 1:n
            G = I;
            count = 0;
            while any(G ~= 1:k)
                count = count + 1;
                P = G;
                a = ceil(k*rand);
                G = [fliplr(P(1:a)) P(a+1:k)];
            end
            Results(i) = count;            
        end
    end
else
    if t == 's'
        for i = 1:n
            G = I;
            count = 0;
            while ( any(G ~= 1:k) || any(G ~= -k:-1) )
                count = count + 1;
                P = G;
                a = ceil(numchoices*rand);
                G = [P(1:(choices(a,1)-1)) fliplr(P(choices(a,1):choices(a,2))) P((choices(a,2)+1):k)];                  
                for j = choices(a,1):choices(a,2)
                  G(j) = -G(j);
                end
            end
            Results(i) = count;
        end
    else
        for i = 1:n
            G = I;
            count = 0;
            while any(G ~= 1:k)
                count = count + 1;
                P = G;
                a = ceil(numchoices*rand);
                G = [P(1:(choices(a,1)-1)) fliplr(P(choices(a,1):choices(a,2))) P((choices(a,2)+1):k)];
            end
            Results(i) = count;
        end
    end
end
time = toc
beep
x = 5:10:max(Results);
hist(Results,x); figure(gcf)