Test | Status | Code Input and Output |
---|---|---|
1 | Fail |
tic
v=1111:6666;
vL=length(v);
m=zeros(vL,4);
for i=1:vL
vp=v(i);
for k=4:-1:1
m(i,k)=mod(vp,10);
vp=floor(vp/10);
end
end
mdel=sum((m==0)+(m>6),2)>0;
m(mdel,:)=[];
v=m*[1000;100;10;1];
mL=size(m,1);
mpc=zeros(mL); % 0.030
for j=1:mL
mpc(:,j)=sum(m==repmat(m(j,:),mL,1),2);
end
mch=zeros(mL,6); % 0.038
for i=1:mL
for k=1:6
mch(i,k)=nnz(m(i,:)==k);
end
end
mc=zeros(mL); % 0.06
for j=1:mL
mc(:,j)=sum(min(mch,repmat(mch(j,:),mL,1)),2);
end
mc=mc-mpc; % remove mpc part
mpc5c=5*mpc+mc;
fprintf('Initialization %.3f\n',toc)
% finished initilaiztion calculation in less than 0.2 sec
ztic=tic;
solved=1;
Gmax=1;
pcase=0;
Lmax=0;
Ltot=0;
for ptr=randperm(1296) % anti-hack randomization
pcase=pcase+1;
mguess=[];mpegs=[];
while solved % loop until solved
ztoc=toc(ztic);
if ztoc>45
solved=0;
break;
end % if
[mguessn]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v);
mguessptr=find(v==mguessn*[1000;100;10;1]);
if isempty(mguessptr),continue;end % invalid input
mguess(end+1,:)=mguessn;
mpegs(end+1,1)=mpc(ptr,mguessptr);
mpegs(end,2)=mc(ptr,mguessptr);
Lsol=size(mguess,1);
if mpegs(end,1)==4 % break on solved to ptr loop
Ltot=Ltot+Lsol;
if Lsol>Lmax, Lmax=Lsol;end
break;
end
if Lsol==5 % length of 5 and not solved
solved=0;
Gmax=0;
break;
end
end % while
if ~solved,break;end % terminate case processing
end % for all 1296 cases
if Gmax==0 % failed Guess max rqmt of 5
fprintf('\n Solution exceeded 5 guesses\n');
fprintf('Puzzle %i %i %i %i\n',m(ptr,:));
fprintf('Guessses and Responses\n');
fprintf('M%i %i %i %i P%i %i\n',[mguess mpegs]');
fprintf('\n');
end
if solved
fprintf('Solved in %.2f sec\n',ztoc)
fprintf('Lmax %i Ltot %i\n',Lmax,Ltot)
assert(isequal(solved,1))
else
fprintf('Solved %i of 1296 cases in %.2f sec\n',pcase-1,ztoc)
fprintf('Lmax %i Ltot %i\n',Lmax,Ltot)
assert(isequal(solved,1))
end
Initialization 0.138
Solved 1044 of 1296 cases in 45.01 sec
Lmax 5 Ltot 4665
|
198 Solvers
Project Euler: Problem 4, Palindromic numbers
120 Solvers
497 Solvers
Combinations without using nchoosek
47 Solvers
Is the paranthesis sequence balanced ?
73 Solvers