parent
a99f11a3ee
commit
19b1f4d145
3 changed files with 104 additions and 4 deletions
@ -1,9 +1,100 @@ |
|||||||
#include "Day12.h" |
#include "Day12.h" |
||||||
|
|
||||||
Result Day12::Task1() { |
Result Day12::Task1() { |
||||||
return Day::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++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return to_string(sum); |
||||||
} |
} |
||||||
|
|
||||||
Result Day12::Task2() { |
Result Day12::Task2() { |
||||||
return Day::Task2(); |
|
||||||
} |
|
||||||
|
|
||||||
|
uint64 sum = 0; |
||||||
|
|
||||||
|
for (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++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return to_string(sum); |
||||||
|
} |
||||||
|
|
||||||
|
vector<Day12::Record> Day12::parseRecords() const { |
||||||
|
vector<Record> records; |
||||||
|
|
||||||
|
for (const string &line : input){ |
||||||
|
auto both = split(line, ' '); |
||||||
|
records.emplace_back(both[0], parseUInts(both[1])); |
||||||
|
} |
||||||
|
|
||||||
|
return records; |
||||||
|
} |
||||||
|
|
||||||
|
vector<Day12::Arrangement> Day12::allArrangements(const Day12::Record &record) { |
||||||
|
auto &[conditions, groups] = record; |
||||||
|
|
||||||
|
vector<Arrangement> arrangements; |
||||||
|
|
||||||
|
if (groups.size() == 1){ |
||||||
|
size_t group = groups[0]; |
||||||
|
for (size_t i = 0; i < conditions.size() - group + 1; i++){ |
||||||
|
Arrangement a(conditions.size(), false); |
||||||
|
for (size_t j = 0; j < group; j++) |
||||||
|
a[i + j] = true; |
||||||
|
arrangements.push_back(a); |
||||||
|
} |
||||||
|
|
||||||
|
return arrangements; |
||||||
|
} |
||||||
|
|
||||||
|
vector<size_t> remainingGroups(groups.size() - 1); |
||||||
|
std::copy(++groups.begin(), groups.end(), remainingGroups.begin()); |
||||||
|
|
||||||
|
size_t neededSpace = sum(remainingGroups) + remainingGroups.size() - 1; |
||||||
|
size_t startLimit = conditions.size() - neededSpace - 1; |
||||||
|
|
||||||
|
for (size_t i = groups[0] - 1; i < startLimit; i++){ |
||||||
|
Arrangement base(i + 2, true); |
||||||
|
base.back() = false; |
||||||
|
for (size_t j = 0; j < i - groups[0] + 1; j++) |
||||||
|
base[j] = false; |
||||||
|
|
||||||
|
auto addedRight = allArrangements({conditions.substr(i + 2), remainingGroups}); |
||||||
|
for (auto &a : addedRight){ |
||||||
|
Arrangement combined(base); |
||||||
|
std::copy(a.begin(), a.end(), std::back_inserter(combined)); |
||||||
|
arrangements.push_back(combined); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return arrangements; |
||||||
|
} |
||||||
|
Loading…
Reference in new issue