From e62e675be22db7b00dbdede1155c9aab8fa1e265 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 21 Dec 2023 16:44:10 +0100 Subject: [PATCH] Day 12 Task 1 --- src/days/12/Day12.cpp | 57 +++++++++++++++++++++++-------------------- src/days/12/Day12.h | 1 + 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/days/12/Day12.cpp b/src/days/12/Day12.cpp index cf84bc7..c159362 100644 --- a/src/days/12/Day12.cpp +++ b/src/days/12/Day12.cpp @@ -4,47 +4,42 @@ Result Day12::Task1() { uint64 sum = 0; for (const auto& [conditions, groups] : parseRecords()){ - for (const auto& arrangement : allArrangements({conditions, groups})){ - bool possible = true; - for (size_t i = 0; i < arrangement.size(); i++){ - char cond = conditions[i]; - bool broken = arrangement[i]; - if (cond == '.' && broken || cond == '#' && !broken){ - possible = false; - break; - } - } - if (possible) - sum++; + uint64 localSum = 0; + auto arrangements = allArrangements({conditions, groups}); + for (const auto& arrangement : arrangements){ + if (matches(conditions, arrangement)) + localSum++; + } + size_t count = arrangements.size(); + sum += localSum; } return to_string(sum); } Result Day12::Task2() { - - - uint64 sum = 0; - for (auto[conditions, groups] : parseRecords()){ + size_t index = 0; + for (auto& [conditions, groups] : parseRecords()){ + auto condCopy = conditions; + auto groupsCopy = groups; + for (size_t i = 0; i < 4; i++){ + conditions += "?" + condCopy; + groups.insert(groups.end(), groupsCopy.begin(), groupsCopy.end()); + } for (const auto& arrangement : allArrangements({conditions, groups})){ - bool possible = true; - for (size_t i = 0; i < arrangement.size(); i++){ - char cond = conditions[i]; - bool broken = arrangement[i]; - if (cond == '.' && broken || cond == '#' && !broken){ - possible = false; - break; - } - } - if (possible) + if (matches(conditions, arrangement)) sum++; } + + + cout << index++ << endl; } + return to_string(sum); } @@ -98,3 +93,13 @@ vector Day12::allArrangements(const Day12::Record &record) { return arrangements; } + +bool Day12::matches(const string &conditions, const Day12::Arrangement &arrangement) { + for (size_t i = 0; i < arrangement.size(); i++){ + char cond = conditions[i]; + bool broken = arrangement[i]; + if (cond == '.' && broken || cond == '#' && !broken) + return false; + } + return true; +} diff --git a/src/days/12/Day12.h b/src/days/12/Day12.h index d24bff4..8e06db9 100644 --- a/src/days/12/Day12.h +++ b/src/days/12/Day12.h @@ -7,6 +7,7 @@ class Day12 : public Day { typedef vector Arrangement; vector parseRecords() const; static vector allArrangements(const Record &record); + static bool matches(const string& conditions, const Arrangement& arrangement); protected: Result Task1() override;