-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_selector.cpp
392 lines (354 loc) · 13.2 KB
/
wordle_selector.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
383
384
385
386
387
388
389
390
391
392
#include "wordle_selector.h"
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
template <typename IterType>
string RandomWordleSelector<IterType>::select(IterType begin, IterType end, size_t rangeSize,
const vector<WordleKnown>& knowns, size_t guessNum) {
advance(begin, getRandom(begin, end, rangeSize));
return *begin;
}
template <typename IterType>
size_t RandomWordleSelector<IterType>::getRandom(IterType begin, IterType end, size_t rangeSize) const {
srand(time(NULL));
return rand() % rangeSize;
}
template <typename IterType>
string EnhancedRandomWordleSelector<IterType>::select(IterType begin, IterType end, size_t rangeSize,
const vector<WordleKnown>& knowns, size_t guessNum) {
string selection;
do {
selection = RandomWordleSelector<IterType>::select(begin, end, rangeSize, knowns, guessNum);
} while (containsDoubleLetter(selection) && containsOneVowel(selection));
return selection;
}
template <typename IterType>
bool EnhancedRandomWordleSelector<IterType>::containsDoubleLetter(const string& word) const {
for (size_t i = 0; i < word.size(); i++) {
for (size_t j = i+1; j < word.size(); j++) {
if (word[i] == word[j]) {
return true;
}
}
}
return false;
}
template <typename IterType>
bool EnhancedRandomWordleSelector<IterType>::isVowel(char letter) const {
return (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u' || letter == 'y');
}
template <typename IterType>
bool EnhancedRandomWordleSelector<IterType>::containsOneVowel(const string& word) const {
size_t numVowels = 0;
for (auto& letter : word) {
numVowels = isVowel(letter) ? numVowels+1 : numVowels;
}
return numVowels <= 1;
}
////////////////
template <typename IterType>
string MostCommonLetterWordleSelector<IterType>::select(IterType begin, IterType end, size_t rangeSize,
const vector<WordleKnown>& knowns, size_t guessNum) {
clearOldState();
this->m_guessNum = guessNum;
m_knowns = knowns;
m_iterBegin = begin;
m_iterEnd = end;
computeFrequencyMap();
#if PRINT_GUESSES == true
printCandidates();
#endif
return getBestCandidate();
}
template <typename IterType>
void MostCommonLetterWordleSelector<IterType>::clearOldState() {
m_knowns.clear();
m_frequencyMapLetter.clear();
m_wordScore.clear();
m_sortedWords.clear();
}
template <typename IterType>
bool MostCommonLetterWordleSelector<IterType>::containsAllHints(const string& word) const {
// vector<char> knownHints;
// cout << "word: " << word << " hints: ";
// for (size_t i = 0; i < LETTER_COUNT; i++) {
// if (this->m_knowns[i].result != WordleResult::BLACK) {
// knownHints.push_back(this->m_knowns[i].letter);
// cout << this->m_knowns[i].letter << ",";
// }
// }
// cout << endl;
// for (size_t i = 0; i < LETTER_COUNT; i++) {
// for (auto it = knownHints.begin(); it != knownHints.end(); it++) {
// if (word[i] == *it) {
// knownHints.erase(it);
// break;
// }
// }
// }
// return knownHints.empty();
return true;
}
template <typename IterType>
void MostCommonLetterWordleSelector<IterType>::printCandidates() const {
cout << "Remaining Candidates: (" << m_sortedWords.size() << ")" << endl;
size_t i = 0;
for (auto it = m_sortedWords.begin(); it != m_sortedWords.end(); it++) {
cout << " " << it->word << ":" << it->score << endl;
i++;
if (i == PRINT_GUESSES_SIZE) {
cout << "... (" << (m_sortedWords.size() - PRINT_GUESSES_SIZE) << ") skipped" << endl;
break;
}
}
}
template <typename IterType>
string MostCommonLetterWordleSelector<IterType>::getBestCandidate() const {
auto it = m_sortedWords.begin();
// for (;!containsAllHints(it->word); it++) {
// cout << "nope." << endl;
// }
return it->word;
}
template <typename IterType>
char MostCommonLetterWordleSelector<IterType>::getMostCommonLetter() const {
size_t highest = 0;
char mostCommon = '0';
for (auto it = m_frequencyMapLetter.begin(); it != m_frequencyMapLetter.end(); it++) {
if (it->second > highest) {
highest = it->second;
mostCommon = it->first;
}
}
if (!mostCommon) {
if (DEBUG) cerr << "Error: [selector] no most common letter" << endl;
throw;
}
return mostCommon;
}
template <typename IterType>
void MostCommonLetterWordleSelector<IterType>::computeFrequencyMap() {
if (m_alphabetWordScore.size() == 0) {
computeFrequencyMapInternal(m_alphabetFrequencyMapLetter, m_alphabetWordScore);
m_frequencyMapLetter = m_alphabetFrequencyMapLetter;
m_wordScore = m_alphabetWordScore;
} else {
computeFrequencyMapInternal(m_frequencyMapLetter, m_wordScore);
}
sortWordsByFrequency();
}
template <typename IterType>
void MostCommonLetterWordleSelector<IterType>::sortWordsByFrequency() {
for (auto wordIt = m_wordScore.begin(); wordIt != m_wordScore.end(); wordIt++) {
auto ws = WordScore();
ws.word = wordIt->first;
ws.score = wordIt->second;
m_sortedWords.insert(ws);
}
}
template <typename IterType>
void NaiveMostCommonLetterWordleSelector<IterType>::computeFrequencyMapInternal(unordered_map<char, size_t>& letterMap,
unordered_map<string, size_t>& wordScore) {
// Compute letter scores
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
for (auto& c : *wordIt) {
if (letterMap.find(c) == letterMap.end()) {
letterMap[c] = 0;
}
letterMap[c]++;
}
}
// Compute word scores
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
size_t score = 0;
set<char> wordLetters;
for (auto& c : *wordIt) {
if (wordLetters.find(c) == wordLetters.end()) {
wordLetters.insert(c);
score += letterMap[c];
}
}
wordScore.insert({*wordIt, score});
}
}
template <typename IterType>
void ImprovedMostCommonLetterWordleSelector<IterType>::computeFrequencyMapInternal(unordered_map<char, size_t>& letterMap,
unordered_map<string, size_t>& wordScore) {
// Compute letter scores
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
for (auto& c : *wordIt) {
if (letterMap.find(c) == letterMap.end()) {
letterMap[c] = 0;
}
letterMap[c]++;
}
}
// Compute word scores
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
size_t score = 0;
set<char> wordLetters;
set<char> greenLetters;
for (size_t i = 0; i < this->m_knowns.size(); i++) {
if (this->m_knowns[i].result == WordleResult::GREEN) {
greenLetters.insert(this->m_knowns[i].letter);
}
}
size_t i = 0;
for (auto& c : *wordIt) {
// only give scores to each letter once
if (wordLetters.find(c) == wordLetters.end()) {
// only give scores to each letter that isn't already known
if (this->m_knowns[i].result != WordleResult::GREEN) {
if (greenLetters.find(c) == greenLetters.end()) {
score += letterMap[c];
}
}
}
wordLetters.insert(c);
i++;
}
wordScore.insert({*wordIt, score});
}
}
template <typename IterType>
void PositionalLetterWordleSelector<IterType>::computeFrequencyMapInternal(unordered_map<char, size_t>& unused_letterMap,
unordered_map<string, size_t>& wordScore) {
// Compute positional letter scores
for (size_t i = 0; i < LETTER_COUNT; i++) {
auto letterMap = unordered_map<char, size_t>();
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
char c = (*wordIt)[i];
if (letterMap.find(c) == letterMap.end()) {
letterMap[c] = 0;
}
letterMap[c]++;
}
m_positionLetterScores.push_back(letterMap);
}
// Compute word scores & write to file
#if CREATE_SCORES_FILE == true
ofstream file_stream;
if (m_initialGuess && g_num_runs == 0) {
file_stream.open(DICTIONARY_SCORES_FILENAME);
}
#endif
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
size_t score = 0;
set<char> wordLetters;
set<char> greenLetters;
for (size_t i = 0; i < this->m_knowns.size(); i++) {
if (this->m_knowns[i].result == WordleResult::GREEN) {
greenLetters.insert(this->m_knowns[i].letter);
}
}
size_t i = 0;
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << *wordIt << ":";
}
#endif
for (auto& c : *wordIt) {
// only give scores to each letter once // for the opening 2 rounds
if ((wordLetters.find(c) == wordLetters.end() && this->m_guessNum < 6) || this->m_guessNum >= 6) {
// only give scores to each letter that isn't already known
//if (this->m_knowns[i].result != WordleResult::GREEN) {
if (greenLetters.find(c) == greenLetters.end()) {
score += m_positionLetterScores[i][c];
}
//}
}
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << m_positionLetterScores[i][c] << ",";
}
#endif
wordLetters.insert(c);
i++;
}
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << " = " << score << endl;
}
#endif
wordScore.insert({*wordIt, score});
}
m_initialGuess = false;
}
template <typename IterType>
void PositionalLetterWordleSelector<IterType>::clearOldState() {
MostCommonLetterWordleSelector<IterType>::clearOldState();
m_positionLetterScores.clear();
}
template <typename IterType>
void FrequencyAndPositionalLetterWordleSelector<IterType>::computeFrequencyMapInternal(unordered_map<char, size_t>& unused_letterMap,
unordered_map<string, size_t>& wordScore) {
// Compute positional letter scores
for (size_t i = 0; i < LETTER_COUNT; i++) {
auto letterMap = unordered_map<char, size_t>();
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
char c = (*wordIt)[i];
if (letterMap.find(c) == letterMap.end()) {
letterMap[c] = 0;
}
letterMap[c]++;
}
m_positionLetterScores.push_back(letterMap);
}
// Compute letter scores
for (size_t i = 0; i < LETTER_COUNT; i++) {
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
char c = (*wordIt)[i];
if (m_fullLetterMap.find(c) == m_fullLetterMap.end()) {
m_fullLetterMap[c] = 0;
}
m_fullLetterMap[c]++;
}
}
// Compute word scores & write to file
#if CREATE_SCORES_FILE == true
ofstream file_stream;
if (m_initialGuess && g_num_runs == 0) {
file_stream.open(DICTIONARY_SCORES_FILENAME);
}
#endif
for (auto wordIt = this->m_iterBegin; wordIt != this->m_iterEnd; wordIt++) {
size_t score = 0;
set<char> wordLetters;
set<char> greenLetters;
for (size_t i = 0; i < this->m_knowns.size(); i++) {
if (this->m_knowns[i].result == WordleResult::GREEN) {
greenLetters.insert(this->m_knowns[i].letter);
}
}
size_t i = 0;
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << *wordIt << ":";
}
#endif
for (auto& c : *wordIt) {
// only give scores to each letter once
if ((wordLetters.find(c) == wordLetters.end() && this->m_guessNum < 6) || this->m_guessNum >= 6) {
if (greenLetters.find(c) == greenLetters.end()) {
score += (m_positionLetterScores[i][c] + 2*m_fullLetterMap[c]);
}
}
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << (m_positionLetterScores[i][c] + 2*m_fullLetterMap[c]) << ",";
}
#endif
wordLetters.insert(c);
i++;
}
#if CREATE_SCORES_FILE == true
if (m_initialGuess && g_num_runs == 0) {
file_stream << " = " << score << endl;
}
#endif
wordScore.insert({*wordIt, score});
}
m_initialGuess = false;
}