AdventOfCode2024/Aufgabe1/main.cpp

151 lines
4.2 KiB
C++
Raw Normal View History

2024-12-08 08:07:27 +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::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<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::tuple<std::vector<int>, std::vector<int>> parseInputVector(std::vector<std::string> inputVector) {
std::vector<int> leftArray;
std::vector<int> rightArray;
for(const auto &inputString : inputVector) {
std::string trimmedInputString = removeExtraSpaces(inputString);
std::vector<std::string> 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<int> merge(const std::vector<int>& left, const std::vector<int>& right) {
std::vector<int> 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<int> mergeSort(std::vector<int>& vec) {
if(vec.size() <= 1) return vec;
int mid = vec.size()/2;
std::vector<int> leftList(vec.begin(), vec.begin() + mid);
std::vector<int> rightList(vec.begin() + mid, vec.end());
return merge(mergeSort(leftList), mergeSort(rightList));
}
int solve(std::vector<int>& left, std::vector<int>& right) {
std::vector<int> sortedLeft = mergeSort(left);
std::vector<int> sortedRight = mergeSort(right);
int sum = 0;
if(sortedLeft.size() == sortedRight.size()) {
for(int i=0; i<sortedLeft.size(); i++) {
sum += std::abs(sortedLeft.at(i) - sortedRight.at(i));
}
}
return sum;
}
2024-12-08 08:12:26 +00:00
int countOccurenceInList(std::vector<int>& vec, int element) {
int occurences = 0;
for(const auto& i: vec) {
if(i == element) {
occurences++;
}
}
return occurences;
}
int calcSimilarity(std::vector<int>& left, std::vector<int>& right) {
int similarityScore = 0;
for(int i : left) {
int occurencesInRightList = countOccurenceInList(right, i);
similarityScore += i * occurencesInRightList;
}
return similarityScore;
}
2024-12-08 08:07:27 +00:00
int main() {
std::string inputFile = "../input.txt";
std::vector<std::string> input = readTextFromFile(inputFile);
auto [leftArray, rightArray] = parseInputVector(input);
int result = solve(leftArray, rightArray);
std::cout << "The result is: " << result << std::endl;
2024-12-08 08:12:26 +00:00
int similarityScore = calcSimilarity(leftArray, rightArray);
std::cout << "The similarity is " << similarityScore << std::endl;
2024-12-08 08:07:27 +00:00
return 0;
}