Skip to content

Instantly share code, notes, and snippets.

@bzed
Last active January 18, 2022 14:59
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 bzed/cda8c65080e22b7afb235288a5362b34 to your computer and use it in GitHub Desktop.
Save bzed/cda8c65080e22b7afb235288a5362b34 to your computer and use it in GitHub Desktop.
Solve wordle superfast
#!/usr/bin/python3
# THIS CODE IS A MESSY QUICK AND DIRTY HACK
# AND WON'T BE COMMENTED FURTHER :)
import re
from collections import OrderedDict
from operator import itemgetter
with open('/usr/share/dict/american-english') as f:
words = f.read()
wordls = re.findall('^[a-zA-Z]{5}$', words, re.MULTILINE)
wordls = [x.lower() for x in wordls]
wordls = sorted(wordls)
lettercount = { chr(i): 0 for i in range(ord('a'), ord('z')+1)}
for word in wordls:
for i in word:
lettercount[i] += 1
lettercount = OrderedDict(sorted(lettercount.items(), key = itemgetter(1), reverse = True))
print(lettercount)
wordls_string = "\n".join(wordls)
start_count = 0
while start_count < len(lettercount) - 5:
loop_start = start_count
for i in range(5, len(lettercount)):
w_re = '^[{}]{{5}}$'.format(''.join(list(lettercount)[start_count:start_count + i]))
perfect_word = re.findall(w_re, wordls_string, re.MULTILINE)
perfect_word = [ w for w in perfect_word if len(set(list(w)))==5 ]
if perfect_word:
start_count = start_count + i
print(perfect_word)
break
start_count = max(start_count, loop_start + 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment