Editor

Code Breaker Clues

#267 · Games and puzzles · Intermediate · 60 XP · about 12 min

In a code-breaking game one player hides a secret row of coloured pegs and the other tries to guess it. Colours are single capital letters and may repeat, so 'RGGB' is a red, two greens and a blue. After each guess the code keeper gives two clues:

  • exact: how many pegs of the guess have the right colour in the right position;
  • colour only: how many more pegs of the guess have a colour that is in the secret, but in a different position. Each peg of the secret can be matched only once, and pegs already counted as exact are not used again.

Write clues(secret, guess) that returns the tuple (exact, colour_only). Both strings have the same length.

Examples

clues('RGBY', 'RGBY')   # returns (4, 0)
clues('RGBY', 'YBGR')   # returns (0, 4)
clues('RGGB', 'GGRR')   # returns (1, 2)
clues('AABB', 'BBBB')   # returns (2, 0)

In the third example the green in position 1 is exact. The secret's other pegs are R, G and B, and the guess's other pegs are G, R and R: the G matches the secret's spare green and one R matches its red, but the second R has nothing left to match, so the colour-only clue is 2.

Constraints

  • Both strings have 1 to 1,000 pegs.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.