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
|
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
auto calc_result(vector<T> code, vector<T> guess) {
const T CrossedOut {};
int exact = 0, near = 0;
// count exact matches
for (size_t i = 0; i < code.size(); ++i)
if (code[i] == guess[i]) {
++exact;
code[i] = guess[i] = CrossedOut;
}
// count near matches
for (auto& gx: guess) {
if (gx == CrossedOut) continue;
for (auto& cx: code)
if (cx == gx) {
++near;
cx = gx = CrossedOut;
break;
}
}
return make_pair(exact, near);
}
int main() {
vector<vector<vector<int>>> v { // positive values for colors
// code guess result
{ { 1, 2, 2, 2 }, { 3, 1, 2, 1 }, { 1, 1 } },
{ { 5, 3, 4, 3 }, { 3, 5, 4, 2 }, { 1, 2 } },
{ { 2, 3, 4, 5 }, { 2, 5, 4, 5 }, { 3, 0 } },
{ { 2, 3, 4, 5 }, { 5, 4, 3, 2 }, { 0, 4 } },
{ { 2, 2, 3, 3 }, { 3, 3, 2, 2 }, { 0, 4 } },
{ { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 0, 0 } },
{ { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 4, 0 } },
{ { 2, 3, 4, 5 }, { 2, 3, 4, 5 }, { 4, 0 } },
{ { 2, 3, 4, 5 }, { 2, 5, 4, 3 }, { 2, 2 } },
};
for (const auto& x: v) {
auto [exact, near] = calc_result(x[0], x[1]);
cout << exact << ' ' << near;
if (exact != x[2][0] || near != x[2][1])
cout << " wrong!";
cout << '\n';
}
}
|