Skip to content

Instantly share code, notes, and snippets.

@estama
Last active January 7, 2022 23:40
Show Gist options
  • Select an option

  • Save estama/a3c25100207b107b2e1d0172d3b96613 to your computer and use it in GitHub Desktop.

Select an option

Save estama/a3c25100207b107b2e1d0172d3b96613 to your computer and use it in GitHub Desktop.
# 12345
greenletters = "....."
yelloletters=[ ".....",
".....",
".....",
"....." ]
greyletters = ""
f = {"a":7.8, "b":2, "c":4, "d":3.8, "e":11, "f":1.4, "g":3, "h":2.3, "i":8.6,
"j":0.21, "k":0.97, "l":5.3, "m":2.7, "n":7.2, "o":6.1, "p":2.8, "q":0.19, "r":7.3,
"s":8.7, "t":6.7, "u":3.3, "v":3.3, "w":0.91, "x":0.27, "y":1.6, "z":0.44}
yset = {c for c in ''.join(yelloletters) if c != '.'}
gset = {c for c in greyletters}
yforbid = [set() for x in range(5)]
for y in yelloletters:
for i in xrange(len(y)):
c = y[i]
if c != '.':
yforbid[i].add(c)
def validwords():
words = open("wordle.txt", "r")
for w in words:
w = w.strip()
foundyellow = set(yset)
for i in xrange(len(w)):
c = w[i]
yfc = yforbid[i]
gc = greenletters[i]
if (c in gset) or (c in yfc) or (gc != '.' and gc != c):
foundyellow.add(1)
break
foundyellow.discard(c)
if len(foundyellow) > 0:
continue
yield w
fl = {}
totalchars = 0
for w in validwords():
for i in xrange(len(w)):
c = w[i]
gc = greenletters[i]
if c == gc:
continue
fl[c] = fl.get(c, 0) + 1
totalchars += 1
for c, freq in fl.iteritems():
f[c] = f[c] * 0.01 + 100 * float(freq) / totalchars
tscore = -1000
bestword = ""
fchars = set()
for w in validwords():
fchars.clear()
wscore = 0
for i in xrange(len(w)):
c = w[i]
wscore = wscore + f[c]
if c in fchars:
wscore -= 100
fchars.add(c)
if wscore > tscore:
tscore = wscore
bestword = w
print(bestword)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment