Vanadium 50 said:
Well, it's hard to tell since we seem to be playing Calvinball, but I think "greedy" has at least two meanings: one would be to get the highest possible number of hits on this turn, and the other would be to remove as many incorrect solutions from the search space as possible.
The example is likely confusing because it is an example of non-optimal play:
After 848 there are only two possibilities: 846 and 818. Guessing 777 gives no new information. Guessing 717 or 776 or 778 etc. would all you to infer the answer in the next turn, but at this point no strategy is better than guessing one number and if it isn't right, guessing the other.
You know it is not 818 because 815 contains only one digit in the right place. That sequence of guesses and answers has only one remaining solution.
I will spitball how I think it might work.
I think I would create two 10x10x10 matrix which would correspond to the answer set. And have two options a 0 (for incorrect), or a 1 (for correct). Start with everything set to 0 in Matrix-A and 1 in Matrix-B. Matrix A has to be re-set to 1 after each round.
Any answer that is ZERO allows 3 sets to be marked as incorrect in the Matrix-B. So if you guess 1,1,1 and get zero, then 1,a,b is incorrect, a,1,b is incorrect, and a,b,1 is incorrect.
Any answer that is ONE allows 3 sets to be marked as correct in the Matrix-A. So if you guess 1,1,1 and get ONE, then 1,a,b is correct. As is a,1,b and a,b,1. In Matrix B, you change the 1,1,a and 1,a,1 and a,1,1 (those would have required a TWO).
Then compare Matrix B to Matrix A. Change any 1's in B to 0's if they are 0's in A.
Any answer that is TWO requires marking some other space as correct in Matrix-A. Again using 1,1,1 and getting TWO ... 1,1,a is marked as correct. 1,a,1 and a,1,1 get marked as correct. Set 1,1,1 as incorrect.
Then compare Matrix B to Matrix A. Change any 1's in B to 0's if they are 0's in B.
Codewise, you keep looping the same three counters from 0-9. You know you have an answer when the sum of the Matrix-B elements is 1.
Each guess allows you to mark elements as incorrect, in a freshly initialized Matrix A. Then transfer the information of disallowed answers into the matrix that started with everything allowed.
I think a person keeps track of the things that are excluded, or required, and tests the possible right answers against the others. Although this is not Mastermind, before you make the next guess, you see how the previous scoring would look against it. If it is not perfectly consistent with previous scoring, you try again. You have to exclude all the guesses that the current information excludes. I'm not sure how we do that in our head, but coding, I would do it with record keeping 10x10x10 matrices, and an i,j,k nested loop system.
In prior programs I have written, I have found that it is often necessary to mave more matrices than you expect. It might be good to have a 3rd. The code can often be very compact if you get the matrices and tests precisely thought out. Because you don't want to change the matrices to lose previous info, you often need a BUNCH of placeholder Matrices.
For example, the two guesses 848 and 816 in the example. The first allows 8,4,a and 8,a,8 and a,4,8. The second allows 8,1,a and 8,a,6 and a,1,6. The second guess allows a set that intersects with the set allowed by the first. If your code tracks the DISallowed sets, and keeps them cumulatively, then there ends with one answer. The other guess in that series of 815 and an answer of ONE, allows the sets 8,not-1,not-5 and not-8,1,not-5 and not-8,not-1,5 to intersect with those.
I hope that helps ... I know I have mistakes, but the gist is that if you set up the right loops and matrices, the code is not that crazy.