24
6
Your goal is to write a program that will solve any Mastermind puzzle in 6 or less moves.
Background
Mastermind is a board game. The goal of the game is to exactly guess the combination (colours and order) of 4 coloured pegs hidden by the other player. When a guess is made, the other player responds with between 0 and 4 white and or red pegs. A red peg is where the colour and location are correct. A white peg is where the colour is represented in the remaining pieces, but is in the incorrect location. If there are duplicate colours in the guess, there will only be one peg awarded per corresponding colour in the secret. (So - if the secret contained 1 Blue, and the guess had 2 blues with one in the correct location, there would be one red peg given). There are 6 different colors, and duplicates may be used.
So for instance, a game might go as follows: (Assuming the solution is Red Green Green Blue)
1: Blue Purple Black Green - 2 white pegs
2: Green Red Black Blue - 2 white pegs, 1 red peg
3: Green Green Green Blue - 3 red pegs
4: Red Green Green Blue - 4 red pegs
The rules are expanded on Wikipedia
Requirements
- The program must read from stdin, and write to stdout
- I will be using numbers for simplicity instead of colors. The combination to guess will be 4 numbers between 1 and 6
They must output their guesses as a series of 4 space separated numbers from 1 to 6 concluding with a newline. For instance:
1 5 2 2 \n
The program will subsequently receive as input after its guess 2 integers between 0 and 4 separated by a space and concluding with a newline. The first will be the amount of white pegs, the second the amount of red pegs.
- On an input of "0 4" (4 red pegs), the program must terminate
- The program must be able to solve any puzzle in less then 6 turns (your program giving output, followed by the response input is 1 turn). There is no bonus (due to complexity of proof) for being able to solve it in less.
- The solution must be completely internal and included in the source. Standard Libraries only are permitted. The solution may therefore not rely on any other files (such as dictionaries), or the internet.
Example Input/Output
> is your programs output
< is the responding input
Solution is 1 5 6 6
> 1 2 3 4
< 0 1
> 4 1 6 6
< 1 2
> 1 6 5 6
< 2 2
> 1 5 6 6
< 0 4
Scoring
- This is pure and simple Code Golf. The shortest solution in bytes wins.
This is my first Code Golf question. My apologies if I have done something wrong, but I've attempted as well as possible to ensure that there is absolutely no ambiguity, and prevent as much rules lawyering as possible. If I have been ambiguous or unclear, please feel free to ask questions.
1In your example input/output shouldn't
1 2 3 4return0 1? – Gaffi – 2012-03-21T14:57:46.2231And in the example text, shouldn't "Green Green Green Blue" give a white peg as well (for the first Green)? EDIT - Wikipedia clarifies that no white should be given, as you wrote. But I think the white/black rules should be explicitly stated in the question. – ugoren – 2012-03-21T17:23:05.863
How slow will it be allowed to work? – ceased to turn counterclockwis – 2012-03-21T18:24:01.537
@Gaffi - Absolutely right - fixed – lochok – 2012-03-21T20:04:37.610
@leftaroundabout - Time was intentionally omitted as to not discourage slow, and very short implementations. Also as I don't know the speeds of some languages that people use (Brainf**k or Golfscript) for instance, and didn't want to exclude on that basis. – lochok – 2012-03-21T20:18:52.033
@ugoren - I've stated them, but please feel free to let me know if they are still unclear – lochok – 2012-03-21T20:23:31.107
Well golfscript is out since it can't do interactive IO – gnibbler – 2012-03-21T21:02:03.650
@gnibbler perhaps it's slow enough to pretend... – ceased to turn counterclockwis – 2012-03-21T21:08:36.800
1The rules for white pegs are not stated here. Suppose you chose 1234 and I guess 5611. Both my 1s are the right color in the wrong place, so from the way you stated the rules I'd say I get 2 whites. But no - Wikipedia says it's 1 white. The incorrect method is also easier to program (but Steven Rumbalski correctly implemented Wikipedia's rules). – ugoren – 2012-03-22T05:33:46.870
@ugoren - I attempted to clarify the rules to match the rules of the game. Feel welcome to edit it if it's still unclear – lochok – 2012-03-22T05:49:43.183
I know it's only an (very old) example, but wouldn't 4166 be a poor guess after 1234 yielded only 1 match? Surely it would keep only one color from the previous guess. Am I missing something? – Igby Largeman – 2013-09-09T02:51:59.497
I gave that example solely as it would demonstrate the requirements and input/output well. Not as a recommended approach. – lochok – 2013-09-09T12:45:59.343