parent
14ff577615
commit
df538935a5
2 changed files with 59 additions and 3 deletions
@ -1,9 +1,62 @@ |
||||
#include "Day04.h" |
||||
#include <cmath> |
||||
|
||||
Result Day04::Task1() { |
||||
return Day::Task1(); |
||||
uint64 sum = 0; |
||||
|
||||
for (const auto &[winning, having] : parseCards()){ |
||||
std::set<uint8_t> result; |
||||
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(), std::inserter(result, result.begin())); |
||||
if (!result.empty()){ |
||||
sum += uint64(powl(2, result.size() - 1)); |
||||
} |
||||
} |
||||
|
||||
return to_string(sum); |
||||
} |
||||
|
||||
Result Day04::Task2() { |
||||
return Day::Task2(); |
||||
vector<uint64> matches; |
||||
|
||||
for (const auto &[winning, having] : parseCards()){ |
||||
std::set<uint8_t> result; |
||||
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(), std::inserter(result, result.begin())); |
||||
matches.push_back(result.size()); |
||||
} |
||||
|
||||
vector<uint64> counts(matches.size(), 1); |
||||
|
||||
for (size_t i = 0; i < matches.size(); i++){ |
||||
for (size_t instance = 0; instance < counts[i]; instance++){ |
||||
for (size_t k = 0; k < matches[i]; k++){ |
||||
counts[i + k + 1]++; |
||||
} |
||||
} |
||||
} |
||||
|
||||
uint64 sum = std::reduce(counts.begin(), counts.end(), 0, [](uint64 a, uint64 b){return a + b;}); |
||||
|
||||
return to_string(sum); |
||||
} |
||||
|
||||
vector<Day04::Card> Day04::parseCards() const { |
||||
vector<Card> cards; |
||||
|
||||
for (string line : input){ |
||||
line = line.substr(line.find(':') + 1); |
||||
string left = line.substr(0, line.find(" |")); |
||||
string right = line.substr(line.find(" |") + 2); |
||||
|
||||
set<uint8_t> winning; |
||||
for (size_t i = 0; i < left.length(); i += 3) |
||||
winning.insert(std::stoul(left.substr(i, 3))); |
||||
|
||||
set<uint8_t> having; |
||||
for (size_t i = 0; i < right.length(); i += 3) |
||||
having.insert(std::stoul(right.substr(i, 3))); |
||||
|
||||
cards.emplace_back(winning, having); |
||||
} |
||||
|
||||
return cards; |
||||
} |
Loading…
Reference in new issue