2001-09-21 00:00:00 UTC

fguess05_1_b

Status: Passed
Results:  62.95,  37.05 (80.19 taken)
CPU Time: 119.353
Score: 1275.07
Submitted at: 2001-09-20 16:57:24 UTC
Scored at: 2001-09-20 17:05:58 UTC

Current Rank: 50th
Based on: fguess05_1 (diff)
Basis for: Compactante 2 (diff)
Basis for: tiny change 3 (diff)

Francois Glineur
20 Sep 2001
Minor speedup
Code
```function finalAnswer = solver(numPegs,numColors,guessLimit,puzzleID)

if lsr>= numPegs | numCallsMade>= guessLimit | isempty(cs)
return;
end

rows = size(cs,1); % to cancel consequentive calls to size().

for i = 1:rows
fr(ind) = [];
for j = i+1:rows
fr = fr(cs(j,2)+1:end);
end
return
end
lsr = lsr + cs(i,2);
if lsr >= numPegs
return;
end;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i=length(x);
if i == n,
ind = 1:n;
return;
end;
if (length(x)<14) & (n~= 1)
return;
end
i = floor(i/2);
xi = x(1:i);
g = gs;
g(xi) = col;
nv = n-black+b0;
if black>b0,
ind = [1:black-b0,(i+1:i+nv)];
return;
end;
ind = [i1,(i+1:i+nv)];
return;
end
ind = i+1:i+nv;
return;
else
i1 = [];
end
if nv,
i2 = i2+i;
else
i2 = [];
end;

ind = [i1 i2];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fr = 1:numPegs;
cs = zeros(numColors,2);
a = ones(1,numPegs);
T = 0;
j = 0;
k = 1;
lsr = 0;
l = 1;
mc = 0;
for i = 1:numColors-1
a(fr) = i;
if l<= j
a(fr(k)) = cs(l,1);
black = black + white - 1 - lsr;
if white
if white>1
a(fr(k)) = i;
fr(k) = [];
lsr = lsr + 1;
black = black - 1;
T = T + 1;
else
k = k + 1;
end
else
fr(k) = [];
cs(l,2) = cs(l,2)-1;
lsr = lsr+1;
if cs(l,2)==0
mc = cs(l,1);
l = l+1;
k = 1;
end;
end;
if length(fr)<cs(l,2)+k
a(fr(k:end))=cs(l,1);
lsr=lsr+cs(l,2);
fr=fr(1:k-1);
mc=cs(l,1);
l=l+1;
k=1;
end
else
black = black-lsr;
end
if black
j = j+1;
cs(j,:) = [i,black];
T = T+black;
else
mc = i;
end
if numCallsMade>= guessLimit | T>= numPegs,
break;
end;
end
%if T<numPegs
j = j+1;
cs(j,:) = cat(2,numColors,numPegs-T);
%        cs(j,:) = [numColors,numPegs-T]; ????is this really slower?

%end;

if numPegs == 1
a = cs(1,1);
lsr = 1;
return;
end;
cs = cs(l:j,:);
if isempty(cs),
lsr = numPegs;
return;
elseif size(cs,1) == 1;
a(fr) = cs(1,1);
lsr = numPegs;
return;
end
j = 1;
for i = 1:size(cs,1)
a(fr(j:j+cs(i,2)-1)) = cs(i,1);
j = j+cs(i,2);
end
lsr = numPegs;
return;
end
if mc
a(fr) = mc;
[i,j] = sort(-cs(:,2));
cs = cs(j,:);
else
cs = cs([2 1 3:end],:);
i = k;
n1 = cs(1,2);
n2 = cs(2,2);
c1 = cs(1,1);
c2 = cs(2,1);
a(fr) = c1;
while n2
a(fr(i)) = c2;
if white==0
fr(i) = [];
n2 = n2-1;
lsr = lsr+1;
else
a(fr(i)) = c1;
i = i+1;
end
a(fr(1:n2)) = c2;
fr = fr(n2+1:end);
for i = 3:size(cs,1)
a(fr(1:cs(i,2))) = cs(i,1);
fr = fr(cs(i,2)+1:end);
end;
lsr = numPegs;
return;
end;
end;
a(fr) = c2;
cs(2,:) = [];
[i,j] = sort(-cs(:,2));
cs = cs([j(end);j(1:end-1)],:);
if size(cs,1) == 1,
a(fr) = cs(1,1);
lsr = numPegs;
end;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent guessesCache
l = length(index);
oneToL=1:l;
if isempty(guessesCache) | length(guessesCache) < l | isempty(guessesCache{l})
twoexp = 1;
guesses = zeros(1, 0);
for i = oneToL
guesses = [guesses zeros(twoexp, 1);guesses ones(twoexp, 1)];
twoexp = twoexp * 2;
end;
guessesCache{l} = guesses;
else
guesses = guessesCache{l};
end
count = 0;
count = count+1;
if count == 1
bestguess = (oneToL)>l/2;
elseif count == 2
bestguess = (oneToL<= round(l/4))+(oneToL>round(3*l/4));
else