Day 2: Part 2
This commit is contained in:
parent
f26c591168
commit
62a92ecb84
@ -40,10 +40,7 @@ std::vector<int> convertReportToNumericalReport(const std::vector<std::string>&
|
|||||||
return numericalReport;
|
return numericalReport;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isReportSave(std::string report) {
|
bool isReportSave(std::vector<int>& numericalReport) {
|
||||||
std::vector<std::string> splittedReport = splitString(report, ' ');
|
|
||||||
std::vector<int> numericalReport = convertReportToNumericalReport(splittedReport);
|
|
||||||
|
|
||||||
bool isIncreasing = true;
|
bool isIncreasing = true;
|
||||||
bool isDecreasing = true;
|
bool isDecreasing = true;
|
||||||
|
|
||||||
@ -65,10 +62,42 @@ bool isReportSave(std::string report) {
|
|||||||
return isIncreasing || isDecreasing;
|
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 calcNumberSaveReports(std::vector<std::string> reports) {
|
||||||
int saveReports = 0;
|
int saveReports = 0;
|
||||||
for(const auto& report : reports) {
|
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++;
|
saveReports++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,6 +109,9 @@ int main() {
|
|||||||
std::vector<std::string> reports = readTextFromFile(inputFile);
|
std::vector<std::string> reports = readTextFromFile(inputFile);
|
||||||
|
|
||||||
int result = calcNumberSaveReports(reports);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user