parent
37153165fa
commit
37d59142ab
2 changed files with 114 additions and 4 deletions
@ -1,9 +1,110 @@ |
||||
#include "Day07.h" |
||||
|
||||
Result Day07::Task1() { |
||||
return Day::Task1(); |
||||
uint64 sum = 0; |
||||
|
||||
uint64 rank = 1; |
||||
for (const Pair& pair : parsePairs()) |
||||
sum += rank++ * pair.bid; |
||||
|
||||
return to_string(sum); |
||||
} |
||||
|
||||
Result Day07::Task2() { |
||||
return Day::Task2(); |
||||
} |
||||
uint64 sum = 0; |
||||
|
||||
uint64 rank = 1; |
||||
for (const Pair& pair : parsePairs(true)) |
||||
sum += rank++ * pair.bid; |
||||
|
||||
return to_string(sum); |
||||
} |
||||
|
||||
set<Day07::Pair> Day07::parsePairs(bool joker) const { |
||||
set<Pair> pairs; |
||||
|
||||
for (const string &line : input){ |
||||
string handRaw = split(line, ' ')[0]; |
||||
uint64 bid = std::stoul(split(line, ' ')[1]); |
||||
|
||||
Pair pair {}; |
||||
pair.bid = bid; |
||||
pair.labels = handRaw; |
||||
for (size_t i = 0; i < 5; i++){ |
||||
char c = handRaw[i]; |
||||
uint8_t digit; |
||||
if (isDigit(c, digit)) |
||||
pair.hand[i] = digit; |
||||
else { |
||||
pair.hand[i] = c == 'T' ? 10 : (c == 'J' ? 11 : (c == 'Q' ? 12 : (c == 'K' ? 13 : (c == 'A' ? 14 : 100)))); |
||||
if (joker) |
||||
pair.hand[i] = c == 'T' ? 10 : (c == 'J' ? 1 : (c == 'Q' ? 11 : (c == 'K' ? 12 : (c == 'A' ? 13 : 100)))); |
||||
} |
||||
|
||||
} |
||||
pair.calculateType(joker); |
||||
pairs.insert(pair); |
||||
} |
||||
|
||||
return pairs; |
||||
} |
||||
|
||||
bool Day07::Pair::operator <(const Day07::Pair &other) const { |
||||
if (type < other.type) |
||||
return true; |
||||
if (type == other.type){ |
||||
for (size_t i = 0; i < 5; i++){ |
||||
if (hand[i] < other.hand[i]) |
||||
return true; |
||||
if (hand[i] == other.hand[i]) |
||||
continue; |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
void Day07::Pair::calculateType(bool joker) { |
||||
map<char, uint8_t> countMap; |
||||
for (char c : labels) |
||||
countMap[c]++; |
||||
vector<uint8_t> counts; |
||||
counts.reserve(countMap.size()); |
||||
for (auto &[key, value] : countMap) |
||||
counts.push_back(value); |
||||
std::sort(counts.begin(), counts.end()); |
||||
|
||||
if (counts == vector<uint8_t>{5}) |
||||
type = FIVE; |
||||
if (counts == vector<uint8_t>{1, 4}) |
||||
type = FOUR; |
||||
if (counts == vector<uint8_t>{2, 3}) |
||||
type = FULL_HOUSE; |
||||
if (counts == vector<uint8_t>{1, 1, 3}) |
||||
type = THREE; |
||||
if (counts == vector<uint8_t>{1, 2, 2}) |
||||
type = TWO_PAIR; |
||||
if (counts == vector<uint8_t>{1, 1, 1, 2}) |
||||
type = ONE_PAIR; |
||||
if (counts == vector<uint8_t>{1, 1, 1, 1, 1}) |
||||
type = HIGH_CARD; |
||||
|
||||
if (joker){ |
||||
uint8_t jC = countMap['J']; |
||||
if (counts == vector<uint8_t>{1, 4} && (jC == 1 || jC == 4)) |
||||
type = FIVE; |
||||
if (counts == vector<uint8_t>{2, 3} && (jC == 2 || jC == 3)) |
||||
type = FIVE; |
||||
if (counts == vector<uint8_t>{1, 1, 3} && (jC == 1 || jC == 3)) |
||||
type = FOUR; |
||||
if (counts == vector<uint8_t>{1, 2, 2} && jC == 1) |
||||
type = FULL_HOUSE; |
||||
if (counts == vector<uint8_t>{1, 2, 2} && jC == 2) |
||||
type = FOUR; |
||||
if (counts == vector<uint8_t>{1, 1, 1, 2} && (jC == 1 || jC == 2)) |
||||
type = THREE; |
||||
if (counts == vector<uint8_t>{1, 1, 1, 1, 1} && jC == 1) |
||||
type = ONE_PAIR; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue