Day 12 Task 1

main
Benjamin Kraft 9 months ago
parent 19b1f4d145
commit e62e675be2
  1. 57
      src/days/12/Day12.cpp
  2. 1
      src/days/12/Day12.h

@ -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 (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;
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());
}
}
if (possible)
for (const auto& arrangement : allArrangements({conditions, groups})){
if (matches(conditions, arrangement))
sum++;
}
cout << index++ << endl;
}
return to_string(sum);
}
@ -98,3 +93,13 @@ vector<Day12::Arrangement> 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;
}

@ -7,6 +7,7 @@ class Day12 : public Day {
typedef vector<bool> Arrangement;
vector<Record> parseRecords() const;
static vector<Arrangement> allArrangements(const Record &record);
static bool matches(const string& conditions, const Arrangement& arrangement);
protected:
Result Task1() override;

Loading…
Cancel
Save