We wish to find a 4‐peg secret code (using “colors” that we denote by the numbers 0–5) that fits the following clues from three guesses:
- Guess 1: (2, 1, 4, 3) → 3 Black, 0 White
- Guess 2: (4, 1, 3, 0) → 1 Black, 1 White
- Guess 3: (4, 4, 1, 1) → 1 Black, 2 White
A “black” peg means a correct color in the correct position. A “white” peg means that a color is correct but appears in a different position. (No peg is counted twice.)
The strategy is to use the very strong information from Guess 1. It says that the secret code and guess (2, 1, 4, 3) agree in exactly three positions—and no extra “correct but misplaced” colors occur. In other words, the secret is exactly like (2, 1, 4, 3) except one peg is “wrong” (and the wrong peg is not elsewhere in the secret).
Thus the secret code must be one of the following four possibilities (depending on which peg is “wrong”):
- It could differ in peg 1: secret = (X, 1, 4, 3) with X ≠ 2.
- Or differ in peg 2: secret = (2, X, 4, 3) with X ≠ 1.
- Or differ in peg 3: secret = (2, 1, X, 3) with X ≠ 4.
- Or differ in peg 4: secret = (2, 1, 4, X) with X ≠ 3.
We now use Guesses 2 and 3 to “test” these forms.
Testing Possibility: Error in Peg 4
Assume the secret is exactly like Guess 1 except in position 4:
Secret: (2, 1, 4, X) with X ≠ 3.
Check against Guess 2: (4, 1, 3, 0)
-
Compare positions:
- Position 1: secret 2 vs guess 4 → not a black.
- Position 2: secret 1 vs guess 1 → black.
- Position 3: secret 4 vs guess 3 → not a black.
- Position 4: secret X vs guess 0 → black if and only if X = 0. But notice:
- If X were 0 then we’d have a second black in Guess 2. However, Guess 2 must have only 1 black.
- Thus we must have X ≠ 0.
So far, the black count is exactly 1 (at position 2), as required.
-
Now count the white pegs (the colors that are present but in the wrong place). For Guess 2, ignore the peg already counted black (position 2).
- The unmatched secret colors are: {2 (position 1), 4 (position 3), X (position 4)}.
- The unmatched guess colors are: {4 (position 1), 3 (position 3), 0 (position 4)}.
Matching these:
- The guess has 4 (in position 1) and the secret has 4 (in position 3). That makes one white.
- The guess color 3 is not among the unmatched secret colors because the secret already had 3 in Guess 1 only at position 4 (and here secret position 4 is X ≠ 3).
- The guess color 0: if X were 0 we’d have a match, but we already excluded X = 0.
So total for Guess 2 is 1 black and 1 white – exactly what is required.
Now check against Guess 3: (4, 4, 1, 1)
With our candidate secret: (2, 1, 4, X) (remember X ≠ 3 and X ≠ 0)
-
Count black pegs (exact matches by position):
- Position 1: secret 2 vs guess 4 → no.
- Position 2: secret 1 vs guess 4 → no.
- Position 3: secret 4 vs guess 1 → no.
- Position 4: secret X vs guess 1 → this is black if and only if X = 1.
Since Guess 3 must yield exactly 1 black, we are forced to choose
X = 1.
(For any other value, there would be 0 blacks, which would be incorrect.)
-
Now, with X = 1, our candidate secret becomes:
Secret = (2, 1, 4, 1).
-
Count the white pegs:
- Mark the black: Position 4 is black (both are 1).
- Remove that peg from further consideration.
- The remaining secret colors are from positions 1, 2, and 3: {2, 1, 4}.
- The remaining guess colors (from Guess 3) from positions 1, 2, and 3 are: {4, 4, 1}.
Now count the matches using “multiset matching”:
- The guess has a 4 (say, from position 1) and the secret has one 4 (position 3) → one white.
- The guess from position 2 is also a 4 but secret no longer has any extra 4.
- The guess has a 1 (from position 3) and the secret has a 1 (position 2) → second white.
Thus, Guess 3 gives 1 black and 2 white pegs, which is exactly the clue.
Double–check Guess 1 for completeness:
Secret: (2, 1, 4, 1) vs Guess 1: (2, 1, 4, 3).
- Position 1: 2 vs 2 → black.
- Position 2: 1 vs 1 → black.
- Position 3: 4 vs 4 → black.
- Position 4: 1 vs 3 → not matching.
That gives exactly 3 blacks and no extra white (since the "wrong" peg 3 is not present anywhere in secret), as required.
Testing Alternative Possibilities
For completeness one might try the other “error positions” (error in peg 1, peg 2, or peg 3), but a quick check shows that they cannot produce the counts in Guess 2 and Guess 3 simultaneously. (For example, if the error were in position 3 – making the candidate secret (2, 1, X, 3) with X ≠ 4 – then when we score Guess 2 or Guess 3 the numbers of black and white pegs will not match the clues.)
Thus the only candidate that works with every guess is:
Secret = (2, 1, 4, 1).
Final Answer
The secret code is (2, 1, 4, 1).