-
Notifications
You must be signed in to change notification settings - Fork 13
/
card-counting-when-easily-distracted.cpp
43 lines (32 loc) · 1.16 KB
/
card-counting-when-easily-distracted.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
#include <bits/stdc++.h>
int doTheJob(const std::string& s, int threshold) noexcept {
static const std::map<char, int> values{
{ 'K', 10 }, { 'Q', 10 }, { 'J', 10 }, { 'T', 10 },
{ '9', 9 }, { '8', 8 }, { '7', 7 }, { '6', 6 },
{ '5', 5 }, { '4', 4 }, { '3', 3 }, { '2', 2 }, { 'A', 1 }
};
static const std::regex rgx("^([2-9]|K|Q|J|T|A)+$",
std::regex_constants::ECMAScript);
int cardsLeft{ 52 };
std::map<int, int> deck{ {10, 16} };
for ( auto i{1}; i < 10; ++i ) deck[i] = 4;
std::string tkn;
std::istringstream iss{ s };
while ( std::getline(iss, tkn, '.') ) {
if ( !std::regex_search(tkn, rgx) ) continue;
for ( const auto& c : tkn ) { --deck[ values.at(c) ]; }
cardsLeft -= std::size(tkn);
}
int cnt{ 0 };
for ( const auto& [val, nb]: deck )
if ( val < threshold ) { cnt += nb; }
return std::round(cnt * 100.0 / cardsLeft);
}
int main()
{
std::string SoC;
int thsh;
getline(std::cin, SoC);
std::cin >> thsh;
std::cout << doTheJob(SoC, thsh) << "%\n";
}