advent-of-code/2023/7/main-1.cpp

68 lines
1.4 KiB
C++
Raw Permalink Normal View History

2023-12-06 22:30:37 -08:00
#include <bits/stdc++.h>
using namespace std;
const string order = "AKQJT98765432";
inline int getval(string s) {
sort(s.begin(), s.end());
if (s[0] == s[4])
return 1;
if (s[0] == s[3] || s[1] == s[4])
return 2;
if ((s[0] == s[2] && s[3] == s[4]) || (s[0] == s[1] && s[2] == s[4]))
return 3;
if (s[0] == s[2] || s[1] == s[3] || s[2] == s[4])
return 4;
if (s[0] == s[1] && (s[2] == s[3] || s[3] == s[4])
|| s[1] == s[2] && s[3] == s[4])
return 5;
if (s[0] == s[1] || s[1] == s[2] || s[2] == s[3] || s[3] == s[4])
return 6;
return 7;
}
inline int geti(char c) {
return find(order.begin(), order.end(), c) - order.begin();
}
bool comp(pair<string, int>& a, pair<string, int>& b) {
string aa = a.first, bb = b.first;
int av = getval(aa), bv = getval(bb);
if (av > bv)
return true;
if (av < bv)
return false;
for (int i = 0; i < 5; i++) {
int ai = geti(aa[i]), bi = geti(bb[i]);
if (ai > bi)
return true;
if (ai < bi) {
return false;
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<pair<string, int>> hands;
string line;
while (getline(cin, line)) {
istringstream ss(line);
string h;
int bid;
ss >> h >> bid;
hands.push_back(make_pair(h, bid));
}
sort(hands.begin(), hands.end(), comp);
int ret = 0;
for (int i = 0; i < hands.size(); i++) {
ret += hands[i].second * (i + 1);
}
cout << ret << '\n';
}