-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordle.py
More file actions
224 lines (165 loc) · 6.13 KB
/
wordle.py
File metadata and controls
224 lines (165 loc) · 6.13 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
import io
import time
import pandas as pd
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
CONSTANTS = {
"wordle_url": "https://www.nytimes.com/games/wordle/index.html",
"words_csv": "https://raw.githubusercontent.com/tabatkins/wordle-list/main/words",
"word_list": ["adieu", "golps", "crwth"]
}
incorrect_letters = set()
correct_letters = set()
counts = {}
previous_correct_letter = set()
ans_list = ["." for i in range(5)]
#####################Driver Functions#############################
def get_letter_data_state(driver_instance, grid, _):
state = driver_instance.execute_script(
"return arguments[0].shadowRoot.querySelector('div > game-tile:nth-child({})')".format(
_ + 1
),
grid,
)
data_state = state.get_attribute("evaluation")
letter = state.get_attribute("letter")
return letter, data_state
def get_keys(driver_instance):
game = get_game(driver_instance)
keyboard = game.find_element(By.TAG_NAME, "game-keyboard")
keys = driver_instance.execute_script(
"return arguments[0].shadowRoot.getElementById('keyboard')", keyboard
)
return keys
def get_board_grid(driver_instance, word):
game = get_game(driver_instance)
grid_row = game.find_element(By.CSS_SELECTOR, f"game-row[letters='{word}']")
return grid_row
def get_game(driver_instance):
game_app = driver_instance.find_element(By.TAG_NAME, "game-app")
game = driver_instance.execute_script(
"return arguments[0].shadowRoot.getElementById('game')", game_app
)
return game
def goaway_popup(driver_instance):
driver_instance.find_element(By.TAG_NAME, "body").click()
def init_driver():
s = Service(ChromeDriverManager().install())
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
# option.add_argument("-headless")
option.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=s, options=option)
driver.get(CONSTANTS["wordle_url"])
time.sleep(1)
return driver
def press_key(keys, letter):
keys.find_element(By.CSS_SELECTOR, f'button[data-key="{letter}"]').click()
###########################################Utility Functions###############################################
def get_words_df():
s = requests.get(CONSTANTS["words_csv"]).content
df = pd.read_csv(io.StringIO(s.decode("utf-8")), names=["Words"], header=None)
df["Words"] = pd.Series(df["Words"], dtype="string")
return df["Words"]
def get_word_for_guess(Words):
positions = {
1: {},
2: {},
3: {},
4: {},
5: {},
}
for word in Words:
for idx, ch in enumerate(word):
positions[idx + 1][ch] = positions[idx + 1].get(ch, 0) + 1
freq_distribution = 0
word_to_send = ""
for word in Words:
for idx, ch in enumerate(word):
freq_distribution += positions[idx + 1][ch]
word_to_send = word
break
for word in Words:
temp = 0
for idx, ch in enumerate(word):
temp += positions[idx + 1][ch]
if temp > freq_distribution:
freq_distribution = temp
word_to_send = word
return word_to_send
def play_wordle():
Words = get_words_df()
idx = 0
word_to_send = CONSTANTS["word_list"][idx]
driver = init_driver()
goaway_popup(driver)
for attempt in range(1, 7):
print(Words)
Words = send_word_and_update_list(
driver_instance=driver, word=word_to_send, Words=Words
)
ans = get_current_ans()
if len(ans) == 5:
time.sleep(10)
return {"result": "success", "word": ans, "attempts": attempt}
idx += 1
if idx <= len(CONSTANTS["word_list"]) - 1:
word_to_send = CONSTANTS["word_list"][idx]
else:
word_to_send = get_word_for_guess(Words)
time.sleep(2)
return {"result": "failure", "attempts": attempt}
def get_current_ans():
return "".join(ans_list).replace(".", "")
def send_word_and_update_list(driver_instance, word, Words):
keys = get_keys(driver_instance)
for letter in word:
press_key(keys, letter)
time.sleep(0.1)
press_key(keys, "↵")
time.sleep(2)
grid = get_board_grid(driver_instance, word)
for _, letter in enumerate(word):
Words = update_words(driver_instance, Words, grid, _)
return Words
def update_words(driver_instance, Words, grid, _):
letter, data_state = get_letter_data_state(driver_instance, grid, _)
counts = {}
if data_state == "correct" or data_state == "present":
correct_letters.add(letter)
previous_correct_letter.add(letter)
counts[letter] = counts.get(letter, 0) + 1
if data_state == "correct":
ans_list[_] = letter
else:
temp = ["." for i in range(5)]
temp[_] = letter
Words = Words[Words.str.match(r"^{}$".format("".join(temp))) == False]
elif data_state == "absent" and (
letter in ans_list or letter in previous_correct_letter
):
temp = ["." for i in range(5)]
temp[_] = letter
Words = Words[Words.str.match(r"^{}$".format("".join(temp))) == False]
elif data_state == "absent" and letter not in previous_correct_letter:
incorrect_letters.add(letter)
if len(incorrect_letters):
for letter in incorrect_letters:
Words = Words[Words.str.contains(letter) == False]
if len(correct_letters):
for letter in correct_letters:
Words = Words[Words.str.contains(letter) == True]
if "".join(ans_list) != ".....":
Words = Words[Words.str.match(r"{}".format("".join(ans_list))) == True]
incorrect_letters.clear()
correct_letters.clear()
for key, value in counts.items():
if value >= 2:
Words = Words[Words.str.count(key) == value]
return Words
if __name__ == "__main__":
print(play_wordle())