mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 15:41:34 +01:00
Solve 2024 day 5 part 2
This commit is contained in:
parent
ca9f0d37ae
commit
86f28a8c38
1 changed files with 8 additions and 7 deletions
|
|
@ -36,13 +36,6 @@ vector<vector<int>> readManuals()
|
||||||
return manuals;
|
return manuals;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sortRules(vector<pair<int, int>> &rules)
|
|
||||||
{
|
|
||||||
sort(rules.begin(), rules.end(), [](const pair<int, int> &a, const pair<int, int> &b) {
|
|
||||||
return a.first < b.first;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isCorrectPageOrdering(const int firstPage,
|
bool isCorrectPageOrdering(const int firstPage,
|
||||||
const int secondPage,
|
const int secondPage,
|
||||||
const vector<pair<int, int>> &rules)
|
const vector<pair<int, int>> &rules)
|
||||||
|
|
@ -82,15 +75,23 @@ int main()
|
||||||
sort(rules.begin(), rules.end());
|
sort(rules.begin(), rules.end());
|
||||||
|
|
||||||
int sumOfCorrectMiddlePages = 0;
|
int sumOfCorrectMiddlePages = 0;
|
||||||
|
int sumOfCorrectedMiddlePages = 0;
|
||||||
|
|
||||||
for (const vector<int> &pages : manuals) {
|
for (const vector<int> &pages : manuals) {
|
||||||
const vector<int> properlyOrderedPages = getProperlyOrderedPages(pages, rules);
|
const vector<int> properlyOrderedPages = getProperlyOrderedPages(pages, rules);
|
||||||
if (pages == properlyOrderedPages) {
|
if (pages == properlyOrderedPages) {
|
||||||
sumOfCorrectMiddlePages += getMiddlePageNumber(pages);
|
sumOfCorrectMiddlePages += getMiddlePageNumber(pages);
|
||||||
|
} else {
|
||||||
|
sumOfCorrectedMiddlePages += getMiddlePageNumber(properlyOrderedPages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Part one
|
// Part one
|
||||||
cout << "Sum of the middle page numbers of all correctly ordered manuals: "
|
cout << "Sum of the middle page numbers of all correctly ordered manuals: "
|
||||||
<< sumOfCorrectMiddlePages << endl;
|
<< sumOfCorrectMiddlePages << endl;
|
||||||
|
|
||||||
|
// Part two
|
||||||
|
cout
|
||||||
|
<< "Sum of the middle page numbers of all incorrectly ordered manuals after ordering them: "
|
||||||
|
<< sumOfCorrectedMiddlePages << endl;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue