2001-09-21 00:00:00 UTC

# tiny change 9

by oleg

Status: Failed
Results: []

Code
```function finalAnswer = solver(numPegs,numColors,guessLimit,puzzleID)

if (numColors^numPegs < 5e4) % a sieve for small puzzles
boards = make(numPegs, numColors);
nb = size(boards,2);
nguess = 0;
sg = 0;
while (nb > 1) & (nguess <= guessLimit)
guess = boards(:,ceil(rand * nb));
[black,white,nguess]=scoreme(guess',puzzleID);
boards = prune(black, white, guess, boards, numColors);
nb = size(boards,2);
end
return
end

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

if numPegs-lsr==2,
return
end

lc=size(cols,1);
for i=1:lc-1
free(ind)=[];
if ng>=guessLimit
cntr=1;
for j=i+1:lc
cntr=cntr+cols(j,2);
end
return
end
lsure=lsure+cols(i,2);
if lsure>=numPegs
return
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(x) == n,
ind = 1:n;
return
end
if (length(x)<14) & (n~= 1)
return
end
i = floor(length(x)/2);
g = gs;
g(x(1:i)) = 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];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function boards = prune(black, white, guess, boards, nc)
nb = size(boards,2);
boards(:, sum(repmat(guess,1,nb) == boards) ~= black) = [];
nb = size(boards,2);
if nb > 0
w = sum(min(repmat(histc(guess, 1:nc),1,nb), histc(boards, 1:nc))) ~= (black+white);
boards(:, w) = [];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fr = 1:numPegs;  % Unidentified columns?
cs = zeros(numColors,2); % Second column is number of unidentified pegs of a given color
guess = ones(1,numPegs); % Our guess
T = 0;
j = 0;
k = 1;
colors_found=logical(zeros(numColors,1));
lsr = 0; %Number of pegs identified?
secondaryColor = 1;  %The secondary color we're testing
mc = 0;
for i = 1:numColors-1
guess(fr) = i;
if (secondaryColor <= j)
guess(fr(k)) = cs(secondaryColor,1);
black = black + white - 1 - lsr;
if white
if (white > 1)
guess(fr(k)) = i;
fr(k) = [];
lsr = lsr + 1;
black = black - 1;
T = T + 1;
else
k = k + 1;
end
else
fr(k)=[];
cs(secondaryColor,2) = cs(secondaryColor,2)-1;
lsr=lsr+1;
if (cs(secondaryColor,2)==0)
mc = cs(secondaryColor,1);
colors_found(secondaryColor)=1;
if sum(cs(:,2))~=0
[buh,secondaryColor]=max(cs(:,2));
else
secondaryColor=secondaryColor+1;
end
k = 1;
end
end
else
black = black - lsr;
end
if black
j=j+1;
cs(j,1)=i;
cs(j,2)=black;
T=T+black;
else
mc=i;
end
if (numCallsMade >= guessLimit) | (T >= numPegs)
break;
end
end
if (T < numPegs)
j = j+1;
cs(j,1) = numColors;
cs(j,2) = numPegs-T;
end

cs=cs(~colors_found(1:j),:);

if isempty(cs),
lsr = numPegs;
return
elseif size(cs,1) == 1;
guess(fr) = cs(1,1);
lsr = numPegs;
return
end
j = 1;
for i = 1:size(cs,1)
guess(fr(j:j+cs(i,2)-1)) = cs(i,1);
j=j+cs(i,2);
end
lsr = numPegs;
return
end
if mc
guess(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);
guess(fr) = c1;
while n2
guess(fr(i)) = c2;
if (white==0)
fr(i) = [];
n2 = n2-1;
lsr = lsr+1;
elseif (white > 1)
guess(fr(i)) = c1;
fr(i) = [];
cs(1,2) = cs (1,2) - 1;
lsr = lsr+1;
else
guess(fr(i)) = c1;
i = i+1;
end
guess(fr(1:n2)) = c2;
fr = fr(n2+1:end);
for i = 3:size(cs,1)
guess(fr(1:cs(i,2))) = cs(i,1);
fr = fr(cs(i,2)+1:end);
end
lsr = numPegs;
return
end
end
guess(fr) = c2;
cs(2,:) = [];
[i,j] = sort(-cs(:,2));
cs = cs([j(end); j(1:end-1)],:);
if (size(cs,1) == 1)
guess(fr) = cs(1,1);
lsr = numPegs;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent guessesCache
len = length(index);
oneToL=1:len;
if isempty(guessesCache) | length(guessesCache) < len | isempty(guessesCache{len})
twoexp = 1;
guesses = zeros(1, 0);
for i = 1:len % MATLAB knows how to optimize this
guesses = [ ...
guesses zeros(twoexp, 1); ...
guesses ones(twoexp, 1)];
twoexp = twoexp * 2;
end
guessesCache{len} = guesses;
else
guesses = guessesCache{len};
end
count = 0;
count = count+1;
if (count == 1)
bestguess = (oneToL)>len/2;
elseif (count == 2)
bestguess = (oneToL <= len/4)+(oneToL > len*0.75);
else
bestguess = guesses(bestguessn,:);
end
g = gs;
g(index(find(bestguess))) = cl;
end