AdventOfCode2024/Aufgabe 2/main.cpp

118 lines
3.7 KiB
C++
Raw Permalink Normal View History

2024-12-08 08:38:24 +00:00
#include <iostream>
#include <vector>
#include <fstream>
std::vector<std::string> readTextFromFile(const std::string& fileName) {
std::vector<std::string> 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<std::string> splitString(const std::string& input, char ch) {
std::vector<std::string> 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<int> convertReportToNumericalReport(const std::vector<std::string>& splittedReport) {
std::vector<int> numericalReport;
for(const auto& s : splittedReport) {
numericalReport.push_back(std::stoi(s));
}
return numericalReport;
}
2024-12-08 08:54:11 +00:00
bool isReportSave(std::vector<int>& numericalReport) {
2024-12-08 08:38:24 +00:00
bool isIncreasing = true;
bool isDecreasing = true;
for(int i=0; i<numericalReport.size()-1; i++) {
if(numericalReport.at(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;
}
2024-12-08 08:54:11 +00:00
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;
}
2024-12-08 08:38:24 +00:00
int calcNumberSaveReports(std::vector<std::string> reports) {
int saveReports = 0;
for(const auto& report : reports) {
2024-12-08 08:54:11 +00:00
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)) {
2024-12-08 08:38:24 +00:00
saveReports++;
}
}
return saveReports;
}
int main() {
std::string inputFile = "../input.txt";
std::vector<std::string> reports = readTextFromFile(inputFile);
int result = calcNumberSaveReports(reports);
2024-12-08 08:54:11 +00:00
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;
2024-12-08 08:38:24 +00:00
return 0;
}