13
13
import time
14
14
15
15
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
-
28
16
def on_release (key ):
29
17
# Start button
30
18
if key == keyboard .Key .esc :
@@ -44,6 +32,7 @@ def get_row_results(game_row):
44
32
for tile in tiles :
45
33
evaluation .append (eval_to_int [tile .get_attribute ("data-state" )])
46
34
print (evaluation )
35
+
47
36
return tuple (evaluation )
48
37
49
38
@@ -54,113 +43,110 @@ def enter_word(word):
54
43
time .sleep (2 )
55
44
56
45
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
80
52
81
- #From the basic list of words, return all the words with 5 characters.
82
- def get_list_of_five ():
83
53
84
- filtered_list = []
54
+ def check_letter_in_word (letter , word ):
55
+ if letter in word :
56
+ return True
57
+ else :
58
+ return False
85
59
86
- file = open ("words_alpha.txt" ,"r" )
87
60
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 ))
95
65
96
- file .close ()
97
- return filtered_list
66
+ return list_of_words
98
67
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)
102
70
103
71
104
- #Algorithm that solve the wordle
105
- #In the string we get :
72
+ # Algorithm that solve the wordle
73
+ # In the string we get :
106
74
# _ : the letter is not on the right position (present)
107
75
# * : the letter is not in the solution (absent)
108
76
# X : the letter is on the right position (correct)
109
77
# letters_not_in_response, is a list which keeps track of the allowed letters
110
78
# letter_not_in_position, is a list which keeps track of the letters in bad position
111
79
# 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 ):
114
81
print ("solving_algorithm start" )
115
82
116
- list_of_words = get_list_of_five ()
83
+ list_of_words = get_list_of_words ()
117
84
absent_letters = set ([])
118
85
present_letters = set ([])
119
86
120
- solution = "mbron" # developpement solution
87
+ solution = "tbest" # developpement solution
121
88
solving_string = "*****"
122
89
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"
128
93
print ("letters in the right position : " , solution [letter ])
129
94
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
132
97
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
136
101
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 :]
139
104
140
-
141
105
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
144
109
buffer_list = []
145
110
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 ))
153
114
154
115
list_of_words = buffer_list
155
116
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 )
159
121
print ("solving string :" , solving_string )
160
- #print(list_of_words)
122
+ # print(list_of_words)
161
123
print ("lenght of list" , len (buffer_list ))
162
124
163
125
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
+
164
150
165
151
if __name__ == "__main__" :
166
- solving_algorithm ()
152
+ main ()
0 commit comments