Created
February 2, 2011 07:42
-
-
Save kevinburke/807376 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import itertools | |
combos = ['a','b','c','d','e','f'] | |
solver_length = 4 | |
#initialize all possible choices | |
c = [] | |
for i in itertools.permutations(combos, solver_length): | |
c.append(i) | |
print len(c) | |
print c[0] | |
print "what is the response?" | |
def possible_match(guess, place, in_ans, possible): | |
'''returns True if possible fits guess pattern''' | |
#possible needs to have exact no. letters matching the guess | |
off = 0 | |
for i,v in zip(guess,possible): | |
if i != v: | |
off += 1 | |
if off != solver_length - place: | |
return False | |
else: | |
guess_copy = list(guess) | |
similar = 0 | |
for letter in possible: | |
if letter in guess_copy: | |
similar += 1 | |
guess_copy.remove(letter) | |
if similar != in_ans: | |
return False | |
if place == 0: | |
for i,v in zip(guess, possible): | |
if i == v: | |
return False | |
if in_ans == 0: | |
for letter in possible: | |
if letter in guess: | |
return False | |
elif in_ans == solver_length: | |
for letter in possible: | |
if letter not in guess: | |
return False | |
return True | |
guess = 'abcd' | |
guesses = [] | |
while True: | |
print "Your guess should be " + guess | |
place, in_ans = raw_input().split() | |
place = int(place) | |
in_ans = int(in_ans) | |
guesses.append((guess, place, in_ans)) | |
for poss in c: | |
for a_guess in guesses: | |
if not possible_match(a_guess[0], a_guess[1], a_guess[2], poss): | |
c.remove(poss) | |
#move to next possible | |
break | |
print "possible combos: " + str(len(c)) | |
if len (c) == 1: | |
print "Congrats! The answer is " + "".join(c[0]) | |
break | |
elif not c: | |
print "No valid solutions (?)" | |
elif len(c) < 20: | |
for i in c: | |
print "".join(i) | |
if len(guesses) == 1: | |
guess = 'bcde' | |
elif len(guesses) == 2 and solver_length == 4: | |
guess = 'cdef' | |
else: | |
guess = "".join(c[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment