-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.cpp
More file actions
180 lines (165 loc) · 4.08 KB
/
7.cpp
File metadata and controls
180 lines (165 loc) · 4.08 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "common.h"
namespace {
enum class Type { HC, ONP, TWP, THK, FH, FRK, FVK };
std::unordered_map<char, int> count(std::string_view sv) {
std::unordered_map<char, int> cnt{};
for (char c : sv) {
++cnt[c];
}
return cnt;
}
struct Hand {
std::string_view cards;
int bid;
};
int64_t p1(const std::vector<std::string> &in) {
std::vector<Hand> hands{};
for (const auto &l : in) {
const auto sp = gb::split(l, " ");
hands.emplace_back(sp.at(0), gb::svtoT<int>(sp.at(1)));
}
const auto rank = [](char c) -> int {
if (c == 'A')
return 14;
if (c == 'K')
return 13;
if (c == 'Q')
return 12;
if (c == 'J')
return 11;
if (c == 'T')
return 10;
if (c >= '2' && c <= '9')
return c - '0';
assert(false);
};
const auto type = [](std::string_view cards) -> Type {
auto cnt = count(cards);
if (cnt.size() == 1) {
return Type::FVK;
}
if (cnt.size() == 2) {
if (cnt.cbegin()->second == 1 || cnt.cbegin()->second == 4) {
return Type::FRK;
} else {
return Type::FH;
}
}
if (cnt.size() == 3) {
for (const auto [k, v] : cnt) {
if (v == 3)
return Type::THK;
}
return Type::TWP;
}
if (cnt.size() == 5) {
return Type::HC;
}
return Type::ONP;
};
const auto handsort = [rank, type](Hand a, Hand b) -> bool {
auto cnta = count(a.cards);
auto cntb = count(b.cards);
if (type(a.cards) == type(b.cards)) {
for (size_t i{0}; i < a.cards.length(); ++i) {
if (a.cards[i] != b.cards[i]) {
return rank(a.cards[i]) < rank(b.cards[i]);
}
}
assert(false);
}
return type(a.cards) < type(b.cards);
};
std::sort(hands.begin(), hands.end(), handsort);
int64_t r{0};
{
size_t i{hands.size()};
for (auto it = hands.rbegin(); it != hands.rend(); ++it, --i) {
r += (it->bid * i);
}
}
return r;
}
int64_t p2(const std::vector<std::string> &in) {
std::vector<Hand> hands{};
for (const auto &l : in) {
const auto sp = gb::split(l, " ");
hands.emplace_back(sp.at(0), gb::svtoT<int>(sp.at(1)));
}
const auto rank = [](char c) -> int {
if (c == 'A')
return 14;
if (c == 'K')
return 13;
if (c == 'Q')
return 12;
if (c == 'T')
return 11;
if (c >= '2' && c <= '9')
return c - '0' + 1;
if (c == 'J')
return 2;
assert(false);
};
const auto type = [](const std::string_view cards) -> Type {
auto cnt = count(cards);
if (cnt.size() == 1) {
return Type::FVK;
}
// take the maximum element and bump it by how many ever J you have
size_t j = cnt['J'];
std::erase_if(cnt, [](const auto &item) { return item.first == 'J'; });
const auto mx =
*std::max_element(cnt.cbegin(), cnt.cend(),
[](std::pair<char, int> a, std::pair<char, int> b) {
return a.second < b.second;
});
if (mx.second + j == 5) {
return Type::FVK;
}
if (mx.second + j == 4) {
return Type::FRK;
}
if (mx.second + j == 3) {
if (cnt.size() == 2)
return Type::FH;
else
return Type::THK;
}
if (mx.second + j == 2) {
if (cnt.size() == 3)
return Type::TWP;
else
return Type::ONP;
}
return Type::HC;
};
const auto handsort = [rank, type](Hand a, Hand b) -> bool {
auto cnta = count(a.cards);
auto cntb = count(b.cards);
if (type(a.cards) == type(b.cards)) {
for (size_t i{0}; i < a.cards.length(); ++i) {
if (a.cards[i] != b.cards[i]) {
return rank(a.cards[i]) < rank(b.cards[i]);
}
}
assert(false);
}
return type(a.cards) < type(b.cards);
};
std::sort(hands.begin(), hands.end(), handsort);
int64_t r{0};
{
size_t i{hands.size()};
for (auto it = hands.rbegin(); it != hands.rend(); ++it, --i) {
r += (it->bid * i);
}
}
return r;
}
} // namespace
int main() {
const auto &in = gb::readIn();
gb::writeOut(p1(in));
gb::writeOut(p2(in));
}