# Finish the Mastermind

Mastermind is a well-known game. Here's a brief reminder of the rules:

• There's a secret code consisting of a sequence of five colored circles (items). Colors may appear more than once.
• Your job is to figure out the code. You do so by guessing a sequence of five colors and then getting feedback on your guess.
• The feedback consists of a number of black and white markers:
• Black: you receive a black peg for each item of the correct color in the correct place.
• White: of the remaining items, you receive a white peg for each one of a correct color but in the wrong place.
• You receive at most one peg per item of the correct secret code.
• The order of the black and white pegs has no significance
• Example: suppose the secret code is RGGGG. If you guess RRRRR, you will receive 1 black (for the first R) and 0 whites. If you guess GBBBB, you will get 1 white and 0 blacks.

In this puzzle, you have had a few guesses already and are ready to guess again. Can you determine the secret code?

• Can there be duplicate colors in the code? – Embodiment of Ignorance Aug 8 '19 at 12:13
• Yes. I'll add a point to clarify that. – Dr Xorile Aug 8 '19 at 12:14
• If the solution is ABCDE and I guess AAAAA do I get one black and four white? – LeppyR64 Aug 8 '19 at 12:17
• No. You get one black. See my new edits. – Dr Xorile Aug 8 '19 at 12:18
• Bayl gur ynfg gjb yvarf ner arrqrq evtug? (rot13) – Apollys supports Monica Aug 9 '19 at 0:58

The secret code is:

RED - GREEN - YELLOW - PURPLE - PURPLE

Explanation: At first glance, my gut instinct (soon to be proved wrong) told me this must be impossible! After all:

If the two rightmost pegs have been swapped from guess#3 to guess#4 and we still have 3 black and 2 white markers, then they cannot both have been correct on guess#3 and still both be correct on guess#4 (contradiction).
Equally, they cannot both have been mis-positioned on guess#3 and still both be mis-positioned on guess#4 (since that would require 2 of the first 3 pegs to be mis-positioned also, but we only have 2 white markers, not 4).

However, then I realised I had completely overlooked the possibility that:

The two rightmost pegs are supposed to be identical, and so only one of the green and purple was correct and the other mis-positioned both times.

Which colour could this be? Well, we know from guess#3 and guess#4 that we have to have:

1 red, 1 yellow, 1 green and 2 purples in total (since all 5 markers are present, meaning we have all the right colours, just not all in the right positions yet...)

So:

The two rightmost pegs must be PURPLE, since all other colours are only represented once.

It now follows that the other three must be ordered as:

RED - GREEN - YELLOW... since on guess#3 and guess#4 two of the black markers must have been referring to the first three pegs, and could not have been referring to the purple in slot 2 (which should have been in slot 4 or 5, and was therefore represented by a white marker). Therefore the red must be in slot 1, the yellow must be in slot 3 and - by deduction - the green goes in slot 2.

Meaning the final solution is as outlined at the top.

• great minds think alike +1 – Omega Krypton Aug 8 '19 at 12:18
• Great job. This was an actual game on Simon Tatham's puzzles and I also had the “wait, that's impossible” moment. Thought I'd share. Of course, it took SE almost no time to get it... – Dr Xorile Aug 8 '19 at 14:10