#include #include #include std::vector readTextFromFile(const std::string& fileName) { std::vector lines; std::ifstream file(fileName); if (!file) { // Überprüft, ob die Datei geöffnet werden konnte std::cerr << "Fehler: Datei konnte nicht geöffnet werden: " << fileName << std::endl; return lines; } std::string s; while(getline(file, s)) { lines.push_back(s); } return lines; } std::vector splitString(const std::string& input, char ch) { std::vector splittedString; size_t pos = input.find(ch); size_t initialPos = 0; while(pos != std::string::npos) { splittedString.push_back(input.substr(initialPos, pos - initialPos)); initialPos = pos + 1; pos = input.find(ch, initialPos); } splittedString.push_back(input.substr(initialPos, std::min(pos, input.size()) - initialPos)); return splittedString; } std::vector convertReportToNumericalReport(const std::vector& splittedReport) { std::vector numericalReport; for(const auto& s : splittedReport) { numericalReport.push_back(std::stoi(s)); } return numericalReport; } bool isReportSave(std::vector& numericalReport) { bool isIncreasing = true; bool isDecreasing = true; for(int i=0; i numericalReport.at(i+1)) { isDecreasing = false; } if(numericalReport.at(i) < numericalReport.at(i+1)) { isIncreasing = false; } int differ = std::abs(numericalReport.at(i) - numericalReport.at(i+1)); if(differ < 1 || differ > 3) { return false; } } 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) { 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++; } } return saveReports; } int main() { std::string inputFile = "../input.txt"; std::vector reports = readTextFromFile(inputFile); int result = calcNumberSaveReports(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; }