-
Notifications
You must be signed in to change notification settings - Fork 13
/
wordle-colorizer.cpp
49 lines (34 loc) · 1.22 KB
/
wordle-colorizer.cpp
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
49
#include <bits/stdc++.h>
std::string solve(std::string_view s, std::string_view answer) noexcept {
enum class EValState : char {
ABSENT = 'X', MISPLACED = 'O', CORRECT = '#'
};
std::string ret(std::size(answer), static_cast<char>(EValState::ABSENT));
if ( std::size(s) != std::size(answer) )
return ret;
std::map<char, int> occCount, occInAnswer;
for ( const auto& c : answer ) ++occInAnswer[c];
for ( auto i{ 0 }; i < std::size(answer); ++i ) {
const auto& curChar{ s.at(i) };
if ( curChar != answer.at(i) )
continue;
ret[i] = static_cast<char>(EValState::CORRECT);
++occCount[curChar];
}
for ( auto i{ 0 }; i < std::size(answer); ++i ) {
const auto& curChar{ s.at(i) };
if ( curChar == answer.at(i) || occCount[curChar] >= occInAnswer[curChar])
continue;
ret[i] = static_cast<char>(EValState::MISPLACED);
++occCount[curChar];
}
return ret;
}
int main()
{
std::string answer, attempt;
getline(std::cin, answer);
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
while ( getline(std::cin, attempt) )
std::cout << solve(attempt, answer) << '\n';
}