-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordleSolver.py
More file actions
282 lines (236 loc) · 8.46 KB
/
WordleSolver.py
File metadata and controls
282 lines (236 loc) · 8.46 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import random
dictionary = []
lettersmin = {}
lettersmax = {}
knownletterpositions = {}
knownfalseletterpositions = {}
def getdictionary(length):
if not os.path.exists("StrippedDictionaries"):
os.makedirs("StrippedDictionaries")
path = os.path.join("StrippedDictionaries", str(length) + "_dictionary.txt")
if not os.path.exists(path):
newdictionary = open(path, "w")
masterdictionary = open("dictionary.txt", "r").read().split("\n")
for word in masterdictionary:
if len(word) == length:
newdictionary.write(word.lower() + "\n")
#last one is removed, because the automatic generation always makes the last row be empy.
return open(path, "r").read().split("\n")[:-1]
#takes words out of a dictionary based on results from wordle
def stripdictionary(dictionary, word, evaluation):
#global ones are used in case we need to generate all possible letter combinations
global lettersmin
global lettersmax
global knownletterpositions
global knownfalseletterpositions
#current ones are used to eliminate ones that don't fit from the library
currentlettersmin = {}
currentlettersmax = {}
currentknownletterpositions = {}
currentknownfalseletterpositions = {}
#lettersmin and lettersmax
for i in range(len(word)):
zeroes = 0
onesandtwos = 0
for i2 in range(len(word)):
if word[i2] == word[i]:
if evaluation[i2] == '0':
zeroes += 1
else:
onesandtwos += 1
if onesandtwos != 0:
if word[i] not in lettersmin:
lettersmin[word[i]] = 0
if lettersmin[word[i]] < onesandtwos:
lettersmin[word[i]] = onesandtwos
if word[i] not in currentlettersmin:
currentlettersmin[word[i]] = 0
if currentlettersmin[word[i]] < onesandtwos:
currentlettersmin[word[i]] = onesandtwos
if zeroes != 0:
#if lettersmax doesn't have the current character we cant compare it, so we add it.
if word[i] not in lettersmax:
lettersmax[word[i]] = onesandtwos
elif lettersmax[word[i]] > onesandtwos:
lettersmax[word[i]] = onesandtwos
if word[i] not in currentlettersmax:
currentlettersmax[word[i]] = onesandtwos
elif currentlettersmax[word[i]] > onesandtwos:
currentlettersmax[word[i]] = onesandtwos
#Known- and and knownfalsepositions
for i in range(len(word)):
if evaluation[i] == '1' or evaluation[i] == 0:
if word[i] not in knownfalseletterpositions:
knownfalseletterpositions[word[i]] = []
knownfalseletterpositions[word[i]].append(i)
if word[i] not in currentknownfalseletterpositions:
currentknownfalseletterpositions[word[i]] = []
currentknownfalseletterpositions[word[i]].append(i)
if evaluation[i] == '2':
if word[i] not in knownletterpositions:
knownletterpositions[word[i]] = []
knownletterpositions[word[i]].append(i)
if word[i] not in currentknownletterpositions:
currentknownletterpositions[word[i]] = []
currentknownletterpositions[word[i]].append(i)
#remove words with too many instances of a letter
for i in range(len(dictionary)-1, -1, -1):
for char in currentlettersmax:
if dictionary[i].count(char) > currentlettersmax[char]:
dictionary.pop(i)
break
#remove words with too few instances of a letter
for i in range(len(dictionary)-1, -1, -1):
for char in currentlettersmin:
if dictionary[i].count(char) < currentlettersmin[char]:
dictionary.pop(i)
break
#remove word with letters in the wrong places
for i in range(len(dictionary)-1, -1, -1):
for char in currentknownfalseletterpositions:
breakfurther = False
for pos in currentknownfalseletterpositions[char]:
if dictionary[i][pos] == char:
dictionary.pop(i)
breakfurther = True
break
if breakfurther:
break
#remove word with wrong letters in known positions
for i in range(len(dictionary)-1, -1, -1):
for char in currentknownletterpositions:
breakfurther = False
for pos in currentknownletterpositions[char]:
if dictionary[i][pos] != char:
dictionary.pop(i)
breakfurther = True
break
if breakfurther:
break
return dictionary
#takes words out based on historic data from previous guesses. Used to filter generated "words"
def stripdictionaryglobal(dictionary):
global lettersmin
global lettersmax
global knownletterpositions
global knownfalseletterpositions
#remove words with too many instances of a letter
for i in range(len(dictionary)-1, -1, -1):
for char in lettersmax:
if dictionary[i].count(char) > lettersmax[char]:
dictionary.pop(i)
break
#remove words with too few instances of a letter
for i in range(len(dictionary)-1, -1, -1):
for char in lettersmin:
if dictionary[i].count(char) < lettersmin[char]:
dictionary.pop(i)
break
#remove word with letters in the wrong places
for i in range(len(dictionary)-1, -1, -1):
for char in knownfalseletterpositions:
breakfurther = False
for pos in knownfalseletterpositions[char]:
if dictionary[i][pos] == char:
dictionary.pop(i)
breakfurther = True
break
if breakfurther:
break
#remove word with wrong letters in known positions
for i in range(len(dictionary)-1, -1, -1):
for char in knownletterpositions:
breakfurther = False
for pos in knownletterpositions[char]:
if dictionary[i][pos] != char:
dictionary.pop(i)
breakfurther = True
break
if breakfurther:
break
return dictionary
print("Welcome to wordle solver.")
length = -1
while(length == -1):
print("Please input the length of the word")
lengthinput = input()
if lengthinput.isnumeric():
length = int(lengthinput)
dictionary = getdictionary(length)
print("Dictionary length: " + str(len(dictionary)))
print("now to get started, you should start with a word that has as many different characters as possible. Here are a few suggestions.")
starterdictionary = open("starters.txt", "r").read().split("\n")
for i in range(len(starterdictionary)-1, -1, -1):
if len(starterdictionary[i]) != length:
starterdictionary.pop(i)
if len(starterdictionary) > 0:
for word in starterdictionary:
print(word)
else:
for i in range(10):
suggestion = ""
while True:
suggestion = random.choice(dictionary)
breakingout = True
for char in suggestion:
if suggestion.count(char) > 1:
breakingout = False
if breakingout:
break
print(suggestion)
while True:
validinput = False
wordinput = ""
while not validinput:
print("What word did you use?")
wordinput = input().lower()
validinput = len(wordinput) == length
validinput = False
evaluationinput = ""
while not validinput:
print("What did we learn? write a string of numbers, with 0 meaning the letter is not in the word, 1 meaning it is but isn't in the correct position and 2 meaning it is fully correct.")
evaluationinput = input()
validinput = len(evaluationinput) == length and len(evaluationinput.replace("0", "").replace("1", "").replace("2", "")) == 0
if "0" not in evaluationinput and "1" not in evaluationinput:
print("Wohoo!")
break
dictionary = stripdictionary(dictionary, wordinput, evaluationinput)
if len(dictionary) == 0:
print("Sorry, it seems the word isn't in my dictionary.")
print("We can however make word combinations which do still fit. I will process these now, this will take a moment, depending on the word length.")
characters = "abcdefghijklmnopqrstuvwxyz"
#Strip out characters we know don't exists
for i in range(len(characters)-1, -1, -1):
if i in lettersmax and lettersmax[characters[i]] == 0:
characters = characters[:i] + characters[i+1:]
indexes = []
for i in range(length):
indexes.append(0)
dictionary = []
while(indexes[0] < len(characters)):
string = ""
for i in range(len(indexes)):
string += characters[indexes[i]]
dictionary.append(string)
indexes[-1] += 1
for i in range(len(indexes)-1, 0, -1):
if indexes[i] == len(characters):
indexes[i] = 0
indexes[i-1] += 1
dictionary = stripdictionaryglobal(dictionary)
print("I found " + str(len(dictionary)) + " word combinations which fit. here they are:")
for word in dictionary:
print(word)
break
if len(dictionary) == 1:
print("It seems that the only possible answer is:")
print(dictionary[0])
elif len(dictionary) <= 10:
print("We are getting close, only " + str(len(dictionary)) + " possibilities left. Here are all the possibilities I know;")
for i in range(len(dictionary)):
print(dictionary[i])
else:
print("there are " + str(len(dictionary)) + " possibilities left. Here are some of them:")
for i in range(10):
print(dictionary[int(len(dictionary) * random.random())])