|
|
|
@ -3,22 +3,17 @@ |
|
|
|
|
Result Day12::Task1() { |
|
|
|
|
uint64 sum = 0; |
|
|
|
|
|
|
|
|
|
for (const auto& [conditions, groups] : parseRecords()){ |
|
|
|
|
uint64 localSum = 0; |
|
|
|
|
auto arrangements = allArrangements({conditions, groups}); |
|
|
|
|
for (const auto& arrangement : arrangements){ |
|
|
|
|
for (const auto& [conditions, groups] : parseRecords()) |
|
|
|
|
for (const auto& arrangement : allArrangements({conditions, groups})) |
|
|
|
|
if (matches(conditions, arrangement)) |
|
|
|
|
localSum++; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
size_t count = arrangements.size(); |
|
|
|
|
sum += localSum; |
|
|
|
|
} |
|
|
|
|
sum++; |
|
|
|
|
|
|
|
|
|
return to_string(sum); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Result Day12::Task2() { |
|
|
|
|
arrangementsCache.clear(); |
|
|
|
|
|
|
|
|
|
uint64 sum = 0; |
|
|
|
|
|
|
|
|
|
size_t index = 0; |
|
|
|
@ -35,7 +30,6 @@ Result Day12::Task2() { |
|
|
|
|
sum++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << index++ << endl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -56,6 +50,8 @@ vector<Day12::Record> Day12::parseRecords() const { |
|
|
|
|
|
|
|
|
|
vector<Day12::Arrangement> Day12::allArrangements(const Day12::Record &record) { |
|
|
|
|
auto &[conditions, groups] = record; |
|
|
|
|
if (arrangementsCache.contains({conditions.size(), groups})) |
|
|
|
|
return arrangementsCache.at({conditions.size(), groups}); |
|
|
|
|
|
|
|
|
|
vector<Arrangement> arrangements; |
|
|
|
|
|
|
|
|
@ -68,6 +64,7 @@ vector<Day12::Arrangement> Day12::allArrangements(const Day12::Record &record) { |
|
|
|
|
arrangements.push_back(a); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
arrangementsCache[{conditions.size(), groups}] = arrangements; |
|
|
|
|
return arrangements; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -91,9 +88,12 @@ vector<Day12::Arrangement> Day12::allArrangements(const Day12::Record &record) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
arrangementsCache[{conditions.size(), groups}] = arrangements; |
|
|
|
|
return arrangements; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::unordered_map<pair<size_t, vector<size_t>>, vector<Day12::Arrangement>, Day12::RecordHash> Day12::arrangementsCache; |
|
|
|
|
|
|
|
|
|
bool Day12::matches(const string &conditions, const Day12::Arrangement &arrangement) { |
|
|
|
|
for (size_t i = 0; i < arrangement.size(); i++){ |
|
|
|
|
char cond = conditions[i]; |
|
|
|
@ -103,3 +103,7 @@ bool Day12::matches(const string &conditions, const Day12::Arrangement &arrangem |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size_t Day12::RecordHash::operator ()(const pair<size_t, vector<uint64>> &record) const{ |
|
|
|
|
return sum(record.second) * record.first * record.second[0]; |
|
|
|
|
} |
|
|
|
|