Skip to content

Instantly share code, notes, and snippets.

@firebus
firebus / mastermind.py
Created March 21, 2012 22:07 — forked from gvx/mastermind.py
An implementation of Knuth's five-guess algorithm to solve a mastermind code [CC0]
from itertools import product
possible = [''.join(secret) for secret in product('ABCDEF', repeat=4)]
results = [(right, wrong) for right in range(5) for wrong in range(5 - right) if not (right == 3 and wrong == 1)]
def score(secret, guess):
first = len([speg for speg, gpeg in zip(secret, guess) if speg == gpeg])
return first, sum([min(secret.count(j), guess.count(j)) for j in 'ABCDEF']) - first
def solve(secrets, attemptfun, first=False):