// Return the score for a mastermind guess. #define CATCH_CONFIG_MAIN #include #include #include #include struct Score { int hits; int pseudo; bool operator==(const Score& rhs) const { return hits == rhs.hits && pseudo == rhs.pseudo; } }; Score score(const std::string& board, const std::string& guess) { std::unordered_set balls{board.cbegin(), board.cend()}; Score ret{}; for (const auto& pair: ranges::view::zip(board, guess)) { if (pair.first == pair.second) { ret.hits++; } else if (balls.count(pair.second) != 0) { ret.pseudo++; } } return ret; } TEST_CASE("cc19.5", "mastermind") { CHECK((score("rggb", "yrgb") == Score{2, 1})); }