-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle_selector.h
170 lines (144 loc) · 6.1 KB
/
wordle_selector.h
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
#pragma once
#include "wordle_rules.h"
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <unordered_map>
using namespace std;
using ForwardIterator = vector<string>::iterator;
using SetIterator = set<string>::iterator;
template <typename IterType>
class WordleSelector {
public:
WordleSelector() { srand(time(nullptr)); }
virtual string select(IterType begin, IterType end, size_t rangeSize, const vector<WordleKnown>& knowns, size_t guessNum) = 0;
protected:
size_t m_guessNum{0};
};
template <typename IterType>
class RandomWordleSelector : public WordleSelector<IterType> {
public:
string select(IterType begin, IterType end, size_t rangeSize, const vector<WordleKnown>& knowns, size_t guessNum) override;
private:
size_t getRandom(IterType begin, IterType end, size_t rangeSize) const;
};
template <typename IterType>
class EnhancedRandomWordleSelector : public RandomWordleSelector<IterType> {
public:
string select(IterType begin, IterType end, size_t rangeSize, const vector<WordleKnown>& knowns, size_t guessNum) override;
private:
bool containsDoubleLetter(const string& word) const;
bool isVowel(char letter) const;
bool containsOneVowel(const string& word) const;
};
///////////////
struct WordScore {
WordScore() : word(), lowestLetterScore(0), score(0) {}
string word;
size_t lowestLetterScore;
size_t score;
};
struct WordScoreComp {
bool operator() (const WordScore& w1, const WordScore& w2) const {
return w1.score > w2.score;
}
};
/////////////////
template <typename IterType>
class MostCommonLetterWordleSelector : public WordleSelector<IterType> {
public:
string select(IterType begin, IterType end, size_t rangeSize, const vector<WordleKnown>& knowns, size_t guessNum) override;
protected:
bool containsAllHints(const string& word) const;
string getBestCandidate() const;
void clearOldState();
char getMostCommonLetter() const;
void computeFrequencyMap();
void sortWordsByFrequency();
void printCandidates() const;
virtual void computeFrequencyMapInternal(unordered_map<char, size_t>& letterMap,
unordered_map<string, size_t>& wordScore) = 0;
IterType m_iterBegin;
IterType m_iterEnd;
unordered_map<char, size_t> m_frequencyMapLetter;
unordered_map<string, size_t> m_wordScore;
vector<WordleKnown> m_knowns;
unordered_map<char, size_t> m_alphabetFrequencyMapLetter;
unordered_map<string, size_t> m_alphabetWordScore;
set<WordScore, WordScoreComp> m_sortedWords{};
};
template <typename IterType>
class NaiveMostCommonLetterWordleSelector : public MostCommonLetterWordleSelector<IterType> {
protected:
void computeFrequencyMapInternal(unordered_map<char, size_t>& letterMap,
unordered_map<string, size_t>& wordScore) override;
};
template <typename IterType>
class ImprovedMostCommonLetterWordleSelector : public MostCommonLetterWordleSelector<IterType> {
protected:
void computeFrequencyMapInternal(unordered_map<char, size_t>& letterMap,
unordered_map<string, size_t>& wordScore) override;
};
template <typename IterType>
class PositionalLetterWordleSelector : public MostCommonLetterWordleSelector<IterType> {
protected:
void computeFrequencyMapInternal(unordered_map<char, size_t>& unused_letterMap,
unordered_map<string, size_t>& wordScore) override;
void clearOldState();
bool m_initialGuess{true};
vector<unordered_map<char, size_t>> m_positionLetterScores;
};
template <typename IterType>
class FrequencyAndPositionalLetterWordleSelector : public PositionalLetterWordleSelector<IterType> {
protected:
void computeFrequencyMapInternal(unordered_map<char, size_t>& unused_letterMap,
unordered_map<string, size_t>& wordScore) override;
bool m_initialGuess{true};
vector<unordered_map<char, size_t>> m_positionLetterScores;
unordered_map<char, size_t> m_fullLetterMap;
};
//////////
enum class WordleSelectorType {
Random,
EnhancedRandom,
NaiveMostCommonLetter,
ImprovedMostCommonLetter,
PositionalLetter,
FrequencyAndPositionalLetter,
};
template <typename IterType>
struct WordleSelectorFactory {
static WordleSelector<IterType>* makeWordleSelector(const WordleSelectorType& selectorType) {
switch (selectorType) {
case WordleSelectorType::Random:
return new RandomWordleSelector<IterType>();
case WordleSelectorType::EnhancedRandom:
return new EnhancedRandomWordleSelector<IterType>();
case WordleSelectorType::NaiveMostCommonLetter:
return new NaiveMostCommonLetterWordleSelector<IterType>();
case WordleSelectorType::ImprovedMostCommonLetter:
return new ImprovedMostCommonLetterWordleSelector<IterType>();
case WordleSelectorType::PositionalLetter:
return new PositionalLetterWordleSelector<IterType>();
case WordleSelectorType::FrequencyAndPositionalLetter:
return new FrequencyAndPositionalLetterWordleSelector<IterType>();
}
if (DEBUG) cerr << "Error: [selector] invalid selectorType" << endl;
throw;
}
};
// Explicity Instantiation to keep linker happy, TODO: why?
template class RandomWordleSelector<SetIterator>;
template class EnhancedRandomWordleSelector<SetIterator>;
template class NaiveMostCommonLetterWordleSelector<SetIterator>;
template class ImprovedMostCommonLetterWordleSelector<SetIterator>;
template class PositionalLetterWordleSelector<SetIterator>;
template class FrequencyAndPositionalLetterWordleSelector<SetIterator>;
template class RandomWordleSelector<ForwardIterator>;
template class EnhancedRandomWordleSelector<ForwardIterator>;
template class MostCommonLetterWordleSelector<ForwardIterator>;
template class NaiveMostCommonLetterWordleSelector<ForwardIterator>;
template class ImprovedMostCommonLetterWordleSelector<ForwardIterator>;
template class PositionalLetterWordleSelector<ForwardIterator>;
template class FrequencyAndPositionalLetterWordleSelector<ForwardIterator>;