Skip to content

Commit 310e46b

Browse files
committed
Filter tests
1 parent 980016c commit 310e46b

File tree

1 file changed

+70
-84
lines changed

1 file changed

+70
-84
lines changed

wordle_solver/wordle_solver.py

Lines changed: 70 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@
1313
import time
1414

1515

16-
# Check word length
17-
def check_word_length(word):
18-
if len(word) != 5:
19-
return False
20-
else:
21-
return True
22-
23-
24-
list_of_words = open("words_alpha.txt", "r").read().strip().splitlines()
25-
list_of_words = list(filter(check_word_length, list_of_words))
26-
27-
2816
def on_release(key):
2917
# Start button
3018
if key == keyboard.Key.esc:
@@ -44,6 +32,7 @@ def get_row_results(game_row):
4432
for tile in tiles:
4533
evaluation.append(eval_to_int[tile.get_attribute("data-state")])
4634
print(evaluation)
35+
4736
return tuple(evaluation)
4837

4938

@@ -54,113 +43,110 @@ def enter_word(word):
5443
time.sleep(2)
5544

5645

57-
def main():
58-
# Start the browser
59-
browser = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
60-
browser.get("https://www.nytimes.com/games/wordle/index.html")
61-
62-
# Wait for start
63-
with keyboard.Listener(on_release=on_release) as listener:
64-
listener.join()
65-
print("Starting")
66-
67-
# Get the game rows
68-
game_rows = browser.find_elements(By.CLASS_NAME, 'Row-module_row__dEHfN')
69-
70-
first_string = "tests"
71-
enter_word(first_string)
72-
get_row_results(game_rows[0])
73-
74-
time.sleep(1)
75-
76-
second_string = "trees"
77-
enter_word(second_string)
78-
get_row_results(game_rows[1])
79-
46+
# Check word length
47+
def check_word_length(word):
48+
if len(word) != 5:
49+
return False
50+
else:
51+
return True
8052

81-
#From the basic list of words, return all the words with 5 characters.
82-
def get_list_of_five():
8353

84-
filtered_list = []
54+
def check_letter_in_word(letter, word):
55+
if letter in word:
56+
return True
57+
else:
58+
return False
8559

86-
file = open("words_alpha.txt","r")
8760

88-
for line in file :
89-
data = line.strip().split(',')
90-
#print('word : ',data)
91-
#print('length : ',len(line))
92-
if (len(line) == 6) :
93-
#print("le mot ",data," est ajoute")
94-
filtered_list.append(line)
61+
# From the basic list of words, return all the words with 5 characters.
62+
def get_list_of_words():
63+
list_of_words = open("words_alpha.txt", "r").read().strip().splitlines()
64+
list_of_words = list(filter(check_word_length, list_of_words))
9565

96-
file.close()
97-
return filtered_list
66+
return list_of_words
9867

99-
#Print to verify filtered_list only contains 5 characters words.
100-
#for line in filtered_list :
101-
#print(line,end="")
68+
# Print to verify filtered_list only contains 5 characters words.
69+
# print("list of words : ", list_of_words)
10270

10371

104-
#Algorithm that solve the wordle
105-
#In the string we get :
72+
# Algorithm that solve the wordle
73+
# In the string we get :
10674
# _ : the letter is not on the right position (present)
10775
# * : the letter is not in the solution (absent)
10876
# X : the letter is on the right position (correct)
10977
# letters_not_in_response, is a list which keeps track of the allowed letters
11078
# letter_not_in_position, is a list which keeps track of the letters in bad position
11179
# For exemple, "A_A_*A", letters_not_in_response = ['B'], letter_not_in_position = ['K'].
112-
def solving_algorithm():
113-
80+
def solving_algorithm(word, res):
11481
print("solving_algorithm start")
11582

116-
list_of_words = get_list_of_five()
83+
list_of_words = get_list_of_words()
11784
absent_letters = set([])
11885
present_letters = set([])
11986

120-
solution = "mbron"#developpement solution
87+
solution = "tbest" # developpement solution
12188
solving_string = "*****"
12289

123-
test_1 = "abdki"
124-
125-
for letter in range(len(test_1)):
126-
print("letter : ",test_1[letter])
127-
if(test_1[letter] == solution[letter]): #Case when the status of the letter is "correct"
90+
for letter in range(len(word)):
91+
print("letter : ", word[letter])
92+
if res[letter] == 1: # Case when the status of the letter is "correct"
12893
print("letters in the right position : ", solution[letter])
12994
print("GREATOOOOO")
130-
solving_string = solving_string[:letter]+test_1[letter]+solving_string[letter+1:]
131-
elif():#status is present
95+
solving_string = solving_string[:letter] + word[letter] + solving_string[letter + 1:]
96+
elif res[letter] == 0: # status is present
13297
print("the letter is present")
133-
solving_string = solving_string[:letter]+"_"+solving_string[letter+1:]
134-
present_letters.add(test_1[letter])
135-
else :#status is absent
98+
solving_string = solving_string[:letter] + "_" + solving_string[letter + 1:]
99+
present_letters.add(word[letter])
100+
else: # status is absent
136101
print("the letter is absent")
137-
absent_letters.add(test_1[letter]) #Case when the status of the letter is 'absent'
138-
solving_string = solving_string[:letter]+"*"+solving_string[letter+1:]
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:]
139104

140-
141105
print("Update list of word")
142-
print("lenght of list", len(list_of_words))
143-
#Update list of words
106+
print("length of list", len(list_of_words))
107+
108+
# Update list of words
144109
buffer_list = []
145110

146-
for word in list_of_words :
147-
print(word)
148-
for absent in absent_letters :
149-
print(absent)
150-
if absent in word :
151-
break
152-
buffer_list.append(word)
111+
for absent in absent_letters:
112+
print(absent)
113+
list_of_words = list(filter(check_letter_in_word(absent, list_of_words), list_of_words))
153114

154115
list_of_words = buffer_list
155116

156-
157-
print("letter not in the right position : ",present_letters)
158-
print("Letters with absent status",absent_letters)
117+
118+
119+
print("letter not in the right position : ", present_letters)
120+
print("Letters with absent status", absent_letters)
159121
print("solving string :", solving_string)
160-
#print(list_of_words)
122+
# print(list_of_words)
161123
print("lenght of list", len(buffer_list))
162124

163125

126+
def main():
127+
# Start the browser
128+
browser = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
129+
browser.get("https://www.nytimes.com/games/wordle/index.html")
130+
131+
# Wait for start
132+
with keyboard.Listener(on_release=on_release) as listener:
133+
listener.join()
134+
print("Starting")
135+
136+
# Get the game rows
137+
game_rows = browser.find_elements(By.CLASS_NAME, 'Row-module_row__dEHfN')
138+
139+
first_string = "tests"
140+
enter_word(first_string)
141+
res = get_row_results(game_rows[0])
142+
solving_algorithm(first_string, res)
143+
144+
time.sleep(1)
145+
146+
second_string = "trees"
147+
enter_word(second_string)
148+
get_row_results(game_rows[1])
149+
164150

165151
if __name__ == "__main__":
166-
solving_algorithm()
152+
main()

0 commit comments

Comments
 (0)