-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwordle.cpp
165 lines (158 loc) · 5.38 KB
/
wordle.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
//
// A very simple, no frills Wordle solver - for those times when you think the answer is just
// not in your vocabulary.
//
// Thanks to Josh at powerlanguage for Wordle!
//
// Bill Forster [email protected] 2021.01.03
//
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
// Check the word list, one word at a time
bool check_word( const std::string &word, const std::string &green_pattern,
const std::vector<std::string> &yellow_patterns, const std::string &yellow_letters,
const std::set<char> &black_letters )
{
int idx=0;
std::set<char> build; // build a set of letters from the word
for( char c: word )
{
char p = green_pattern[idx];
if( p == '?' )
{
auto it = black_letters.find(c);
bool is_banned = (it != black_letters.end());
if( is_banned )
return false; // wildcard letter is banned
}
else if( c != p )
{
return false; // word doesn't match acceptable pattern
}
idx++;
build.insert(c); // dups are automatically ignored
}
for( char c: yellow_letters )
{
if( c != '-' )
{
auto it = build.find(c);
bool is_present = (it != build.end());
if( !is_present )
return false; // yellow_letters letter not present
}
}
for( std::string yellow_pattern: yellow_patterns )
{
idx = 0;
for( char c: word )
{
char p = yellow_pattern[idx];
if( c == p )
return false; // word matches unacceptable pattern
idx++;
}
}
return true;
}
// Entry point
int main( int argc, char *argv[] )
{
const char *usage =
"Usage;\n"
" wordle green_pattern black_letters yellow_patterns...\n"
" green_pattern = 5 letter known pattern, lower case letters and ?s\n"
" black_letters = letters known to be excluded, '-' to omit\n"
" yellow_patterns = 5 letter patterns, lower case letters and ?s\n"
"For example;\n"
" You know o is the middle letter, you know e,l,i,a,n and r aren't\n"
" included. You know s and t are included, but t can't be the second\n"
" letter and s can't be the first, second or last letter. Run this\n"
" command;\n"
" wordle ??o?? elianr st??s ?s???\n"
"Prints 'boost', 'coost' and 'ghost'. The word list now matches the\n"
"actual Wordle word list (thanks Github contributor SeanJA). It doesn't\n"
"have quite as many strange words as the previous huge list, but it\n"
"clearly still has some!\n"
"\n"
"Thanks to Josh at powerlanguage.co.uk for creating Wordle.\n"
"\n"
"Bill Forster, github.com/billforsternz/wordle, [email protected]\n";
// Check a few preconditions etc.
if( argc < 3 )
{
printf( "Simple command line Wordle solver. Use only when terminally frustrated!\n\n" );
printf( "%s", usage );
return -1;
}
std::string green_pattern(argv[1]);
std::string black_letters(argv[2]);
bool err = false;
size_t offset;
if( green_pattern.length() != 5 )
{
printf( "Error: Green pattern string '%s' should be 5 characters long\n", green_pattern.c_str() );
err = true;
}
if( (offset=green_pattern.find_first_not_of("?abcdefghijklmnopqrstuvwxyz")) != std::string::npos )
{
printf( "Error: Illegal character '%c' in green pattern string '%s'\n", green_pattern[offset], green_pattern.c_str() );
err = true;
}
if( (offset=black_letters.find_first_not_of("-abcdefghijklmnopqrstuvwxyz")) != std::string::npos )
{
printf( "Error: Illegal character '%c' in black letters string '%s'\n", black_letters[offset], black_letters.c_str() );
err = true;
}
// Create yellow_patterns and yellow_letters
std::vector<std::string> yellow_patterns;
std::string yellow_letters;
for( int i=3; !err && i<argc; i++ )
{
std::string s(argv[i]);
if( (offset=s.find_first_not_of("?abcdefghijklmnopqrstuvwxyz")) != std::string::npos )
{
printf( "Error: Illegal character '%c' in yellow pattern string '%s'\n", s[offset], s.c_str() );
err = true;
}
if( s.length() != 5 )
{
printf( "Error: Yellow pattern string '%s' should be 5 characters long\n", s.c_str() );
err = true;
}
yellow_patterns.push_back(s);
for( char c: s )
{
if( c != '?' && yellow_letters.find(c)==std::string::npos )
yellow_letters.push_back(c);
}
}
if( err )
{
printf( "%s", usage );
return -1;
}
// Parameter black_letters is a std::set of banned letters
std::set<char> banned;
for( char c: black_letters )
{
if( c != '-' )
banned.insert(c);
}
// Loop through the word list, easy peasy
extern const char *wordlist[];
extern unsigned int wordlist_length;
printf( "Checking %u known five letter words\n", wordlist_length );
for( unsigned int i=0; i<wordlist_length; i++ )
{
std::string word(wordlist[i]);
if( check_word( word, green_pattern, yellow_patterns, yellow_letters, banned ) )
printf( "%s\n", word.c_str() );
}
return 0;
}