-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordleSolver.py
77 lines (50 loc) · 2.11 KB
/
wordleSolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import collections
import re
def getMostCommonLetters(wordList: list) -> dict:
letterCounts = collections.Counter()
for word in wordList:
letterCounts.update(set(word))
return {l[0]: l[1] for l in letterCounts.most_common()}
def getWordWeight(word: str, letterCounts: dict) -> int:
wordWeight = 0
for letter in set(word):
wordWeight += getLetterWeight(letter, letterCounts)
return wordWeight
def getLetterWeight(letter, letterCounts) -> int:
try:
weight = letterCounts[letter]
except KeyError:
weight = 0
return weight
def getWeightedWordList(wordList: dict) -> dict:
return {word: getWordWeight(word, getMostCommonLetters(wordList)) for word in wordList}
def getSortedWordList(weightedWords: dict) -> list:
return [x[0] for x in sorted(weightedWords.items(), key=lambda item: item[1], reverse=True)]
def getRecommendation(pattern, wordList):
try:
pattern = re.compile(pattern)
candidates = [word for word in wordList if pattern.match(word)]
return candidates[0]
except:
print("Sad panda. Unable to compile the pattern.")
def __promptAndRespond(sortedList:list):
knownLetters = input("Enter the word, using hyphens for letters you don't know.: ")
exclusions = input("What letters are known to not appear in the word?: ")
print(f"Try {getRecommendation(__buildPattern(knownLetters, exclusions), sortedList)}")
def __buildPattern(knownLetters: str, exclusions = None):
if exclusions:
newPattern = knownLetters.replace('-', f'[^{exclusions}]{{1}}')
else:
newPattern = knownLetters.replace('-', '.')
return newPattern
if __name__ == "__main__":
print("Booting up. Just a sec while I load all the things.")
wordList = [word.rstrip() for word in open('/Users/barrett/Desktop/wordleSolver/dictionary.txt')]
sortedList = getSortedWordList(getWeightedWordList(wordList))
print(f"Ready to go. Start with {sortedList[0]}")
while True:
try:
__promptAndRespond(sortedList)
except KeyboardInterrupt:
print("Bye")
exit()