|
|
@ -4,47 +4,42 @@ Result Day12::Task1() { |
|
|
|
uint64 sum = 0; |
|
|
|
uint64 sum = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (const auto& [conditions, groups] : parseRecords()){ |
|
|
|
for (const auto& [conditions, groups] : parseRecords()){ |
|
|
|
for (const auto& arrangement : allArrangements({conditions, groups})){ |
|
|
|
uint64 localSum = 0; |
|
|
|
bool possible = true; |
|
|
|
auto arrangements = allArrangements({conditions, groups}); |
|
|
|
for (size_t i = 0; i < arrangement.size(); i++){ |
|
|
|
for (const auto& arrangement : arrangements){ |
|
|
|
char cond = conditions[i]; |
|
|
|
if (matches(conditions, arrangement)) |
|
|
|
bool broken = arrangement[i]; |
|
|
|
localSum++; |
|
|
|
if (cond == '.' && broken || cond == '#' && !broken){ |
|
|
|
|
|
|
|
possible = false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (possible) |
|
|
|
|
|
|
|
sum++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
size_t count = arrangements.size(); |
|
|
|
|
|
|
|
sum += localSum; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return to_string(sum); |
|
|
|
return to_string(sum); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Result Day12::Task2() { |
|
|
|
Result Day12::Task2() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint64 sum = 0; |
|
|
|
uint64 sum = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (auto[conditions, groups] : parseRecords()){ |
|
|
|
size_t index = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (const auto& arrangement : allArrangements({conditions, groups})){ |
|
|
|
for (auto& [conditions, groups] : parseRecords()){ |
|
|
|
bool possible = true; |
|
|
|
auto condCopy = conditions; |
|
|
|
for (size_t i = 0; i < arrangement.size(); i++){ |
|
|
|
auto groupsCopy = groups; |
|
|
|
char cond = conditions[i]; |
|
|
|
for (size_t i = 0; i < 4; i++){ |
|
|
|
bool broken = arrangement[i]; |
|
|
|
conditions += "?" + condCopy; |
|
|
|
if (cond == '.' && broken || cond == '#' && !broken){ |
|
|
|
groups.insert(groups.end(), groupsCopy.begin(), groupsCopy.end()); |
|
|
|
possible = false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
for (const auto& arrangement : allArrangements({conditions, groups})){ |
|
|
|
if (possible) |
|
|
|
if (matches(conditions, arrangement)) |
|
|
|
sum++; |
|
|
|
sum++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cout << index++ << endl; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return to_string(sum); |
|
|
|
return to_string(sum); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -98,3 +93,13 @@ vector<Day12::Arrangement> Day12::allArrangements(const Day12::Record &record) { |
|
|
|
|
|
|
|
|
|
|
|
return arrangements; |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|