Day 1: Part 1
This commit is contained in:
parent
06b0a112a7
commit
939cf8b74a
6
Aufgabe1/CMakeLists.txt
Normal file
6
Aufgabe1/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
project(Aufgabe1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(Aufgabe1 main.cpp)
|
1000
Aufgabe1/input.txt
Normal file
1000
Aufgabe1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
Aufgabe1/input_example.txt
Normal file
6
Aufgabe1/input_example.txt
Normal file
@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
128
Aufgabe1/main.cpp
Normal file
128
Aufgabe1/main.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user