-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.hpp
170 lines (158 loc) · 4.77 KB
/
Evaluator.hpp
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
#include "Util.hpp"
#include "WordleEngine.hpp"
#include <cmath>
#include <map>
#include <limits>
#include <chrono>
#include <iomanip>
typedef pair<char, int> mapkey_t;
typedef multimap<mapkey_t, sid_t> map_t;
typedef pair <int, string> score_t;
typedef multimap<int, string,
greater<int> > score_table_t;
class Evaluator : public WordleEngine
{
protected:
map_t Map;
string Alphabet;
size_t Longest;
int populate_multimap()
{
Map.clear();
Alphabet.clear();
Longest = 0;
set<char> ab, cs;
for ( sid_t i = 0 ; i < WinnablesEnd() ; ++i )
{
string const& word = IdToWordMap[i];
size_t l = word.size();
Longest = max(Longest, l);
cs.clear();
while (l>0)
{
char const c = word[--l];
ab.insert ( c );
cs.insert ( c );
Map.insert ( make_pair ( make_pair(c, l ) , i ) );
}
for ( char c : cs )
Map.insert ( make_pair ( make_pair(c, ANYWHERE ) , i ) );
}
copy ( ab.begin(), ab.end(), back_inserter(Alphabet) );
// all okay
return 0;
}
public:
Evaluator ( list_t const& _w, list_t const& _g )
: WordleEngine::WordleEngine(_w, _g)
{
populate_multimap();
}
Evaluator ( string const& wfile, string const& gfile )
: WordleEngine::WordleEngine(wfile, gfile)
{
populate_multimap();
}
size_t count_greens(string const& word) const
{
size_t matches = 0;
for ( size_t i = 0 ; i < word.size() ; ++i )
matches += Map.count(make_pair(word[i], (int)i));
return matches;
}
size_t count_yellows(string const& word) const
{
size_t matches = 0;
for ( size_t i = 0 ; i < word.size() ; ++i )
matches += Map.count(make_pair(word[i], ANYWHERE));
return matches;
}
int score_candidates(score_table_t& scores) const
{
for ( size_t i = 0 ; i < End() ; ++i )
{
// drop redundancies
if ( !UniqueChars(i) )
continue;
string const& key = IdToWordMap[i];
scores.insert ( make_pair ( entropy(i), key) );
}
return (int)scores.size();
}
int score_candidates2(score_table_t& scores) const
{
constexpr char const delim = '-';
string key;
size_t const max_status = binomial_coefficient(End(), 2);
size_t const status_delta = max_status / 100;
size_t next_status = 100;
size_t counter = 0;
for ( size_t i = 0 ; i < End() ; ++i )
{
// drop redundancies
if ( !UniqueChars(i) )
continue;
for ( size_t j = 0 ; j < i ; ++j, ++counter )
{
if ( counter >= next_status )
{
time_t t = time(nullptr);
tm tm = *localtime(&t);
cerr << setw(10) << counter << " of " << setw(10) << max_status
<< " : " << put_time(&tm, "%T") << endl;
next_status += status_delta;
}
// drop redundancies
if ( !UniqueChars(j) )
continue;
if ( OverlappingChars(i,j) )
continue;
key = IdToWordMap[i] + delim + IdToWordMap[j];
scores.insert ( make_pair ( coverage(i, j), key ) );
}
}
return (int)scores.size();
}
int entropy(sid_t id) const
{
if ( id >= End() )
return numeric_limits<int>::min();
string const& word = IdToWordMap[id];
set_t s;
for ( size_t i = 0 ; i < word.size() ; ++i )
{
auto p = Map.equal_range ( make_pair(word[i],i) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
p = Map.equal_range ( make_pair(word[i], ANYWHERE) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
}
double const ratio = (double)s.size() / (double)Winnables.size();
// return int ( 1000. * log2 (ratio) );
// return int ( 1000. * (1 - fabs(0.5-ratio)) );
return int ( 1000. * ratio );
}
int coverage(sid_t ai, sid_t bi) const
{
string const& a = IdToWordMap[ai];
string const& b = IdToWordMap[bi];
set_t s;
for ( size_t i = 0 ; i < a.size() ; ++i )
{
auto p = Map.equal_range ( make_pair(a[i],i) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
p = Map.equal_range ( make_pair(a[i], ANYWHERE) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
p = Map.equal_range ( make_pair(b[i],i) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
p = Map.equal_range ( make_pair(b[i], ANYWHERE) );
transform ( p.first, p.second, inserter(s, s.end()),
[](auto i){ return i.second; } );
}
return (int)s.size();
}
};