advent-of-code/2023/7/main-2.cpp
eriedaberrie c3c5068e62 2023 day 7
Pain
2023-12-06 22:30:37 -08:00

82 lines
1.4 KiB
C++

#include <bits/stdc++.h>
using namespace std;
const string order = "AKQT98765432J";
inline int getval(string s) {
sort(s.begin(), s.end());
vector<int> groups;
char last = '\0';
int nj = 0;
for (char c : s) {
if (c == 'J') {
nj++;
continue;
}
if (c != last) {
groups.push_back(1);
last = c;
} else {
groups.back()++;
}
}
switch (groups.size()) {
case 0:
case 1:
return 1;
case 2:
return (min(groups[0], groups[1]) == 1) ? 2 : 3;
case 3:
return (max(groups[0], max(groups[1], groups[2])) + nj == 3) ? 4 : 5;
case 4:
return 6;
default:
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';
}