This is a code golf challenge to see the shortest implementation of an automated guesser in the pen and paper game bulls and cows.

The game works as follows:

A (usually 4 digit) secret code with only distinct integers 0-9 is picked by one player. A second player guesses codes that each receive a rank. This rank contains 1 bull for every digit that is correctly placed in the guess, and 1 cow for every digit in the guess that is in the secret code, but not in the correct location within the code.

The code should assume a format as follows:

  • The combination will be a 4 digit number containing only distinct digits 0-9. (eg. 0123 is valid, 0112 is not)

  • After each guess, you will be given a string as a response telling you how many bulls and how many cows are in your answer. It will be formatted as xByC, in which x is the number of bulls and y is the number of cows. (i.e. 4B0C means you have won). You can assume that a made-up method called "rateGuess", which takes your guess as a string will provide the response.

  • \$\begingroup\$ I will edit the question to contain the game rules. This is the game that was the basis for mastermind, but the questions are different. The question you linked to asks you to rank the guesses, while this question asks you to guess the code assuming a given ranking method. \$\endgroup\$ Jan 27, 2015 at 22:16
  • 3
    \$\begingroup\$ Okay, so you want us to write a Mastermind solver. In that case, the question is, how good does it have to be? Does it have to play optimally? And I think we've had several Mastermind-solving questions as well. There's this for code golf and this for a code challenge. \$\endgroup\$ Jan 27, 2015 at 22:19
  • \$\begingroup\$ I hadn't seen any mastermind solver questions, but that could be because I am used to referring to the game as bulls and cows, and I didn't see any results for that name. The only criteria that the answer is ranked by in this case will be size of implementation. \$\endgroup\$ Jan 27, 2015 at 22:21
  • 3
    \$\begingroup\$ But if you don't require optimal play, I could just submit random guesses. There's got to be some (objective) criterion for how well it should perform if you want to make this a code golf. \$\endgroup\$ Jan 27, 2015 at 22:22

1 Answer 1


Pyth - 11 bytes

This will eventually guess the correct answer. On average 2500 tries. Literally just takes random guesses no matter what the input is. Since you can't define rateGuess in pyth just takes (uneeded) input from stdin and outputs to stdout.


O         Pick random
 f        filter
  ql{T4   set length is four (checks for duplicates)
  ^  4    Caretesian product with 4 repeats
   UT     Unary range till 10

Not the answer you're looking for? Browse other questions tagged or ask your own question.