-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_solver.cpp
382 lines (343 loc) · 9.43 KB
/
wordle_solver.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Author: eddy1021
#include <bits/stdc++.h>
// Game config of Wordle(https://www.powerlanguage.co.uk/wordle/)
constexpr int kLen = 5;
constexpr int kTries = 6;
// Filtered wordlist
constexpr char k5LettersWords[] = "5letters_words.txt";
// Show `kCandidateQuery` in case some words are not in the wordlist of Wordle.
constexpr int kCandidateQuery = 3;
// Show all the possible candidates when there are no more than
// `kShowCandidates`.
constexpr int kShowCandidates = 25;
// 3^5
constexpr int kQuinticOfThree = 243;
constexpr int kThreads = 4;
constexpr bool kHardMode = true;
std::vector<std::string> dict;
std::vector<std::string> cand;
int base3[kLen];
#ifdef TEST
std::string test_answer;
#endif // TEST
template <typename... Args>
void Printf(const char *format, Args... args) {
#ifndef TEST
printf(format, args...);
#endif
}
// Sets up the list of words for candidate queries and answers.
void SetUp(const char word_file[], std::vector<std::string> *vec) {
FILE *in = fopen(word_file, "r");
char buf[256];
while (fscanf(in, "%s", buf) == 1) {
assert(strlen(buf) == kLen);
std::string str;
for (int i = 0; i < kLen; ++i) {
str += buf[i];
}
vec->push_back(str);
}
}
void Prepare() {
SetUp(k5LettersWords, &dict);
base3[0] = 1;
for (int i = 1; i < kLen; ++i) {
base3[i] = base3[i - 1] * 3;
}
srand(time(0));
random_shuffle(dict.begin(), dict.end());
}
void Init() { SetUp(k5LettersWords, &cand); }
// Returns the encoded results if we guess `guess` and the answer is `answer`.
int Guess(const std::string &guess, const std::string &answer) {
int encode = 0;
bool used[kLen] = {};
for (int i = 0; i < kLen; ++i) {
if (guess[i] == answer[i]) {
encode += 2 * base3[i];
used[i] = true;
}
}
int has[26] = {};
for (int i = 0; i < kLen; ++i) {
if (!used[i]) {
++has[answer[i] - 'a'];
}
}
for (int i = 0; i < kLen; ++i) {
if (guess[i] == answer[i]) {
continue;
}
if (has[guess[i] - 'a']) {
--has[guess[i] - 'a'];
encode += base3[i];
}
}
return encode;
}
// contains the information of a query and the corresponding result.
struct Hint {
std::string query;
int result;
};
// Returns true if `query` is a valid query in hard mode given the `hints`.
bool Valid(const std::vector<Hint> &hints, const std::string &query) {
for (const auto &hint : hints) {
int has[26] = {};
int matched = 0;
int code = hint.result;
for (int i = 0; i < kLen; ++i) {
if (code % 3 == 2) {
if (query[i] != hint.query[i]) {
return false;
} else {
matched |= (1 << i);
}
} else if (code % 3 == 1) {
++has[hint.query[i] - 'a'];
}
code /= 3;
}
for (int i = 0; i < kLen; ++i) {
if ((matched >> i) & 1) {
continue;
}
if (has[query[i] - 'a']) {
--has[query[i] - 'a'];
}
}
if (std::accumulate(has, has + 26, 0) != 0) {
return false;
}
}
return true;
}
// Finds the current best queries which will result in minimum possible
// largest candidate groups.
// Returns top `num_cand` best queries in case some are not in the wordlist of
// Wordle.
std::vector<std::pair<int, std::string>> FindBestQuery(
const std::vector<Hint> &hints, int num_cand = kCandidateQuery) {
auto find_best = [&](int st, int ed,
std::vector<std::pair<int, std::string>> &cand_query) {
for (int i = st; i < ed; ++i) {
const auto &query = dict[i];
if (kHardMode and not Valid(hints, query)) {
continue;
}
bool cut_early = false;
int groups[kQuinticOfThree] = {};
for (const auto &c : cand) {
int code = Guess(query, c);
++groups[code];
if (static_cast<int>(cand_query.size()) == num_cand and
groups[code] > cand_query.back().first) {
cut_early = true;
break;
}
}
if (cut_early) {
continue;
}
cand_query.push_back(std::make_pair(
*std::max_element(groups, groups + kQuinticOfThree), query));
std::sort(cand_query.begin(), cand_query.end());
if (static_cast<int>(cand_query.size()) > num_cand) {
cand_query.resize(num_cand);
}
}
};
std::vector<std::pair<int, std::string>> cand_query;
#ifdef MULTI_THREAD
std::vector<std::pair<int, std::string>> cands[kThreads];
std::vector<std::thread> threads;
for (int i = 0, st = 0; i < kThreads; ++i) {
int ed = dict.size() * (i + 1) / kThreads;
threads.emplace_back(find_best, st, ed, std::ref(cands[i]));
st = ed;
}
for (int i = 0; i < kThreads; ++i) {
threads[i].join();
for (const auto &c : cands[i]) {
cand_query.push_back(c);
}
}
std::sort(cand_query.begin(), cand_query.end());
if (static_cast<int>(cand_query.size()) > num_cand) {
cand_query.resize(num_cand);
}
#else
find_best(0, dict.size(), cand_query);
#endif
return cand_query;
}
// Gets the actual query player choose.
std::string GetActualQuery() {
std::string query;
while (true) {
Printf("Please enter the chosen query: ");
fflush(stdout);
std::getline(std::cin, query);
if (query.length() != kLen) {
Printf("The query must be a word with length of 5!\n");
continue;
}
bool failed = false;
for (int i = 0; i < kLen; ++i) {
if (!isalpha(query[i])) {
Printf("The query must contain English letter only!\n");
failed = true;
break;
}
if (isupper(query[i])) {
query[i] = query[i] - 'A' + 'a';
}
}
if (failed) {
continue;
}
break;
}
return query;
}
// Gets the results Wordle responds.
int GetQueriedResult() {
int encode = 0;
while (true) {
// print out the message to prompt the user input the response from Wordle.
auto print_message = []() {
static bool first = true;
Printf("Please enter the result separated by space");
// print out the detailed message only for the first time.
if (first) {
Printf(
" (0 for not in any spot, 1 for in wrong spot, 2 for in correct "
"spot). For example, `2 2 0 0 1`");
first = false;
}
Printf(": ");
};
print_message();
fflush(stdout);
std::string line;
std::getline(std::cin, line);
std::stringstream ss(line);
std::vector<int> nums;
int val;
while (ss >> val) {
nums.push_back(val);
}
if (nums.size() != kLen) {
Printf("Expected %d [0,1,2] values\n", kLen);
continue;
}
bool failed = false;
for (int i = 0; i < kLen; ++i) {
if (nums[i] < 0 or nums[i] > 2) {
Printf("Values must be 0, 1, or 2\n");
failed = true;
break;
}
}
if (failed) {
continue;
}
encode = 0;
for (int i = 0; i < kLen; ++i) {
encode += base3[i] * nums[i];
}
break;
}
return encode;
}
// Filters out the possible candidates left.
void FilterCand(std::string queried, int code) {
std::vector<std::string> left_cand;
for (const auto &c : cand) {
int res = Guess(queried, c);
if (res == code) {
left_cand.push_back(c);
}
}
cand.swap(left_cand);
}
int Solve(int max_tries = kTries) {
std::vector<Hint> hints;
for (int turn = 0; turn < max_tries; ++turn) {
Printf("Guess #%d (%d candidates left):\n", turn,
static_cast<int>(cand.size()));
if (cand.empty()) {
Printf("No possible word in the list matched!!!\n");
return -2;
}
if (cand.size() <= kShowCandidates) {
Printf("Candidates: ");
for (const auto &c : cand) {
Printf("%s ", c.c_str());
}
Printf("\n");
}
if (cand.size() == 1u) {
Printf("The only possible answer is: %s\n", cand[0].c_str());
return turn + 1;
}
auto s = std::chrono::system_clock::now();
std::vector<std::pair<int, std::string>> queries = FindBestQuery(hints);
auto t = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = t - s;
Printf("(spent %.3f seconds)\n", elapsed_seconds.count());
for (const auto &query : queries) {
Printf("%s (largest groups left = %d)\n", query.second.c_str(),
query.first);
}
fflush(stdout);
#ifdef TEST
std::string queried = queries[0].second;
int code = Guess(queried, test_answer);
#else
std::string queried = GetActualQuery();
int code = GetQueriedResult();
#endif
hints.push_back({queried, code});
if (code == kQuinticOfThree - 1) {
return turn + 1;
}
FilterCand(queried, code);
}
Printf("Failed to guess the correct word within %d tries...\n", kTries);
return -1;
}
int main() {
Prepare();
#ifdef TEST
std::vector<std::string> all_answers;
SetUp(k5LettersWords, &all_answers);
std::random_shuffle(all_answers.begin(), all_answers.end());
constexpr int kTest = 100;
int tries[kTries + 1] = {};
for (int i = 0; i < kTest; ++i) {
test_answer = all_answers[i];
printf("Trying #%d (answer = %s): ", i, test_answer.c_str());
Init();
int ret = Solve();
if (ret == -1) {
++tries[0];
printf("Failed to find out in %d tries\n", kTries);
} else if (ret == -2) {
assert(false);
} else {
assert(1 <= ret and ret <= kTries);
++tries[ret];
printf("%d tries\n", ret);
}
}
for (int i = 1; i <= kTries; ++i) {
printf("Tries %d: %d\n", i, tries[i]);
}
printf("Failed to found in %d tries: %d\n", kTries, tries[0]);
#else
Init();
Solve();
#endif
}