Programming contests

50 Programming Exercise for Beginners

January 2, 2019 12:00 AM – December 31, 2019 12:00 AM

Mastermind

In Mastermind, the player has to find a sequence of colored balls of a certain length in the least possible number of attempts. They always guess the entire ball sequence by giving a sequence of colors. After each guess, a feedback is given to them about the number of balls that are correct in both color and position, and the number of balls of a correct color in the wrong position.

For example, if the ball sequence to be found is blue-green-red-yellow, and the player guesses the sequence brown-green-blue-yellow, then two balls are correct in color and position, and one is correct in color but not in position. After the guess, this information is shared with the player.

The basic game is played with four balls, but there are many variations of the game, which can be obtained by changing the length of the ball sequence.

Write a program that reads two strings of the same length from each line of the standard input, each containing only uppercase letters of the English alphabet, up to 100 characters, until end-of-file (EOF): the first contains letters indicating the colors of the sequence to be found, and the second is the player’s guess for this sequence. You may assume that each color is represented by a unique letter.

For each line of the input, write two integers to the standard output based on the rules of the Mastermind game: first the number of balls that are correct in both color and position, then the number of balls of a correct color in the wrong position. In each line, the two numbers should be separated by a single space character.

Sample Input

  1. KZPS BZKS
  2. ABCDEF ABCDEF
  3. FEDCBA ABCDEF
  4. ABCD AAAA
  5. ABCD XYAA
  6. XYAA ABCD
  7. AABBCC ABCABC
  8. CBACBA ABCABC
  9. ABCABC AACCBB
download as text file

Output for Sample Input

  1. 2 1
  2. 6 0
  3. 0 6
  4. 1 0
  5. 0 1
  6. 0 1
  7. 2 4
  8. 2 4
  9. 3 3
download as text file

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019