-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
154 lines (132 loc) · 3.49 KB
/
Copy pathsolver.cpp
File metadata and controls
154 lines (132 loc) · 3.49 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
#ifndef SOLVER
#define SOLVER
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <fstream>
struct weightedWord
{
std::string word;
int weight;
bool operator<(const weightedWord& a) const
{
return weight > a.weight;
}
};
//Function Declarations:
std::string getMode();
std::string getAnswersString(std::string&, std::ifstream&);
std::vector<int> weighLetters(std::string&);
std::vector<weightedWord> getAnswersVector(std::string&, std::ifstream&, std::vector<int>&);
int weighWords(std::string&, std::vector<int>& weightedLetters);
void displayModes();
void displayModeSelected(std::string&);
int main()
{
//Get Mode for answers.txt
std::ifstream inputFile;
std::string fileName = getMode();
displayModeSelected(fileName);
//Weigh answer letters for all words
std::string answerString = getAnswersString(fileName, inputFile);
std::vector<int> weightedLetters = weighLetters(answerString);
//Weigh answer words based on letter weight
std::vector<weightedWord> potentialAnswers = getAnswersVector(fileName, inputFile, weightedLetters);
//Sort potiential Answers
std::sort(potentialAnswers.begin(), potentialAnswers.end());
int count = 0;
for(auto i : potentialAnswers)
{
std::cout << i.word << " " << i.weight << '\n';
if(count == 4)
break;
count++;
}
}
std::string getMode()
{
std::string fileName;
enum wordleModes {test, nyt, original};
displayModes();
int mode;
std::cin >> mode;
switch (mode)
{
case test:
fileName = "test-answers.txt";
break;
case nyt:
fileName = "wordle-nyt-answers-alphabetical.txt";
break;
case original:
fileName = "wordle-answers-alphabetical.txt";
break;
default:
break;
}
return fileName;
}
std::string getAnswersString(std::string& fileName, std::ifstream& inputFile)
{
std::string answersString, temp;
inputFile.open(fileName, std::ios::in);
while(getline(inputFile, temp))
{
answersString += temp;
}
inputFile.close();
return answersString;
}
std::vector<int> weighLetters(std::string& answersString)
{
std::vector<int> alphabet(26,0);
int i = 0;
for(char ch = 'a'; ch <= 'z'; ch++)
{
alphabet[i] = std::count(answersString.begin(), answersString.end(), ch);
i++;
}
return alphabet;
}
std::vector<weightedWord> getAnswersVector(std::string& fileName, std::ifstream& inputFile, std::vector<int>& weightedLetters)
{
std::vector<weightedWord> vect;
std::string tempString;
weightedWord tempWord;
inputFile.open(fileName, std::ios::in);
while(getline(inputFile, tempString))
{
tempWord.word = tempString;
tempWord.weight = weighWords(tempString, weightedLetters);
vect.push_back(tempWord);
}
inputFile.close();
return vect;
}
int weighWords(std::string& word, std::vector<int>& weightedLetters)
{
int tempWeight = 0;
for(int i = 0; i < 5; i++)
{
for(char ch = 'a'; ch <= 'z'; ch++)
{
if(word[i] == ch)
{
tempWeight += weightedLetters[ch - 97];
break;
}
}
}
return tempWeight;
}
void displayModes()
{
std::cout << "Select Mode: [0]test, [1]nyt, or [2]original ";
}
void displayModeSelected(std::string& fileName)
{
std::cout << "Opening " << fileName << "...\n";
}
#endif