#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::string removeExtraSpaces(const std::string& input) { std::string result; bool inSpace = false; for(char c: input) { if(c == ' ') { if(!inSpace) { result += c; inSpace = true; } } else { result += c; inSpace = false; } } return result; } 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::tuple, std::vector> parseInputVector(std::vector inputVector) { std::vector leftArray; std::vector rightArray; for(const auto &inputString : inputVector) { std::string trimmedInputString = removeExtraSpaces(inputString); std::vector splittedString = splitString(trimmedInputString, ' '); std::cout << "Splittted-Size: " << splittedString.size() << std::endl; int leftValue = std::stoi(splittedString.at(0)); int rightValue = std::stoi(splittedString.at(1)); leftArray.push_back(leftValue); rightArray.push_back(rightValue); } return std::make_tuple(leftArray, rightArray); } std::vector merge(const std::vector& left, const std::vector& right) { std::vector resultingList; size_t i = 0, j = 0; while(i < left.size() && j < right.size()) { if(left[i] <= right[j]) { resultingList.push_back(left[i]); i++; } else { resultingList.push_back(right[j]); j++; } } while(i < left.size()) { resultingList.push_back(left[i]); i++; } while(j < right.size()) { resultingList.push_back(right[j]); j++; } return resultingList; } std::vector mergeSort(std::vector& vec) { if(vec.size() <= 1) return vec; int mid = vec.size()/2; std::vector leftList(vec.begin(), vec.begin() + mid); std::vector rightList(vec.begin() + mid, vec.end()); return merge(mergeSort(leftList), mergeSort(rightList)); } int solve(std::vector& left, std::vector& right) { std::vector sortedLeft = mergeSort(left); std::vector sortedRight = mergeSort(right); int sum = 0; if(sortedLeft.size() == sortedRight.size()) { for(int i=0; i& vec, int element) { int occurences = 0; for(const auto& i: vec) { if(i == element) { occurences++; } } return occurences; } int calcSimilarity(std::vector& left, std::vector& right) { int similarityScore = 0; for(int i : left) { int occurencesInRightList = countOccurenceInList(right, i); similarityScore += i * occurencesInRightList; } return similarityScore; } int main() { std::string inputFile = "../input.txt"; std::vector input = readTextFromFile(inputFile); auto [leftArray, rightArray] = parseInputVector(input); int result = solve(leftArray, rightArray); std::cout << "The result is: " << result << std::endl; int similarityScore = calcSimilarity(leftArray, rightArray); std::cout << "The similarity is " << similarityScore << std::endl; return 0; }