-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
335 lines (298 loc) · 6.63 KB
/
main.cpp
File metadata and controls
335 lines (298 loc) · 6.63 KB
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
/*
CREDIT FOR DATA:
File for all english words: https://github.com/dwyl/english-words (words.txt)
File for all english words and there frequency: https://github.com/hermitdave/FrequencyWords (en_full.txt)
*/
/*
Program: Primitive Wordle solver.
Instructions:
1) Access a wordle game online (read instructions online if unsure how to play).
2) On BASH terminal type command (./comp.sh)
3) Enter your guess on wordle, look at the color sequence you get
4) Enter your guess in the terminal (all caps) followed by a sequence of B/O/G where
B = black, O = Orange / Yellow, and G = Green. You should see word recommendations given
Repeat 3 & 4.
5) Enjoy the game :) {Should be self explanatory}
Logic: Basic idea of combining letter frequency of remaining valid words and commonly used in
english language
Just a fun idea I had :)
*/
#include <iostream>
#include <vector>
#include <utility>
#include <ctype.h>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <map>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
vector<string>entities;
map<string, int>helper;
map<string, double>prob;
int attempts;
bool answerFound = false;
pair<string, long long> seperator(string s){
string first, second;
long long i = 0;
while(s[i] != 32){
first += s[i];
i++;
}
i++;
while(i != s.size()){
second += s[i];
i++;
}
stringstream g(second);
long long ans = 0;
g >> ans;
return {first, ans};
}
void calculateProbability(){
int finals = 0;
ifstream in("freq.txt");
string d;
vector<string>token;
while(getline(in, d, '\n')){
token.push_back(d);
}
unsigned long long total = 0;
for(string a : token){
bool alpha = false;
bool length = false;
auto ir = seperator(a);
if(ir.first.size() != 5){
}
else{
length = true;
for(int i = 0; i < ir.first.size(); i++){
if(!isalpha(ir.first[i])){
alpha = false;
break;
}
if(i == ir.first.size() - 1){
alpha = true;
}
}
}
if(length && alpha){
for(int i = 0; i < ir.first.size(); i++){
if(!isupper(ir.first[i])){
ir.first[i] = toupper(ir.first[i]);
}
}
if(helper.find(ir.first) != helper.end()){
++finals;
prob[ir.first] = ir.second;
total += prob[ir.first];
}
}
}
auto it = prob.begin();
while(it != prob.end()){
it->second = (it->second * 1.0) / total;
++it;
}
}
bool isUpper(string s){
for(char d : s){
if(isupper(d)){
}
else{
return false;
}
}
return true;
}
void next(){
map<char, double>freq;
for(int i = 0; i < entities.size(); i++){
string s = entities[i];
if(s[s.size() - 1] != '!'){
for(char d : s){
freq[d]++;
}
}
}
vector<pair<double, string>>remaining;
for(int i = 0; i < entities.size(); i++){
string s = entities[i];
int amount = 0;
if(s[s.size() - 1] != '!' && isUpper(s)){
for(char sd : s){
amount += freq[sd];
}
if(prob.find(s) != prob.end()){
pair<double, string> h = {amount*prob[s], s};
remaining.push_back(h);
}
}
}
sort(remaining.begin(), remaining.end());
if(remaining.size() >= 10){
for(int i = remaining.size() - 1; i >= remaining.size() - 10; i--){
cout << remaining[i].second << " {" << remaining[i].first << "}" << endl;
}
}
else{
for(int i = remaining.size() - 1; i >= 0; i--){
cout << remaining[i].second << " {" << remaining[i].first << "}" << endl;
}
}
cout << endl << endl;
}
void readInput(){
ifstream in("English.txt");
string s;
while(in >> s){
entities.push_back(s);
helper[s] = 1;
}
}
void getUserInput(int attempt){
ofstream out("Help.txt");
bool errorcheck = true;
string guess; string bog;
while(errorcheck == true){
cout << "Enter guess #" << 7-attempt << ": " << endl;
cin >> guess;
if(guess.size() != 5){
cout << "Guess needs to be 5 in length" << endl;
}
else{
bool passUpper = true;
for(char d : guess){
if(!isupper(d)){
cout << endl << "All uppercase letters please" << endl << endl;;
passUpper = false;
break;
}
}
if(passUpper == true){
errorcheck = false;
}
}
}
errorcheck = true;
while(errorcheck){
cout << "Enter a string consisting of B/O/G" << endl;
cin >> bog;
if(bog.size() != 5){
cout << "BOG string needs to be 5 in length" << endl;
}
else{
bool passChar = true;
for(char d : bog){
if(d != 'B' && d != 'O' && d != 'G'){
cout << "Only enter capital B/O/G please" << endl;
passChar = false;
break;
}
}
if(passChar == true){
errorcheck = false;
}
}
}
cout << "___________________________________________" << endl;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(guess[i] == guess[j]){
if(bog[i] != bog[j]){
if(bog[i] == 'B' && bog[j] == 'G'){
guess[i] = '%';
}
else if(bog[i] == 'G' && bog[j] == 'B'){
guess[j] = '%';
}
else if(bog[i] == 'B' && bog[j] == 'O'){
guess[i] = '%';
}
else if(bog[i] == 'O' && bog[j] == 'B'){
guess[j] = '%';
}
}
}
}
}
for(int i = 0; i < 5; i++){
char placement = bog[i];
char character = guess[i];
if(placement == 'B'){
// Remove all strings that contain this character
for(int j = 0; j < entities.size(); j++){
string consider = entities[j];
if(consider[consider.size() - 1] != '!' && isalpha(character)){
for(char d : consider){
if(d == character){
entities[j] += "!";
break;
}
}
}
}
}
else if(placement == 'O'){
for(int j = 0; j < entities.size(); j++){
string consider = entities[j];
if(consider[consider.size() - 1] != '!' && isalpha(character)){
bool contain = false;
for(int jjj = 0; jjj < 5; jjj++){
char d = consider[jjj];
if(d == character && jjj != i){
contain = true;
}
}
if(!contain){
entities[j] += "!";
}
}
}
}
else{
for(int j = 0; j < entities.size(); j++){
string consider = entities[j];
if(consider[consider.size() - 1] != '!'){
if(consider[i] != character && isalpha(character)){
entities[j] += "!";
}
}
}
}
}
int finals = 0;
for(string s : entities){
if(s[s.size() - 1] != '!'){
++finals;
}
}
if(finals == 1){
string ans;
for(string s : entities){
if(s[s.size() - 1] != '!'){
ans = s;
break;
}
}
cout << ans << " is the final answer" << endl;
answerFound = true;
return;
}
else{
cout << finals << " valid words left" << endl;
}
next();
}
int main(){
cout << fixed << setprecision(16);
readInput();
calculateProbability();
attempts = 6;
while(attempts != 0 && answerFound == false){
getUserInput(attempts);
attempts--;
}
}