Day 2: Part 2

This commit is contained in:
Fawkes100 2024-12-08 09:54:11 +01:00
parent f26c591168
commit 62a92ecb84

View File

@ -40,10 +40,7 @@ std::vector<int> convertReportToNumericalReport(const std::vector<std::string>&
return numericalReport;
}
bool isReportSave(std::string report) {
std::vector<std::string> splittedReport = splitString(report, ' ');
std::vector<int> numericalReport = convertReportToNumericalReport(splittedReport);
bool isReportSave(std::vector<int>& numericalReport) {
bool isIncreasing = true;
bool isDecreasing = true;
@ -65,10 +62,42 @@ bool isReportSave(std::string report) {
return isIncreasing || isDecreasing;
}
bool isReportSaveTolerateBadLevel(std::vector<int>& numericalReport) {
//First: Check if report is already counted as save
if(isReportSave(numericalReport)) return true;
//Check if a single level can be removed such that the report counts as safe
for(int i=0; i < numericalReport.size(); i++) {
int potentialBadLevel = numericalReport.at(i);
numericalReport.erase(numericalReport.begin() + i);
if(isReportSave(numericalReport)) {
return true;
} else {
numericalReport.insert(numericalReport.begin() + i, potentialBadLevel);
}
}
return false;
}
int calcNumberSaveReports(std::vector<std::string> reports) {
int saveReports = 0;
for(const auto& report : reports) {
if(isReportSave(report)) {
std::vector<std::string> splittedReport = splitString(report, ' ');
std::vector<int> numericalReport = convertReportToNumericalReport(splittedReport);
if(isReportSave(numericalReport)) {
saveReports++;
}
}
return saveReports;
}
int calcNumberSaveReportsTolerateBadLevel(std::vector<std::string> reports) {
int saveReports = 0;
for(const auto& report : reports) {
std::vector<std::string> splittedReport = splitString(report, ' ');
std::vector<int> numericalReport = convertReportToNumericalReport(splittedReport);
if(isReportSaveTolerateBadLevel(numericalReport)) {
saveReports++;
}
}
@ -80,6 +109,9 @@ int main() {
std::vector<std::string> reports = readTextFromFile(inputFile);
int result = calcNumberSaveReports(reports);
std::cout << "There are " << result << " save reports";
std::cout << "There are " << result << " save reports" << std::endl;
int resultBadLevel = calcNumberSaveReportsTolerateBadLevel(reports);
std::cout << "There are " << resultBadLevel << " save reports when tolerating single bad levels!" << std::endl;
return 0;
}