-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordle_Solver.py
More file actions
157 lines (132 loc) · 6.17 KB
/
Wordle_Solver.py
File metadata and controls
157 lines (132 loc) · 6.17 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Class to solve wordles
class WordleBot:
# Initialize a wordle bot with a list of 5 letter words and a known letter dictionary:
# Additionally, add a ranking system for each letter in the alphabet:
def __init__(self, letters = 5):
# Constant number of letters in a word
WordleBot.letters = letters
# List of english letters & followup dictionary to rank those letters
self.letterList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
self.letterRank = {}
# List of usable 5 letter words
self.wordList = []
# List of letters that do not show
self.blackLetters = []
# List of letters who's position is not known
self.yellowLetters = []
# Dictionary of letters who's position has been correctly identified
self.greenLetters = {}
# Access the words document
wordsAddress = open("Words.txt")
# For each line in the word document:
for line in wordsAddress:
# If the word is 5 letters long excluding the \n:
if len(line) == letters + 1:
# Append it to the word list, removing the \n
self.wordList.append(line[:-1])
print("Wordle Solver Initialized")
print("Ranking Letters...\n")
# For each letter in the letter list:
for letter in self.letterList:
# Instantiate a count for how many times that letter is used and total letters
rankLetterCount = 0
allLetterCount = 0
# For each word in the word list:
for word in self.wordList:
# For each letter in the word:
for wLet in word:
# If the letter is equal to the current ranked letter; add to the counter
if letter == wLet:
rankLetterCount += 1
# Count the letter
allLetterCount += 1
# Assign that letter to the dictionary with it's percentage ranking
self.letterRank[letter] = rankLetterCount / allLetterCount
# Print statement setup:
def __repr__(self):
return f'{len(self.wordList)} words remaining.'
# Method to obtain a guess:
def getGuesses(self):
# Instantiate a grading system
grades = {"" : -99999}
# Instantiate a counter for items deleted
deleted = 0
# For each of the guesses:
for guess in self.wordList:
# Keep a new score
score = 0
# For each letter of the guess:
for letter in guess:
# If the guess contains a letter not used; remove the letter from guess list, and word list
if letter in self.blackLetters:
# To avoid error; check to see if the word is still in the word list
if guess in self.wordList:
self.wordList.remove(guess)
deleted += 1
if deleted % 1000 == 0:
print(f'{deleted} items removed...')
continue
# If the guess contains a yellow letter; add 2 to it's score
elif letter in self.yellowLetters:
score += 2 * self.letterRank[letter]
# If the guess contains a green letter; add 1 to it's score
elif letter in self.greenLetters:
if guess.index(letter) == self.greenLetters[letter]:
score += 1 * self.letterRank[letter]
# If it is an unused letter, add a special calculation to its score
else:
score += (WordleBot.letters - 2 - (1 / WordleBot.letters * len(self.yellowLetters)) - len(self.greenLetters) // 1) * self.letterRank[letter]
# If there is more than one instance of this letter; subtract 1 point from it's score
if guess.count(letter) > 1:
score -= 1
# Add the guess, and it's score to the grading system
grades[guess] = score
# Instantiate the selected word
selectedWord = ""
# For each graded word:
for word in grades.keys():
# If this graded word is better than the next; select this word
if grades[selectedWord] < grades[word]:
selectedWord = word
# Return the selected word
return selectedWord
# Method to set a Black Letter
def blackLet(self, char, index):
self.blackLetters.append(char)
# Remove all words that have that letter
changed = True
while changed:
changed = False
for word in self.wordList:
if char in word:
self.wordList.remove(word)
changed = True
# Method to set a Yellow Letter
def yellowLet(self, char, index):
# Obtain the index of the yellow letter
self.yellowLetters.append(char)
# Remove all words that have that letter in that index
changed = True
while changed:
changed = False
for word in self.wordList:
if word[index] == char:
self.wordList.remove(word)
changed = True
# Method to set a Green Letter
def greenLet(self, char, index):
# Remove any instances of this letter in the yellow letters
if char in self.yellowLetters:
self.yellowLetters.remove(char)
self.greenLetters[char] = index
# Remove all words that have that letter, but not at the right index
changed = True
while changed:
changed = False
for word in self.wordList:
if char in word and word[index] != char:
self.wordList.remove(word)
changed = True
# Method to remove a guess
def removeGuess(self, word):
self.wordList.remove(word)