mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 12:11: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;
|
||||
}
|
||||
|
||||
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,
|
||||
const int secondPage,
|
||||
const vector<pair<int, int>> &rules)
|
||||
|
|
@ -82,15 +75,23 @@ int main()
|
|||
sort(rules.begin(), rules.end());
|
||||
|
||||
int sumOfCorrectMiddlePages = 0;
|
||||
int sumOfCorrectedMiddlePages = 0;
|
||||
|
||||
for (const vector<int> &pages : manuals) {
|
||||
const vector<int> properlyOrderedPages = getProperlyOrderedPages(pages, rules);
|
||||
if (pages == properlyOrderedPages) {
|
||||
sumOfCorrectMiddlePages += getMiddlePageNumber(pages);
|
||||
} else {
|
||||
sumOfCorrectedMiddlePages += getMiddlePageNumber(properlyOrderedPages);
|
||||
}
|
||||
}
|
||||
|
||||
// Part one
|
||||
cout << "Sum of the middle page numbers of all correctly ordered manuals: "
|
||||
<< 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