From 37d59142ab4901cb22214ccaa2ec342925644c46 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Tue, 19 Dec 2023 13:05:37 +0100 Subject: [PATCH] Day 7 --- src/days/07/Day07.cpp | 107 ++++++++++++++++++++++++++++++++++++++++-- src/days/07/Day07.h | 11 ++++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/src/days/07/Day07.cpp b/src/days/07/Day07.cpp index d89165a..7b134c1 100644 --- a/src/days/07/Day07.cpp +++ b/src/days/07/Day07.cpp @@ -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(); -} \ No newline at end of file + uint64 sum = 0; + + uint64 rank = 1; + for (const Pair& pair : parsePairs(true)) + sum += rank++ * pair.bid; + + return to_string(sum); +} + +set Day07::parsePairs(bool joker) const { + set 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 countMap; + for (char c : labels) + countMap[c]++; + vector counts; + counts.reserve(countMap.size()); + for (auto &[key, value] : countMap) + counts.push_back(value); + std::sort(counts.begin(), counts.end()); + + if (counts == vector{5}) + type = FIVE; + if (counts == vector{1, 4}) + type = FOUR; + if (counts == vector{2, 3}) + type = FULL_HOUSE; + if (counts == vector{1, 1, 3}) + type = THREE; + if (counts == vector{1, 2, 2}) + type = TWO_PAIR; + if (counts == vector{1, 1, 1, 2}) + type = ONE_PAIR; + if (counts == vector{1, 1, 1, 1, 1}) + type = HIGH_CARD; + + if (joker){ + uint8_t jC = countMap['J']; + if (counts == vector{1, 4} && (jC == 1 || jC == 4)) + type = FIVE; + if (counts == vector{2, 3} && (jC == 2 || jC == 3)) + type = FIVE; + if (counts == vector{1, 1, 3} && (jC == 1 || jC == 3)) + type = FOUR; + if (counts == vector{1, 2, 2} && jC == 1) + type = FULL_HOUSE; + if (counts == vector{1, 2, 2} && jC == 2) + type = FOUR; + if (counts == vector{1, 1, 1, 2} && (jC == 1 || jC == 2)) + type = THREE; + if (counts == vector{1, 1, 1, 1, 1} && jC == 1) + type = ONE_PAIR; + } +} diff --git a/src/days/07/Day07.h b/src/days/07/Day07.h index 39f98fe..239250c 100644 --- a/src/days/07/Day07.h +++ b/src/days/07/Day07.h @@ -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 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 parsePairs(bool joker=false) const; }; \ No newline at end of file