Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active March 9, 2022 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpruss/df791e48286550f474151abf2a7c07ee to your computer and use it in GitHub Desktop.
Save arpruss/df791e48286550f474151abf2a7c07ee to your computer and use it in GitHub Desktop.
from __future__ import print_function
import sys
LENGTH = 5
RIGHT_LETTER_RIGHT_PLACE = 1
RIGHT_LETTER_WRONG_PLACE = 2
SCORE_NONE = 0
def evaluate(word, guess):
eval = [0,] * LENGTH
for i in range(LENGTH):
c = guess[i]
if word[i] == c:
eval[i] = RIGHT_LETTER_RIGHT_PLACE
for i in range(LENGTH):
if not eval[i]:
c = guess[i]
count = word.count(c)
if count:
already = 0
for j in range(LENGTH):
if eval[j] and guess[j] == c:
already += 1
if already < count:
eval[i] = RIGHT_LETTER_WRONG_PLACE
eval = tuple(eval)
return eval
def binSize(haystack, guess):
dict = {}
for word in haystack:
e = evaluate(word, guess)
if e in dict:
dict[e] += 1
else:
dict[e] = 1
return max(dict[a] for a in dict)
def getBin(haystack, guess, evaluation):
bin = []
for word in haystack:
if evaluate(word, guess) == evaluation:
bin.append(word)
return tuple(bin)
cachedGuesses = {}
def getBestGuess(haystack, guesses, verbose=False):
bestBin = len(haystack)
if bestBin == 12972 and 'serai' in guesses:
return 'serai'
if len(guesses) == 12972:
haystack = tuple(haystack)
if haystack in cachedGuesses:
if verbose: print('*')
return cachedGuesses[haystack]
bestGuess = None
count = 0
for guess in guesses:
s = binSize(haystack, guess)
if s < bestBin or (s == bestBin and bestGuess not in haystack and guess in haystack):
bestBin = s
bestGuess = guess
if verbose:
count += 1
if count % 100 == 0:
print("%3.1f\r" % (100 * count / float(len(guesses))))
assert(bestGuess)
if len(guesses) == 12972:
cachedGuesses[haystack] = bestGuess
return bestGuess
def play(answer, haystack, supply=None, count=0, verbose=False):
count += 1
if len(haystack) == 1:
if verbose:
print(haystack[0]+"!")
assert(haystack[0] == answer)
return count
if supply is None:
supply = haystack
guess = getBestGuess(haystack, supply)
if verbose:
print(guess)
if answer == guess:
return count
return play(answer, getBin(haystack, guess, evaluate(answer, guess)), supply=supply, count=count, verbose=verbose)
allWords = set()
with open("full.txt") as f:
for w in f:
w = w.strip()
if len(w) == 5:
allWords.add(w)
answerWords = set()
with open("answers.txt") as f:
for w in f:
w = w.strip()
if len(w) == 5:
allWords.add(w)
answerWords.add(w)
allWords = tuple(sorted(allWords))
answerWords = tuple(sorted(answerWords))
#bestGuess = getBestGuess(allWords, allWords, verbose=True)
hardestWord = None
hardestCount = 0
for w in answerWords:
c = play(w, allWords, supply=allWords, verbose=False)
if c > hardestCount:
hardestCount = c
hardestWord = w
print(":", c, w)
print("Hardest: ", hardestWord, hardestCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment