Benjamin Kraft 9 months ago
parent 37153165fa
commit 37d59142ab
  1. 107
      src/days/07/Day07.cpp
  2. 11
      src/days/07/Day07.h

@ -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;
}
}

@ -3,8 +3,17 @@
#include "../../Day.h"
class Day07 : public Day {
struct Pair {
enum Type {HIGH_CARD = 0, ONE_PAIR, TWO_PAIR, THREE, FULL_HOUSE, FOUR, FIVE};
std::array<uint8_t, 5> hand;
string labels;
uint64 bid;
Type type;
void calculateType(bool joker=false);
bool operator <(const Pair &other) const;
};
protected:
Result Task1() override;
Result Task2() override;
set<Pair> parsePairs(bool joker=false) const;
};
Loading…
Cancel
Save