#include "ordle.h" #include #include #include #include #include #include std::vector read_candidates(std::istream &input) { std::vector 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 { std::string lowercase_word = word; std::transform(lowercase_word.begin(), lowercase_word.end(), lowercase_word.begin(), ::tolower); std::string lowercase_set = letter_set; std::transform(lowercase_set.begin(), lowercase_set.end(), lowercase_set.begin(), ::tolower); return contains_any_of(lowercase_word, lowercase_set); } bool correct_fn::operator()(const std::string &word) const { std::string lowercase_word = word; std::transform(lowercase_word.begin(), lowercase_word.end(), lowercase_word.begin(), ::tolower); return std::all_of(map.begin(), map.end(), [&lowercase_word](const auto &pair) { return contains_at(lowercase_word, std::tolower(pair.second), pair.first); }); } bool misplaced_fn::operator()(const std::string &word) const { std::string lowercase_word = word; std::transform(lowercase_word.begin(), lowercase_word.end(), lowercase_word.begin(), ::tolower); if (map.empty()) { return true; } if (std::all_of(map.begin(), map.end(), [&lowercase_word](const auto &pair) { return pair.first < lowercase_word.size() && lowercase_word[pair.first] == std::tolower(pair.second); })) { return false; } if (std::any_of(map.begin(), map.end(), [&lowercase_word](const auto &pair) { return std::find(lowercase_word.begin(), lowercase_word.end(), std::tolower(pair.second)) != lowercase_word.end(); })) { return true; } return false; } void do_filter(std::vector &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 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}; }