From 62a92ecb841acae2ca8119dfce06e5d4c8dfe1a6 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Sun, 8 Dec 2024 09:54:11 +0100 Subject: [PATCH] Day 2: Part 2 --- Aufgabe 2/main.cpp | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Aufgabe 2/main.cpp b/Aufgabe 2/main.cpp index 88c883b..424eb6a 100644 --- a/Aufgabe 2/main.cpp +++ b/Aufgabe 2/main.cpp @@ -40,10 +40,7 @@ std::vector convertReportToNumericalReport(const std::vector& return numericalReport; } -bool isReportSave(std::string report) { - std::vector splittedReport = splitString(report, ' '); - std::vector numericalReport = convertReportToNumericalReport(splittedReport); - +bool isReportSave(std::vector& numericalReport) { bool isIncreasing = true; bool isDecreasing = true; @@ -65,10 +62,42 @@ bool isReportSave(std::string report) { return isIncreasing || isDecreasing; } +bool isReportSaveTolerateBadLevel(std::vector& 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 reports) { int saveReports = 0; for(const auto& report : reports) { - if(isReportSave(report)) { + std::vector splittedReport = splitString(report, ' '); + std::vector numericalReport = convertReportToNumericalReport(splittedReport); + if(isReportSave(numericalReport)) { + saveReports++; + } + } + return saveReports; +} + +int calcNumberSaveReportsTolerateBadLevel(std::vector reports) { + int saveReports = 0; + for(const auto& report : reports) { + std::vector splittedReport = splitString(report, ' '); + std::vector numericalReport = convertReportToNumericalReport(splittedReport); + if(isReportSaveTolerateBadLevel(numericalReport)) { saveReports++; } } @@ -80,6 +109,9 @@ int main() { std::vector 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; }