Skip to content

Commit 2e2023f

Browse files
committed
Build solving algorithm - Regex
1 parent eeff382 commit 2e2023f

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

wordle_solver/wordle_solver.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ def enter_word(word):
4343
time.sleep(2)
4444

4545

46+
def create_regex(solving_string, present_letters):
47+
regex = ""
48+
for letter in range(len(solving_string)):
49+
if solving_string[letter] == "*":
50+
regex += "[a-z]"
51+
elif solving_string[letter] == "_":
52+
regex += \
53+
"[^" + str(list(present_letters)).replace("'", "").replace(",", "").replace("[", "").replace(" ", "")
54+
else:
55+
regex += "[" + solving_string[letter] + "]"
56+
return regex
57+
58+
4659
# Check word length
4760
def check_word_length(word):
4861
if len(word) != 5:
@@ -77,53 +90,59 @@ def get_list_of_words():
7790
# letters_not_in_response, is a list which keeps track of the allowed letters
7891
# letter_not_in_position, is a list which keeps track of the letters in bad position
7992
# For exemple, "A_A_*A", letters_not_in_response = ['B'], letter_not_in_position = ['K'].
80-
def solving_algorithm(word, res):
93+
def solving_algorithm(word, res, list_of_words, present_letters, absent_letters):
8194
print("solving_algorithm start")
8295

83-
list_of_words = get_list_of_words()
84-
absent_letters = set([])
85-
present_letters = set([])
86-
87-
solution = "tbest" # developpement solution
8896
solving_string = "*****"
8997

9098
for letter in range(len(word)):
9199
print("letter : ", word[letter])
92100
if res[letter] == 1: # Case when the status of the letter is "correct"
93-
print("letters in the right position : ", solution[letter])
94-
print("GREATOOOOO")
101+
print("the letter is correct")
95102
solving_string = solving_string[:letter] + word[letter] + solving_string[letter + 1:]
96-
elif res[letter] == 0: # status is present
103+
if word[letter] in present_letters:
104+
present_letters.remove(word[letter])
105+
elif res[letter] == 0: # Case when the status of the letter is "present" (present but at the wrong position)
97106
print("the letter is present")
98107
solving_string = solving_string[:letter] + "_" + solving_string[letter + 1:]
99108
present_letters.add(word[letter])
100-
else: # status is absent
109+
else: # Case when the status of the letter is "absent"
101110
print("the letter is absent")
102-
absent_letters.add(word[letter]) # Case when the status of the letter is 'absent'
103-
solving_string = solving_string[:letter] + "*" + solving_string[letter + 1:]
111+
if word[letter] not in present_letters:
112+
absent_letters.add(word[letter])
113+
solving_string = solving_string[:letter] + "*" + solving_string[letter + 1:]
104114

105-
print("Update list of word")
115+
print("Update list of words")
106116
print("length of list", len(list_of_words))
107117

108118
# Update list of words
109-
buffer_list = []
110-
111119
for absent in absent_letters:
112120
print(absent)
113-
list_of_words = list(filter(check_letter_in_word(absent), list_of_words))
121+
list_of_words = list(filter(lambda x_word: not check_letter_in_word(absent, x_word), list_of_words))
122+
for present in present_letters:
123+
print(present)
124+
list_of_words = list(filter(lambda x_word: check_letter_in_word(present, x_word), list_of_words))
114125

115126
print("letter not in the right position : ", present_letters)
127+
print(create_regex(solving_string, present_letters))
116128
print("Letters with absent status", absent_letters)
117129
print("solving string :", solving_string)
118-
# print(list_of_words)
130+
print("list of words : ", list_of_words)
119131
print("length of list", len(list_of_words))
120132

133+
return list_of_words
134+
121135

122136
def main():
123137
# Start the browser
124138
browser = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
125139
browser.get("https://www.nytimes.com/games/wordle/index.html")
126140

141+
list_of_words = get_list_of_words()
142+
absent_letters = set([])
143+
present_letters = set([])
144+
guesses_left = 2
145+
127146
# Wait for start
128147
with keyboard.Listener(on_release=on_release, suppress=True) as listener:
129148
print("Starting")
@@ -139,13 +158,16 @@ def main():
139158
first_string = "tests"
140159
enter_word(first_string)
141160
res = get_row_results(game_rows[0])
142-
# solving_algorithm(first_string, res)
161+
list_of_words = solving_algorithm(first_string, res, list_of_words, present_letters, absent_letters)
162+
guesses_left -= 1
143163

144164
time.sleep(1)
145165

146-
second_string = "trees"
147-
enter_word(second_string)
148-
get_row_results(game_rows[1])
166+
for i in range(guesses_left, 0, -1):
167+
enter_word(list_of_words[0])
168+
res = get_row_results(game_rows[i])
169+
solving_algorithm(list_of_words[0], res, list_of_words, present_letters, absent_letters)
170+
time.sleep(1)
149171

150172

151173
if __name__ == "__main__":

0 commit comments

Comments
 (0)