[Math] Knuth’s algorithm for Mastermind question

algorithmspuzzle

I'm reading about Knuth's algorithm to solve the mastermind game, so I've looked in wikipedia and read the pseudo-code (http://en.wikipedia.org/wiki/Mastermind_(board_game)#Five-guess_algorithm).

I have a question about step 3 in the algorithm, "Remove from S any code that would not give the same response if it (the guess) were the code".

How would this estimation (of which codes would not give the same response) be "performed"? Obviously we can't ask for responses for each of the codes in S, that would just be brute force.

Thanks!

Best Answer

The idea is that $S$ maintains the set of all possible codes that you could be guessing at, so after each guess you iterate through the remaining elements of $S$ seeing how they stack up against the (most recent) guess you've made and rejecting those that don't fit the response you got. For instance, if your first guess is 1122 and you receive a response of one white and one black peg, then you'd remove 1134 from $S$ (since if that had been the actual code you would have gotten a different response, namely two black pegs), but you would keep 2134 in $S$ (since that code is consistent with the response of one black, one white to 1122). You're constantly asking the question 'if this were the master's code, what would the response to my guess have been?' of all codes in $S$, and that calculation — while inefficient — doesn't require any guesses at all.

Related Question