function plain = unbeer2(cipher,skull)

table = cipher;
wall = '';

inv = {'1'; '81'; '871'; '7561' ;'27451'; '147541'; '1971831'; '87654321'; '10989011'; '99009901'; '90990990991'; '900826446281'; '6793893129771' ;'24822695035461'; '576158940397351'; '2484472049689441'; '94152046783625731'; '745856353591160221'; '4293193717277486911'; '90547263681592039801'; '507109004739336492891'; '6063348416289592760181'; '95670995670995670995671'; '622406639004149377593361'; '39840637450199203187251'; '61685823754789272030651341'; '309963099630996309963099631'; '9964412811387900355871886121'; '5841924398625429553264604811'; '787375415282392026578073089701'};

k = length(table);
t = length(wall);
length_skull = length(skull);

while k > 1
    bone = table(k);
    
    bigbone = '';
    for i = 1:k-1,
        bigbone(i) = bone;
    end;
    
    table = beer_sub(table,bigbone);
    table = table(1:k-1);
    k = k - 1;
    %disp('"pass it around"')
    disp(sprintf('New Table : %s',table));
    wall = strcat(wall,bone);
    t = t + 1;
    %disp(sprintf('Wall : %s \n',wall));
    
    %disp('"take one down"')
    disp(sprintf('Bone : %s \n',bone));

    
    part_skull = skull(length_skull-t+1:length_skull);
    if bigger(part_skull,wall)
        wall = beer_sub(strcat('1',wall),part_skull);
    else
        wall = beer_sub(wall,part_skull);
    end;

    wall = wall(length(wall)-t+1:length(wall));

    
    %disp('"on the wall"')
    %disp(sprintf('Wall : %s \n',wall));
    
    %mult = num2str(10*t+1);
    inverter = inv(t);
    wall = beer_multi(wall,char(inverter));
    wall = wall(length(wall)-t+1:length(wall));
    
    % Left rotation
    wall = beer_right_rotation(wall);

    %[div,res] = beer_div('15646574645','154');
    disp(sprintf('"%d bottles of beer"',t));
    disp(sprintf('Wall : %s \n',wall));
end;    

plain = beer_translate(wall)

function res = beer_sub(ta,bb)
lb = length(bb);
lt = length(ta);
if (lt-lb == 0)
    lim = 1;
else lim = lt-lb;
end;

for i = lb:-1:lim,
    if (str2num(bb(i)) <= str2num(ta(lt-lb+i)))
        ta(lt-lb+i) = num2str(str2num(ta(lt-lb+i)) - str2num(bb(i)));
    else
        ta = beer_sub_carry(ta,lt-lb+i,bb(i));
    end;
end;
res = ta;

function x = beer_sub_carry(ta,i,a)
j = i-1;
if j > 0
    if ~bigger('0',ta(j))
        ta(j) = num2str(str2num(ta(j))-1);
        ta(i) = num2str(str2num(ta(i))+10-str2num(a));
    else 
        ta = beer_sub_carry(ta,j,'1');
        ta(i) = num2str(str2num(ta(i))+10-str2num(a));
    end;
else
    disp('error !')
end;
x = ta;

function res = bigger(a,b)
la = length(a);
lb = length(b);
if la==lb
    i = la;
    bigger = 1;
    while bigger 
        if str2num(a(la+1-i)) > str2num(b(la+1-i))
            res = 1;
            bigger = 0;
        elseif str2num(a(la+1-i)) < str2num(b(la+1-i))
            res = 0;
            bigger = 0;
        else
            if i == 1
                bigger = 0;
                res = 1;
            end;
            i = i-1;
        end;
    end;
else
    res = (la > lb);
end;
%if res
%    disp(sprintf('%s is bigger than %s',a,b));
%else
%    disp(sprintf('%s is not bigger than %s',a,b));
%end;
    
function wall = wash(w,k)
w
if w == '0'
    x(1,:) = w;
    i = 1
else
    x = '';
    x(1,:) = w;
    i = 1; 
    while (x(i,1) == '0')
        for j = 1:length(x)-1
            x(i+1,j) = x(i,j+1);
        end;
        i = i+1;
    end;
end;
result = deblank(x(i,:));
if nargin == 2
    wall = result(length(result)-k+1:length(result));
else
    wall = result;
end;
    

function plain = beer_translate(cipher)
l = length(cipher);
for i = 1:2:l-1,
    if cipher(i:i+1) == '00'
        plain((1+i)/2) = ' ';
    else
        plain((i+1)/2) = char(str2num(cipher(i:i+1))+64);
    end;
end;

function [div,res] = beer_div(a,b);
la = length(a);
lb = length(b);
x = a;
lx = length(x);

div = '';
res = '';
while ~bigger(b,x)
    %disp('big loop');
    lxtmp = lx;
    lx = length(x);
    lb = length(b);
    ld = length(div);
    dif = lxtmp - lx;
    if dif > 1
        for i = 1:dif-1
            ld = ld + 1;
            div(ld) = '0';
        end;
    end;
    
    xtmp = x(1:lb);
    while ~bigger(xtmp,b)
        %disp('find xtmp');
        lb = lb+1;
        xtmp = x(1:lb);
    end;
    
    mul = 1;
    btmp = b;
    b2 = btmp;
    while ~bigger(btmp,xtmp)
        %disp('find the correct mul');
        mul = mul +1;
        b2 = btmp;
        btmp = wash(beer_multi(b,num2str(mul)));
    end;
    if strcmp(btmp,xtmp)
        x = x(lb+1:lx);
        ld = ld+1;
        div(ld) = num2str(mul);
    else
        tmp = '';
        for i = 1:(length(x)-length(xtmp))
            tmp = strcat(tmp,'0');
        end;
        btmp = strcat(b2,tmp);
        x = wash(beer_sub(x,btmp));
        ld = ld+1;
        div(ld) = num2str(mul-1);
    end
end;

res = x;
        
function result = beer_multi(w,m)
k = length(w);
res = '';
for i = 1:length(m)
    mul = m(length(m)+1-i);
    carry = '0';
    ind = k+length(m)+2-i;
    for j = 1:k,
        restmp = num2str(str2num(mul)*str2num(w(k-j+1))+str2num(carry));
        if length(restmp) == 2
            res(i,ind) = restmp(2);
            ind = ind - 1;
            carry = restmp(1);
        else
            res(i,ind) = restmp(1);
            ind = ind - 1;
            carry = '0';
        end;
    end;
    if carry == '0'
    else res(i,ind) = carry;
    end;
end;
s = size(res);
result = '';
carry = '0';
carry2 = '0';
for i = s(2):-1:1,
    rest = str2num(res(1,i));
    if rest == []
        rest = 0;
    end;
    for j = 2:s(1),
        if (double(res(j,i)) == 0 | double(res(j,i)) == [])
            rest = rest;
        else
            rest = rest+str2num(res(j,i));
        end;
    end;
    rest = rest+ str2num(carry);
    if length(rest) >3
        disp('probleme dans la multi : carry > 999')
    end;
    
    if length(num2str(rest)) == 2
        a = num2str(rest);
        result(i) = a(2);
        carry = num2str(str2num(a(1))+str2num(carry2));
        carry2 = '0';
    elseif length(num2str(rest)) == 3
        a = num2str(rest);
        result(i) = a(3);
        carry = num2str(str2num(a(2))+str2num(carry2));
        carry2 = a(1);
    else
        result(i) = num2str(rest);
        carry = carry2;
        carry2 = '0';
    end;
end;

function tmp = beer_right_rotation(w)
k = length(w);
tmp(1) = w(k);
for i = 1:k-1,
    tmp(i+1) = w(i);
end

function x = beer_add(res)
s = size(res);
result = '';
carry = '0';
for i = s(2):-1:1,
    rest = str2num(res(1,i));
    if rest == []
        rest = 0;
    end;
    for j = 2:s(1),
        if (double(res(j,i)) == 0 | double(res(j,i)) == [])
            rest = rest;
        else
            rest = rest+str2num(res(j,i));
        end;
    end;
    rest = rest+ str2num(carry);
    if length(num2str(rest)) == 2
        a = num2str(rest);
        result(i) = a(2);
        carry = a(1);
    else
        result(i) = num2str(rest);
        carry = '0';
    end;
end;