102 lines
2.8 KiB
C++
102 lines
2.8 KiB
C++
#include "ordle.h"
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
std::vector<std::string> read_candidates(std::istream &input) {
|
|
std::vector<std::string> candidates;
|
|
std::string line;
|
|
|
|
while (std::getline(input, line)) {
|
|
if (!line.empty() && line.back() == '\r') {
|
|
line.pop_back();
|
|
}
|
|
|
|
if (line.size() == 5) {
|
|
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
|
|
candidates.push_back(line);
|
|
}
|
|
}
|
|
|
|
std::sort(candidates.begin(), candidates.end());
|
|
|
|
candidates.erase(std::unique(candidates.begin(), candidates.end()),
|
|
candidates.end());
|
|
|
|
return candidates;
|
|
}
|
|
|
|
bool wrong_fn::operator()(const std::string &word) const {
|
|
return contains_any_of(word, letter_set);
|
|
}
|
|
|
|
bool correct_fn::operator()(const std::string &word) const {
|
|
return std::all_of(map.begin(), map.end(), [&word](const auto &pair) {
|
|
return contains_at(word, pair.second, pair.first);
|
|
});
|
|
}
|
|
|
|
// TODO:Correct but ugly
|
|
bool misplaced_fn::operator()(const std::string &word) const {
|
|
if (std::all_of(map.begin(), map.end(), [&word](const auto &pair) {
|
|
return pair.first < word.size() && word[pair.first] == pair.second;
|
|
})) {
|
|
return false;
|
|
}
|
|
|
|
if (std::any_of(map.begin(), map.end(), [&word](const auto &pair) {
|
|
return std::find(word.begin(), word.end(), pair.second) !=
|
|
word.end();
|
|
})) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void do_filter(std::vector<std::string> &candidates, const std::string &wrong,
|
|
const IndexMap &green, const IndexMap &yellow) {
|
|
auto predicate = [&wrong, &green, &yellow](const std::string &word) {
|
|
return wrong_fn(wrong)(word) || !correct_fn(green)(word) ||
|
|
!misplaced_fn(yellow)(word);
|
|
};
|
|
|
|
// Remove all words that do not satisfy the conditions
|
|
candidates.erase(
|
|
std::remove_if(candidates.begin(), candidates.end(), predicate),
|
|
candidates.end());
|
|
}
|
|
|
|
IndexMap build_list(const std::string &line) {
|
|
std::istringstream iss(line);
|
|
IndexMap result;
|
|
char letter;
|
|
size_type index;
|
|
|
|
while (iss >> letter >> index) {
|
|
result[index] = letter;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
std::tuple<std::string, IndexMap, IndexMap> prompt() {
|
|
std::string wrong;
|
|
std::cout << "Enter wrong letters:\n";
|
|
std::getline(std::cin, wrong);
|
|
|
|
std::string correct;
|
|
std::cout << "Enter correct letters (letter index)*:\n";
|
|
std::getline(std::cin, correct);
|
|
auto green = build_list(correct);
|
|
|
|
std::string misplaced;
|
|
std::cout << "Enter misplaced letters (letter index)*:\n";
|
|
std::getline(std::cin, misplaced);
|
|
auto yellow = build_list(misplaced);
|
|
|
|
return {wrong, green, yellow};
|
|
}
|