-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_trie.h
70 lines (62 loc) · 2.74 KB
/
wordle_trie.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
#pragma once
#include "wordle_selector.h"
#include "wordle_solver.h"
#include <string>
#include <set>
#include <vector>
using namespace std;
class WordleTrie;
class WordleTrieNode {
public:
WordleTrieNode() = delete;
WordleTrieNode(char v, WordleTrie* wt, WordleTrieNode* parent, string prefix="", bool isLeaf=false)
: val(v), children(), m_wordleTrie(wt), m_parent(parent), m_prefix(prefix), m_isLeaf(isLeaf) {}
char val;
vector<WordleTrieNode> children;
bool operator==(const WordleTrieNode& node) const { return node.val == val; }
bool operator!=(const WordleTrieNode& node) const { return node.val != val; }
private:
friend WordleTrie;
WordleTrie* m_wordleTrie;
WordleTrieNode* m_parent;
string m_prefix;
bool m_isLeaf;
};
class WordleTrie {
public:
WordleTrie() { m_root = new WordleTrieNode('_', this, nullptr); }
bool insert(string word);
void fixupGreen(size_t letterPosition, char letter) {
removeExceptLetterAtLevel(0, letterPosition, letter, *m_root);
}
void fixupYellow(size_t letterPosition, char letter) {
removeSingleLetter(letterPosition, letter);
removeWordsWithoutLetter(letter);
}
void fixupBlack(size_t letterPosition, char letter) {
removeAllOfLetter(letter, *m_root);
}
string getCandidate(WordleSelector<SetIterator>* selector, const vector<WordleKnown>& knowns, size_t guessNum);
size_t getNumCandidates() const { return m_candidates.size(); }
void printCandidates() const;
private:
void insertAtNode(string prefix, string remainingWord, WordleTrieNode* node);
bool letterExists(char letter, WordleTrieNode* node, WordleTrieNode** match);
void addAllOfLetterToSolution(char letter);
void addAllOfLetterToSolution(char letter, WordleTrieNode& node, vector<string>& letterCandidates);
void removeFromCandidates(WordleTrieNode* node);
void removeExceptLetterAtLevel(size_t curDepth, size_t letterPosition, char letter, WordleTrieNode& node);
void removeLetterAtLevel(char letter, WordleTrieNode& node);
void removeAllOfLetter(char letter, WordleTrieNode& node);
void removeSingleLetter(size_t letterPosition, char letter);
void removeSingleLetter(size_t curDepth, size_t letterPosition, char letter, WordleTrieNode& node);
void removeAllChildren(WordleTrieNode& node);
void addAllChildren(WordleTrieNode& node, vector<string>& letterCandidates);
void promoteLetter();
void removeWordsWithoutLetter(char letter);
void removeWordsWithoutLetterAtLevel(size_t curDepth, char letter, WordleTrieNode& node);
void removeWord(WordleTrieNode& node);
WordleTrieNode* getChild(size_t letterPosition, char letter); // TODO: unused
WordleTrieNode* m_root;
set<string> m_candidates;
};