{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":44239,"title":"Mastermind IV: Optimal Solver - max of 5 guesses","description":"\u003chttps://en.wikipedia.org/wiki/Mastermind_(board_game) Mastermind\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\r\n\r\n  Answer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.\r\n\r\n[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v) \r\n\r\nwhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\r\n\r\nChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\r\n\r\nTheory: The optimal minimal guess solution requires only 5 guesses. The \u003chttps://en.wikipedia.org/wiki/Mastermind_(board_game) Mastermind\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\r\n\r\nMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Mastermind_(board_game)\"\u003eMastermind\u003c/a\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eAnswer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.\r\n\u003c/pre\u003e\u003cp\u003e[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\u003c/p\u003e\u003cp\u003ewhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\u003c/p\u003e\u003cp\u003eChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\u003c/p\u003e\u003cp\u003eTheory: The optimal minimal guess solution requires only 5 guesses. The \u003ca href = \"https://en.wikipedia.org/wiki/Mastermind_(board_game)\"\u003eMastermind\u003c/a\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\u003c/p\u003e\u003cp\u003eMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]\u003c/p\u003e","function_template":"function [guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\r\n% mguessn 1x4\r\n% mguess kx4\r\n% mpegs kx2  Location\u0026Color, Colors_Not_in location\r\n\r\n if isempty(mguess)\r\n  guess=[1 1 2 2]; % [1 1 1 1] is not a good first guess\r\n  return\r\n end\r\n \r\n guess=[1 1 1 2];\r\nend","test_suite":"%%\r\ntic\r\nv=1111:6666;\r\nvL=length(v);\r\nm=zeros(vL,4);\r\nfor i=1:vL\r\n  vp=v(i);\r\n  for k=4:-1:1\r\n   m(i,k)=mod(vp,10);\r\n   vp=floor(vp/10);\r\n  end\r\nend\r\nmdel=sum((m==0)+(m\u003e6),2)\u003e0;\r\nm(mdel,:)=[];\r\nv=m*[1000;100;10;1];\r\nmL=size(m,1);\r\n\r\nmpc=zeros(mL); % 0.030\r\nfor j=1:mL\r\nmpc(:,j)=sum(m==repmat(m(j,:),mL,1),2);\r\nend\r\n\r\nmch=zeros(mL,6); % 0.038\r\nfor i=1:mL\r\n  for k=1:6\r\n   mch(i,k)=nnz(m(i,:)==k);\r\n  end\r\nend\r\n\r\nmc=zeros(mL); % 0.06\r\nfor j=1:mL\r\n  mc(:,j)=sum(min(mch,repmat(mch(j,:),mL,1)),2);\r\nend\r\nmc=mc-mpc; % remove mpc part\r\nmpc5c=5*mpc+mc;\r\nfprintf('Initialization %.3f\\n',toc)\r\n% finished initilaiztion calculation in less than 0.2 sec\r\n\r\n\r\nztic=tic;\r\nsolved=1;\r\nGmax=1;\r\npcase=0;\r\nLmax=0;\r\nLtot=0;\r\nfor ptr=randperm(1296) % anti-hack randomization\r\n pcase=pcase+1;\r\n mguess=[];mpegs=[];\r\n while solved % loop until solved\r\n  ztoc=toc(ztic);\r\n  if ztoc\u003e45\r\n   solved=0;\r\n   break;\r\n  end % if\r\n  [mguessn]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v);\r\n  mguessptr=find(v==mguessn*[1000;100;10;1]);\r\n  if isempty(mguessptr),continue;end % invalid input\r\n  mguess(end+1,:)=mguessn;\r\n  mpegs(end+1,1)=mpc(ptr,mguessptr);\r\n  mpegs(end,2)=mc(ptr,mguessptr);\r\n  \r\n  Lsol=size(mguess,1);\r\n  if mpegs(end,1)==4 % break on solved to ptr loop\r\n   Ltot=Ltot+Lsol;\r\n   if Lsol\u003eLmax, Lmax=Lsol;end\r\n   break;\r\n  end\r\n  if Lsol==5 % length of 5 and not solved\r\n   solved=0;\r\n   Gmax=0;\r\n   break;\r\n   end\r\n end % while\r\n if ~solved,break;end % terminate case processing\r\nend % for all 1296 cases\r\n\r\nif Gmax==0 % failed Guess max rqmt of 5\r\n fprintf('\\n Solution exceeded 5 guesses\\n');\r\n fprintf('Puzzle %i %i %i %i\\n',m(ptr,:));\r\n fprintf('Guessses and Responses\\n');\r\n fprintf('M%i %i %i %i   P%i %i\\n',[mguess mpegs]');\r\n fprintf('\\n');\r\nend\r\n\r\nif solved\r\n fprintf('Solved in %.2f sec\\n',ztoc)\r\n fprintf('Lmax %i   Ltot %i\\n',Lmax,Ltot)\r\n assert(isequal(solved,1))\r\nelse\r\n fprintf('Solved %i of 1296 cases in %.2f sec\\n',pcase-1,ztoc)\r\n fprintf('Lmax %i   Ltot %i\\n',Lmax,Ltot)\r\n assert(isequal(solved,1))\r\nend\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":5,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-06-18T20:04:24.000Z","updated_at":"2025-12-12T15:50:04.000Z","published_at":"2017-06-18T20:39:39.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Mastermind_(board_game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[Answer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTheory: The optimal minimal guess solution requires only 5 guesses. The\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Mastermind_(board_game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":44239,"title":"Mastermind IV: Optimal Solver - max of 5 guesses","description":"\u003chttps://en.wikipedia.org/wiki/Mastermind_(board_game) Mastermind\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\r\n\r\n  Answer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.\r\n\r\n[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v) \r\n\r\nwhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\r\n\r\nChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\r\n\r\nTheory: The optimal minimal guess solution requires only 5 guesses. The \u003chttps://en.wikipedia.org/wiki/Mastermind_(board_game) Mastermind\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\r\n\r\nMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Mastermind_(board_game)\"\u003eMastermind\u003c/a\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eAnswer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.\r\n\u003c/pre\u003e\u003cp\u003e[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\u003c/p\u003e\u003cp\u003ewhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\u003c/p\u003e\u003cp\u003eChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\u003c/p\u003e\u003cp\u003eTheory: The optimal minimal guess solution requires only 5 guesses. The \u003ca href = \"https://en.wikipedia.org/wiki/Mastermind_(board_game)\"\u003eMastermind\u003c/a\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\u003c/p\u003e\u003cp\u003eMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]\u003c/p\u003e","function_template":"function [guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\r\n% mguessn 1x4\r\n% mguess kx4\r\n% mpegs kx2  Location\u0026Color, Colors_Not_in location\r\n\r\n if isempty(mguess)\r\n  guess=[1 1 2 2]; % [1 1 1 1] is not a good first guess\r\n  return\r\n end\r\n \r\n guess=[1 1 1 2];\r\nend","test_suite":"%%\r\ntic\r\nv=1111:6666;\r\nvL=length(v);\r\nm=zeros(vL,4);\r\nfor i=1:vL\r\n  vp=v(i);\r\n  for k=4:-1:1\r\n   m(i,k)=mod(vp,10);\r\n   vp=floor(vp/10);\r\n  end\r\nend\r\nmdel=sum((m==0)+(m\u003e6),2)\u003e0;\r\nm(mdel,:)=[];\r\nv=m*[1000;100;10;1];\r\nmL=size(m,1);\r\n\r\nmpc=zeros(mL); % 0.030\r\nfor j=1:mL\r\nmpc(:,j)=sum(m==repmat(m(j,:),mL,1),2);\r\nend\r\n\r\nmch=zeros(mL,6); % 0.038\r\nfor i=1:mL\r\n  for k=1:6\r\n   mch(i,k)=nnz(m(i,:)==k);\r\n  end\r\nend\r\n\r\nmc=zeros(mL); % 0.06\r\nfor j=1:mL\r\n  mc(:,j)=sum(min(mch,repmat(mch(j,:),mL,1)),2);\r\nend\r\nmc=mc-mpc; % remove mpc part\r\nmpc5c=5*mpc+mc;\r\nfprintf('Initialization %.3f\\n',toc)\r\n% finished initilaiztion calculation in less than 0.2 sec\r\n\r\n\r\nztic=tic;\r\nsolved=1;\r\nGmax=1;\r\npcase=0;\r\nLmax=0;\r\nLtot=0;\r\nfor ptr=randperm(1296) % anti-hack randomization\r\n pcase=pcase+1;\r\n mguess=[];mpegs=[];\r\n while solved % loop until solved\r\n  ztoc=toc(ztic);\r\n  if ztoc\u003e45\r\n   solved=0;\r\n   break;\r\n  end % if\r\n  [mguessn]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v);\r\n  mguessptr=find(v==mguessn*[1000;100;10;1]);\r\n  if isempty(mguessptr),continue;end % invalid input\r\n  mguess(end+1,:)=mguessn;\r\n  mpegs(end+1,1)=mpc(ptr,mguessptr);\r\n  mpegs(end,2)=mc(ptr,mguessptr);\r\n  \r\n  Lsol=size(mguess,1);\r\n  if mpegs(end,1)==4 % break on solved to ptr loop\r\n   Ltot=Ltot+Lsol;\r\n   if Lsol\u003eLmax, Lmax=Lsol;end\r\n   break;\r\n  end\r\n  if Lsol==5 % length of 5 and not solved\r\n   solved=0;\r\n   Gmax=0;\r\n   break;\r\n   end\r\n end % while\r\n if ~solved,break;end % terminate case processing\r\nend % for all 1296 cases\r\n\r\nif Gmax==0 % failed Guess max rqmt of 5\r\n fprintf('\\n Solution exceeded 5 guesses\\n');\r\n fprintf('Puzzle %i %i %i %i\\n',m(ptr,:));\r\n fprintf('Guessses and Responses\\n');\r\n fprintf('M%i %i %i %i   P%i %i\\n',[mguess mpegs]');\r\n fprintf('\\n');\r\nend\r\n\r\nif solved\r\n fprintf('Solved in %.2f sec\\n',ztoc)\r\n fprintf('Lmax %i   Ltot %i\\n',Lmax,Ltot)\r\n assert(isequal(solved,1))\r\nelse\r\n fprintf('Solved %i of 1296 cases in %.2f sec\\n',pcase-1,ztoc)\r\n fprintf('Lmax %i   Ltot %i\\n',Lmax,Ltot)\r\n assert(isequal(solved,1))\r\nend\r\n\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":5,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":14,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-06-18T20:04:24.000Z","updated_at":"2025-12-12T15:50:04.000Z","published_at":"2017-06-18T20:39:39.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Mastermind_(board_game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is a code breaking logic puzzle. A pattern of 6 colors(values 1:6) of four positions (1111;1112;....6666) for a possible 6^4(1296) cases is generated. The solver plays a length 4 vector with values 1:6. Accuracy of the play is returned by a count of values in the right position and a count of values(excluding those in the right positions) common to the solution.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[Answer:1233  Guess:3231 Response: 2,2  as x23x are right value/position, 3xx1 are right values.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere guess is a 1x4 vector, mguess is the kx4 matrix of prior guesses and is empty on first try, mpegs is kx2 giving right [value/position, values] for mguess, m is a 1296x4 array [1 1 1 1;...6 6 6 6] of all solutions, mpc is a 1296x1296 array of 0:4 for value/position solutions, mc is a 1296x1296 array of 0:4 for value solutions, mpc5c is state array of a combined mpc and pc of values 0:20, 5*mpc+mc, and v is integer value of solutions 1111 thru 6666.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eChallenge: All 1296 cases will be provided. The maximum number of guesses allowed is 5 with a time limit of 45 seconds. The user will see their prior guesses and the guess response.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eTheory: The optimal minimal guess solution requires only 5 guesses. The\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Mastermind_(board_game)\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e link contains a description of the Knuth Five-guess algorithm. My description of this process with Matlab in mind is: Select the guess that will create the fewest remaining possible solutions for the worst case guess. All guesses are evaluated for the viable remaining possible solutions. Use Matlab array ability to mask the mpc5c variable by remaining cases. The hist function is too slow so create a hist array of states [0:8 10... 20] for all guesses [21,1296]. Sorting of the hist array is used to find a min solution group. Select the first guess with the lowest remaining solutions maximum. A guess that is not a remaining solution is only used if it has the lowest remaining solutions maximum. A later guess is possible if the first size is tied and its second size is less than the second of the earlier guess.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eMastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"custom hist\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"custom hist\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"custom hist\"","","\"","custom hist","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fa2ba8633f0\u003e":null,"#\u003cMathWorks::Search::Field:0x00007fa2ba863350\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fa2ba8629f0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fa2ba863670\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fa2ba8635d0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fa2ba863530\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fa2ba863490\u003e":"tag:\"custom hist\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fa2ba863490\u003e":"tag:\"custom hist\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"custom hist\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"custom hist\"","","\"","custom hist","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fa2ba8633f0\u003e":null,"#\u003cMathWorks::Search::Field:0x00007fa2ba863350\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fa2ba8629f0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fa2ba863670\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fa2ba8635d0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fa2ba863530\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fa2ba863490\u003e":"tag:\"custom hist\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fa2ba863490\u003e":"tag:\"custom hist\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":44239,"difficulty_rating":"hard"}]}}