Problem 44239. Mastermind IV: Optimal Solver  max of 5 guesses
Mastermind 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.
Answer:1233 Guess:3231 Response: 2,2 as x23x are right value/position, 3xx1 are right values.
[guess]=solve_mastermind(mguess,mpegs,m,mpc,mc,mpc5c,v)
where 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.
Challenge: 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.
Theory: The optimal minimal guess solution requires only 5 guesses. The Mastermind link contains a description of the Knuth Fiveguess 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.
Mastermind challenges: [Solve no limit, Solve in 8 or less, Solve in 1 given a guess pattern, Solve in 5 or less]
Solution Stats
Problem Comments

1 Comment
Please do not insert a timer on your test suite. Cody already has one, so why not use it? I was under the impression that my implementation of Knuth's algorithm was wrong, when in fact it was only slow. Or if you do insert a timer, please, provide a better feedback. For instance: "your solution cracked all codes under 5 steps, but it will fail to solve them all on time".
Solution Comments
Show commentsProblem Recent Solvers13
Suggested Problems

1374 Solvers

Determine Whether an array is empty
773 Solvers

Compute a dot product of two vectors x and y
986 Solvers

Sum the numbers on the main diagonal
578 Solvers

Calculate the Number of Sign Changes in a Row Vector (No Element Is Zero)
707 Solvers
More from this Author308
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!