-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.cpp
92 lines (75 loc) · 2.9 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
// For debugging
#include <iostream>
// For std::remove
#include <algorithm>
#include <map>
#include <set>
#include "wordle.h"
#include "dict-eng.h"
using namespace std;
// Add prototypes of helper functions here
void wordle_helper(
const std::string& in,
const std::set<char> floating,
const std::set<std::string>& dict,
unsigned int index, std::set<std::string> possible);
/*
Approach:
find all words with given string in and floating chars
fill in rest of letters with all letters and check if english language word
*/
// Definition of primary wordle function
std::set<std::string> wordle(
const std::string& in,
const std::string& floating,
const std::set<std::string>& dict)
{
std::set<char> floating_set(floating.begin(), floating.end()); //floating set copy
std::set<std::string> possible;
wordle_helper(in,floating_set, dict, 0, possible);
return possible; //answer set
}
void wordle_helper(
const std::string& in,
const std::set<char> floating_set,
const std::set<std::string>& dict,
unsigned int index, std::set<std::string> possible){
std::string current = in; //copy of string
//alphabet set
std::string alpha = "abcdefghijklmnopqrstuvwxyz";
const std::set<char> alphabet(alpha.begin(),alpha.end());
//iterator
std::set<char>::iterator alpha_iterate;
std::set<char>::iterator curr_floating_iterate;
std::set<char> curr_floating(floating_set.begin(), floating_set.end()); //copy of floating set
//base case, if reach end of index
if(index == in.length()){
//get to end of index and checkk, make sure floating is empty
if(dict.find(current) != dict.end() ){ //check if English word
possible.insert(current);
}
return;
}
if(current[index] == '-'){ //empty location
//iterate floating letters
for(curr_floating_iterate = floating_set.begin();
curr_floating_iterate != floating_set.end(); ++curr_floating_iterate){
current[index] = *curr_floating_iterate; //add letter
//remove floating
std::set<char>::iterator iterate;
iterate = curr_floating.find(*curr_floating_iterate);
if(iterate != curr_floating.end()){
curr_floating.erase(iterate);
}
wordle_helper(current, curr_floating, dict, index+1, possible); //recursive call, move one space
std::set<char> curr_floating(floating_set.begin(), floating_set.end()); //make original set again
}
//check alphabet
for(alpha_iterate = alphabet.begin();
alpha_iterate != alphabet.end(); ++alpha_iterate){ //iterate thru alphabet set
current[index] = *alpha_iterate; //try alphabet letter
wordle_helper(current, floating_set, dict, index+1, possible); //recurse
}
}
return;
}