Day 5: Part 2
This commit is contained in:
parent
d7686a2bda
commit
dcae65ce86
@ -176,6 +176,42 @@ std::vector<Update> determineCorrectUpdates(std::vector<Update>& updates, std::v
|
||||
return validUpdates;
|
||||
}
|
||||
|
||||
std::vector<Update> determineIncorrectUpdates(std::vector<Update>& updates, std::vector<Rule>& rules) {
|
||||
std::vector<Update> validUpdates;
|
||||
for(const Update& update : updates) {
|
||||
if(!isUpdateCorrect(update, rules)) {
|
||||
validUpdates.push_back(update);
|
||||
}
|
||||
}
|
||||
return validUpdates;
|
||||
}
|
||||
|
||||
Update correctUpdate(const Update& update, std::vector<Rule>& rules) {
|
||||
Update correctedUpdate = update;
|
||||
for(int i=0; i<correctedUpdate.pages.size(); i++) {
|
||||
for(int j=0; j<i; j++) {
|
||||
if(!isBefore(rules, correctedUpdate.pages[j], correctedUpdate.pages[i])) {
|
||||
std::swap(correctedUpdate.pages[j], correctedUpdate.pages[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for(int j=i+1; j<correctedUpdate.pages.size(); j++) {
|
||||
if(!isAfter(rules, correctedUpdate.pages[j], correctedUpdate.pages[i])) {
|
||||
std::swap(correctedUpdate.pages[j], correctedUpdate.pages[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return correctedUpdate;
|
||||
}
|
||||
|
||||
std::vector<Update> correctUpdates(std::vector<Update>& updates, std::vector<Rule>& rules) {
|
||||
std::vector<Update> correctedUpdates;
|
||||
for(const Update& update : updates) {
|
||||
correctedUpdates.push_back(correctUpdate(update, rules));
|
||||
}
|
||||
return correctedUpdates;
|
||||
}
|
||||
|
||||
int calcMiddleSum(std::vector<Update>& updates) {
|
||||
int sum = 0;
|
||||
for(const auto& update: updates) {
|
||||
@ -188,8 +224,13 @@ int main() {
|
||||
std::vector<std::string> input = readTextFromFile("../input.txt");
|
||||
std::vector<Rule> rules = parseRules(input);
|
||||
std::vector<Update> updates = parseUpdates(input);
|
||||
std::vector<Update> correctUpdates = determineCorrectUpdates(updates, rules);
|
||||
int result = calcMiddleSum(correctUpdates);
|
||||
std::vector<Update> validUpdates = determineCorrectUpdates(updates, rules);
|
||||
int result = calcMiddleSum(validUpdates);
|
||||
std::cout << "The middle sum of the correct Updates is: " << result << std::endl;
|
||||
|
||||
std::vector<Update> incorrectUpdates = determineIncorrectUpdates(updates, rules);
|
||||
std::vector<Update> correctedUpdates = correctUpdates(incorrectUpdates, rules);
|
||||
int result2 = calcMiddleSum(correctedUpdates);
|
||||
std::cout << "The middle sum of the corrected Updates is: " << result2 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user