cxx17/cc195.cc

35 lines
757 B
C++

// Return the score for a mastermind guess.
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <string>
#include <range/v3/all.hpp>
#include <unordered_set>
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<char> 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}));
}