commit 96775c800bad2fc29dbc2e5dbc80e2a4cffe00d0 Author: PoliEcho Date: Thu Dec 12 16:32:37 2024 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/day1/day.cpp b/day1/day.cpp new file mode 100644 index 0000000..48e2d11 --- /dev/null +++ b/day1/day.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector splitString(const std::string& str) { + std::istringstream iss(str); + std::vector words; + std::string word; + + while (iss >> word) { + words.push_back(word); + } + + return words; +} + +int main() { + std::ifstream inputfile("input"); + std::string input; + + std::vector l_sl; + std::vector r_sl; + if (inputfile.is_open()) { + // Read each line from the file and store it in the + // 'line' variable. + std::string line; + std::vector num_s; + while (std::getline(inputfile, line)) { + num_s = splitString(line); + l_sl.push_back(std::stoi(num_s[0])); + r_sl.push_back(std::stoi(num_s[1])); + } + + // Close the file stream once all lines have been + // read. + inputfile.close(); + } + else { + // Print an error message to the standard error + // stream if the file cannot be opened. + std::cerr << "Unable to open file!" << std::endl; + return ENOENT; + } + std::cout << "right: \n"; + for (int i = 0; i< r_sl.size(); i++) { + std::cout << i << ": " << r_sl[i] << "\n"; + } + int min_l; + int min_r; + int res = 0; + while (l_sl.size() != 0) { + min_l = l_sl[0]; + for(int i = 0; i < l_sl.size(); i++) { + if(l_sl[i] < min_l) { + min_l = l_sl[i]; + } + } + min_r = r_sl[0]; + for(int i = 0; i < r_sl.size(); i++) { + if(r_sl[i] < min_r) { + min_r = r_sl[i]; + } + } + l_sl.erase(find(l_sl.begin(), l_sl.end(), min_l)); + r_sl.erase(find(r_sl.begin(), r_sl.end(), min_r)); + res+= std::abs(min_l - min_r); + } + std::cout << "\n\n" << res << "\n\n"; + + +} \ No newline at end of file diff --git a/day1/second.cpp b/day1/second.cpp new file mode 100644 index 0000000..7f73994 --- /dev/null +++ b/day1/second.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector splitString(const std::string& str) { + std::istringstream iss(str); + std::vector words; + std::string word; + + while (iss >> word) { + words.push_back(word); + } + + return words; +} + +int main() { + std::ifstream inputfile("input"); + std::string input; + + std::vector l_sl; + std::vector r_sl; + if (inputfile.is_open()) { + // Read each line from the file and store it in the + // 'line' variable. + std::string line; + std::vector num_s; + while (std::getline(inputfile, line)) { + num_s = splitString(line); + l_sl.push_back(std::stoi(num_s[0])); + r_sl.push_back(std::stoi(num_s[1])); + } + + // Close the file stream once all lines have been + // read. + inputfile.close(); + } + else { + // Print an error message to the standard error + // stream if the file cannot be opened. + std::cerr << "Unable to open file!" << std::endl; + return ENOENT; + } + std::cout << "right: \n"; + for (int i = 0; i< r_sl.size(); i++) { + std::cout << i << ": " << r_sl[i] << "\n"; + } + int min_l; + int min_r; + int res = 0; + for (int i = 0; i < l_sl.size(); i++ ) { + + + res+= l_sl[i] * std::count(r_sl.begin(), r_sl.end(), l_sl[i]); + } + std::cout << "\n\n" << res << "\n\n"; + + +} \ No newline at end of file diff --git a/day2/day.cpp b/day2/day.cpp new file mode 100644 index 0000000..2288c35 --- /dev/null +++ b/day2/day.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector splitString(const std::string& str) { + std::istringstream iss(str); + std::vector words; + std::string word; + + while (iss >> word) { + words.push_back(word); + } + + return words; +} + +int main() { + std::ifstream inputfile("example"); + std::string input; + + int res = 0; + if (inputfile.is_open()) { + + // Read each line from the file and store it in the + // 'line' variable. + std::string line; + std::vector num_s; + std::vector num; + + while (std::getline(inputfile, line)) { + num_s = splitString(line); + num.clear(); + std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) { + return std::stoi(s); + }); + bool safe = true; + int old_n = -1; + for (int n : num) { + if(old_n == -1) { + old_n = n; + continue; + } + if ((old_n - n) > 2 || (old_n - n) > 0) { + safe = false; + } + old_n = n; + } + if (safe) { + res++; + continue; + } + safe = true; + old_n = -1; + for (int n : num) { + if(old_n == -1) { + old_n = n; + continue; + } + if ((old_n - n) <= -3 && (old_n - n) < 0) { + safe = false; + } + old_n = n; + } + if (safe) { + res++; + } + } + + + + // Close the file stream once all lines have been + // read. + inputfile.close(); + } + else { + // Print an error message to the standard error + // stream if the file cannot be opened. + std::cerr << "Unable to open file!" << std::endl; + return ENOENT; + } + + + + std::cout << "\n\n" << res << "\n\n"; + + return 0; +} \ No newline at end of file