-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_tree.py
168 lines (146 loc) · 7.24 KB
/
guess_tree.py
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
import string
import pickle
import time
letters = list(string.ascii_lowercase)
empty_dictionary_string = '{}'
def load_in_pickle(data):
with open(f'pickled_dictionaries/{data}.pkl', 'rb') as f:
return pickle.load(f)
def remove_dict_of_words_from_remaining(from_dict,target_dict):
for key in from_dict:
if key in target_dict:
del target_dict[f'{key}']
return target_dict
def get_intersection_between_two_dict(from_dict,target_dict):
return len(from_dict.keys() & target_dict.keys())
for letter in letters:
for number in range(0,5):
exec(f"""{letter}{number} = load_in_pickle('{letter}{number}')""")
def save_as_pickle(data,data_as_string):
with open(f'cached_dict/{data_as_string}.pkl', 'wb') as f:
pickle.dump(data, f)
all_wordle_words_dict = load_in_pickle('all_wordle_words_dict')
class new_feedback:
def __init__(self,input_word,input_feedback,input_remaining_wordle_words):
self.word = input_word
self.feedback = input_feedback
# print(self.feedback)
self.remaining_wordle_words = input_remaining_wordle_words
self.running_dict = {}
# print(f'before trim: {len(self.remaining_wordle_words)}')
def trim_eliminated_words(self):
self.running_dict.clear()
for index in range(0,len(self.feedback)):
letter_result = self.feedback[index]
if letter_result == '0':
for number in range(0,5):
exec(f'self.running_dict.update({self.word[index]}{number})')
if letter_result == '2':
for letter in letters:
if letter != self.word[index]:
exec(f'self.running_dict.update({letter}{index})')
if letter_result == '1':
exec(f'self.running_dict.update({self.word[index]}{index})')
return get_intersection_between_two_dict(self.running_dict,self.remaining_wordle_words)
def get_number_of_remaining_words(self):
# print(f'after trim: {len(self.remaining_wordle_words)}')
return self.trim_eliminated_words()
class new_branch:
def __init__(self,input_word,input_remaining_wordle_words):
self.remaining_wordle_words_branch = input_remaining_wordle_words.copy()
self.word = input_word
# print(self.word)
def get_max_for_all_feedback(self):
all_possibilities_after_feedback = []
# start_time = time.time()
for place_1 in range(0,3):
for place_2 in range(0,3):
for place_3 in range(0,3):
for place_4 in range(0,3):
for place_5 in range(0,3):
feedback_this_branch = f'{place_1}{place_2}{place_3}{place_4}{place_5}'
# print(len(self.remaining_wordle_words_branch))
feedback = new_feedback(self.word,feedback_this_branch,self.remaining_wordle_words_branch)
all_possibilities_after_feedback.append(feedback.get_number_of_remaining_words())
# print(all_possibilities_after_feedback)
# end_time = time.time()
# print(f'for 1 word: time = {end_time-start_time}s')
max_for_this_word = min(all_possibilities_after_feedback)
# print(all_possibilities_after_feedback)
# print(max_for_this_word)
return max_for_this_word
class guesser:
def __init__(self):
#load in pickled dictionaries
self.branches_all_wordle_words_dict = all_wordle_words_dict.copy()
self.remaining_possible_wordle_words = all_wordle_words_dict.copy()
self.how_many_guesses_so_far = 1
self.guess = 'sauce'
self.feedback = ''
self.word_found = False
self.removed_dicts = []
def modify_dict_of_remaining_words(self,feedback):
for index in range(0,len(feedback)):
result = feedback[index]
if result == '0':
for number in range(0,5):
exec(f'self.remaining_possible_wordle_words = remove_dict_of_words_from_remaining({self.guess[index]}{number},self.remaining_possible_wordle_words)')
if result == '2':
for letter in letters:
if letter != self.guess[index]:
exec(f'self.remaining_possible_wordle_words = remove_dict_of_words_from_remaining({letter}{index},self.remaining_possible_wordle_words)')
if result == '1':
exec(f'self.remaining_possible_wordle_words = remove_dict_of_words_from_remaining({self.guess[index]}{index},self.remaining_possible_wordle_words)')
# if len(self.remaining_possible_wordle_words) == 2:
# self.guess = list(self.remaining_possible_wordle_words.keys())[0]
# self.how_many_guesses_so_far += 1
# print(f'guess #{self.how_many_guesses_so_far}: {self.guess}!')
# if self.manually_get_feedback() == '22222':
# self.modify_dict_of_remaining_words(self.feedback)
# self.word_found = True
# self.guess = list(self.remaining_possible_wordle_words.keys())[0]
# print(f'word found! word: {self.guess}')
# else:
# self.modify_dict_of_remaining_words(self.feedback)
# self.how_many_guesses_so_far += 1
# self.guess = list(self.remaining_possible_wordle_words.keys())[0]
# print(f'guess #{self.how_many_guesses_so_far}: {self.guess}!')
# self.word_found = True
if len(self.remaining_possible_wordle_words) == 1:
self.word_found = True
self.how_many_guesses_so_far += 1
self.guess = list(self.remaining_possible_wordle_words.keys())[0]
print(f'word found! word: {self.guess}')
def update_branch(self):
index = 0
percent_complete = 0
for key in list(all_wordle_words_dict):
branch = new_branch(key,self.remaining_possible_wordle_words)
self.branches_all_wordle_words_dict[key] = branch.get_max_for_all_feedback()
index += 1
if index%23 == 0:
percent_complete += 1
print(f'search {percent_complete}% complete')
def rearrange_all_wordle_words_dict(self):
self.branches_all_wordle_words_dict = dict(sorted(self.branches_all_wordle_words_dict.items(), key=lambda item: item[1]))
def generate_guess(self):
self.rearrange_all_wordle_words_dict()
print(self.branches_all_wordle_words_dict)
print(self.remaining_possible_wordle_words)
self.guess = list(self.branches_all_wordle_words_dict.keys())[-1]
self.how_many_guesses_so_far += 1
print(f'guess #{self.how_many_guesses_so_far}: {self.guess}!')
def manually_get_feedback(self):
print("0=grey,1=yellow,2=green")
self.feedback = input()
return self.feedback
print('guess #1: sauce!')
g = guesser();
while not g.word_found:
g.modify_dict_of_remaining_words(g.manually_get_feedback())
if g.word_found == True:
print(f'word found in {g.how_many_guesses_so_far} guesses!')
break
g.update_branch()
g.generate_guess()
save_as_pickle(g.branches_all_wordle_words_dict,f'{g.guess}{g.feedback}')