by somethingsright · 9y ago · 0 comments
I think I read some algorithm for solving mastermind; but never tried understanding mastermind. For the related game of Cows-and-Bulls, there is a elegant algorithm that goes on reducing the list of candidates at each turn. Starting with 9999 potential numbers seems like a lot, but that solution also quickly terminates (7 guesses max, I think).
You can speed up the first step by realizing you don't need to test every code. E.g. 1122 and 1133 are essentially equivalent. You only have five unique choices for the first step 1111, 1112, 1122, 1223, 1234. If you run all codes against those 5, you'll get the first step. Continue doing it for the rest and you'll get the other steps.
Another way to optimize is for the avg number of guesses. The algorithm for this approach is to choose your next guess based on the guess that will give you the maximum amount of information gain (https://en.wikipedia.org/wiki/Entropy_(information_theory)#D...).