diff --git a/day1/day.cpp b/day1/day.cpp index 48e2d11..5236de4 100644 --- a/day1/day.cpp +++ b/day1/day.cpp @@ -1,78 +1,61 @@ +#include "../include/useful_funcs.h" #include -#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; -} +#include 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])); - } + std::ifstream inputfile("input"); + std::string input; - // Close the file stream once all lines have been - // read. - inputfile.close(); + 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])); } - 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"; - + // 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 index 7f73994..c4674bc 100644 --- a/day1/second.cpp +++ b/day1/second.cpp @@ -1,66 +1,48 @@ +#include "../include/useful_funcs.h" #include -#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; -} +#include 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])); - } + std::ifstream inputfile("input"); + std::string input; - // Close the file stream once all lines have been - // read. - inputfile.close(); + 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])); } - 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"; - + // 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 index 1e2fd0d..94213eb 100644 --- a/day2/day.cpp +++ b/day2/day.cpp @@ -1,3 +1,4 @@ +#include "../include/useful_funcs.h" #include #include #include @@ -7,18 +8,6 @@ #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; @@ -33,9 +22,7 @@ int main() { num_s = splitString(line); num.clear(); - // Convert strings to integers - std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), - [](const std::string &s) { return std::stoi(s); }); + CastStringVectorToIntVector(num_s, num); // decreasing bool safe = true; diff --git a/day2/second.cpp b/day2/second.cpp index 8659665..d5483f9 100644 --- a/day2/second.cpp +++ b/day2/second.cpp @@ -1,3 +1,4 @@ +#include "../include/useful_funcs.h" #include #include #include @@ -7,18 +8,6 @@ #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; -} - bool isSafe(const std::vector &num, bool checkIncreasing) { for (size_t i = 1; i < num.size(); ++i) { int diff = checkIncreasing ? (num[i] - num[i - 1]) : (num[i - 1] - num[i]); @@ -61,9 +50,7 @@ int main() { num_s = splitString(line); num.clear(); - // Convert strings to integers - std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), - [](const std::string &s) { return std::stoi(s); }); + CastStringVectorToIntVector(num_s, num); // Check safety with Problem Dampener support if (isSafeWithDampener(num)) { diff --git a/day4/day.cpp b/day4/day.cpp index 578ec7d..746a924 100644 --- a/day4/day.cpp +++ b/day4/day.cpp @@ -1,4 +1,5 @@ +#include "../include/useful_funcs.h" #include #include #include @@ -6,27 +7,6 @@ #include #include -bool AreWeInBounds(int i, int j, std::vector> &wordsearch) { - if (i < 0 || j < 0) { - return false; - } - if (i >= wordsearch.size() || j >= wordsearch[i].size()) { - return false; - } - return true; -} - -int conditional_operation(int a, int b, char op) { - switch (op) { - case '+': - return a + b; - case '-': - return a - b; - default: - return a; - } -} - int Check(std::vector> &wordsearch, int i, int j) { int xmases = 0; char op_i; diff --git a/day4/second.cpp b/day4/second.cpp index e6d1758..70d19e8 100644 --- a/day4/second.cpp +++ b/day4/second.cpp @@ -1,36 +1,15 @@ +#include "../include/useful_funcs.h" #include #include #include #include #include #include - #define RANGE 1 int res; -bool AreWeInBounds(int i, int j, std::vector> &wordsearch) { - if (i < 0 || j < 0) { - return false; - } - if (i >= wordsearch.size() || j >= wordsearch[i].size()) { - return false; - } - return true; -} - -int conditional_operation(int a, int b, char op) { - switch (op) { - case '+': - return a + b; - case '-': - return a - b; - default: - return a; - } -} - int Check(std::vector> &wordsearch, int i, int j) { int xmases = 0; char op_i; diff --git a/day5/day.cpp b/day5/day.cpp index 53e71b7..d13f2d8 100644 --- a/day5/day.cpp +++ b/day5/day.cpp @@ -1,3 +1,4 @@ +#include "../include/useful_funcs.h" #include #include #include @@ -8,24 +9,6 @@ #include #include -std::vector splitString(const std::string &str) { - std::vector words; - std::string word; - std::istringstream iss(str); - - // Use std::getline with ',' as the delimiter - while (std::getline(iss, word, ',')) { - // Trim whitespace from the beginning and end of the word - size_t start = word.find_first_not_of(" \t"); - size_t end = word.find_last_not_of(" \t"); - if (start != std::string::npos && end != std::string::npos) { - words.push_back(word.substr(start, end - start + 1)); - } - } - - return words; -} - void MoveElementByIndex(std::vector &vec, int index, int newIndex) { if (index == newIndex) { return; @@ -64,11 +47,9 @@ int main() { sscanf(line.c_str(), "%d|%d", &RuleTmp[0], &RuleTmp[1]); rules.push_back(RuleTmp); } else { - lineSplited = splitString(line); - // transform strings to ints - std::transform(lineSplited.begin(), lineSplited.end(), - std::back_inserter(lineSplitedInt), - [](const std::string &s) { return std::stoi(s); }); + lineSplited = splitStringByChar(line, ','); + + CastStringVectorToIntVector(lineSplited, lineSplitedInt); for (int i = 0; i < rules.size(); i++) { for (int j = 0; j < lineSplitedInt.size(); j++) { diff --git a/day5/second.cpp b/day5/second.cpp index 79d9b5f..6b99fe1 100644 --- a/day5/second.cpp +++ b/day5/second.cpp @@ -1,3 +1,4 @@ +#include "../include/useful_funcs.h" #include #include #include @@ -8,24 +9,6 @@ #include #include -std::vector splitString(const std::string &str) { - std::vector words; - std::string word; - std::istringstream iss(str); - - // Use std::getline with ',' as the delimiter - while (std::getline(iss, word, ',')) { - // Trim whitespace from the beginning and end of the word - size_t start = word.find_first_not_of(" \t"); - size_t end = word.find_last_not_of(" \t"); - if (start != std::string::npos && end != std::string::npos) { - words.push_back(word.substr(start, end - start + 1)); - } - } - - return words; -} - void MoveElementByIndex(std::vector &vec, int index, int newIndex) { if (index == newIndex) { return; @@ -65,10 +48,8 @@ int main() { rules.push_back(RuleTmp); } else { lineSplited = splitString(line); - // transform strings to ints - std::transform(lineSplited.begin(), lineSplited.end(), - std::back_inserter(lineSplitedInt), - [](const std::string &s) { return std::stoi(s); }); + + CastStringVectorToIntVector(lineSplited, lineSplitedInt); for (int i = 0; i < rules.size(); i++) { for (int j = 0; j < lineSplitedInt.size(); j++) { diff --git a/day6/day.cpp b/day6/day.cpp index 3f6c2be..a300061 100644 --- a/day6/day.cpp +++ b/day6/day.cpp @@ -1,28 +1,8 @@ +#include "../include/useful_funcs.h" #include #include #include -bool AreWeInBounds(int x, int y, std::vector> &map) { - if (x < 0 || y < 0) { - return false; - } - if (x >= map.size() || y >= map[x].size()) { - return false; - } - return true; -} - -int conditional_operation(int a, int b, char op) { - switch (op) { - case '+': - return a + b; - case '-': - return a - b; - default: - return a; - } -} - int main() { std::ifstream inputfile("input"); std::string input; diff --git a/day6/second.cpp b/day6/second.cpp index 7b0e8aa..84ef77d 100644 --- a/day6/second.cpp +++ b/day6/second.cpp @@ -1,29 +1,9 @@ +#include "../include/useful_funcs.h" #include #include #include #include -bool AreWeInBounds(int x, int y, std::vector> &map) { - if (x < 0 || y < 0) { - return false; - } - if (x >= map.size() || y >= map[x].size()) { - return false; - } - return true; -} - -int conditional_operation(int a, int b, char op) { - switch (op) { - case '+': - return a + b; - case '-': - return a - b; - default: - return a; - } -} - bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY, std::vector> map) { char op_x; diff --git a/day7/day.cpp b/day7/day.cpp new file mode 100644 index 0000000..aa13456 --- /dev/null +++ b/day7/day.cpp @@ -0,0 +1,37 @@ +#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; + + int res = 0; + if (!inputfile.is_open()) { + std::cerr << "Could not open the file" << std::endl; + return ENOENT; + } + std::string line; + std::vector lineSplited; + int EqRes; + + while (std::getline(inputfile, line)) { + lineSplited = splitString(line); + sscanf(lineSplited[0].c_str(), "%d:", &EqRes); + lineSplited.erase(lineSplited.begin()); + } +} \ No newline at end of file diff --git a/include/useful_funcs.h b/include/useful_funcs.h new file mode 100644 index 0000000..f192cce --- /dev/null +++ b/include/useful_funcs.h @@ -0,0 +1,62 @@ +#include +#include +#include + +inline 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; +} + +inline void CastStringVectorToIntVector(const std::vector &strVec, + std::vector &intVec) { + std::transform(strVec.begin(), strVec.end(), std::back_inserter(intVec), + [](const std::string &s) { return std::stoi(s); }); +} + +inline bool AreWeInBounds(int x, int y, + std::vector> &TwoDCharVec) { + if (x < 0 || y < 0) { + return false; + } + if (x >= TwoDCharVec.size() || y >= TwoDCharVec[x].size()) { + return false; + } + return true; +} + +inline int conditional_operation(int a, int b, char op) { + switch (op) { + case '+': + return a + b; + case '-': + return a - b; + default: + return a; + } +} + +inline std::vector splitStringByChar(const std::string &str, + char c) { + std::vector words; + std::string word; + std::istringstream iss(str); + + // Use std::getline with ',' as the delimiter + while (std::getline(iss, word, c)) { + // Trim whitespace from the beginning and end of the word + size_t start = word.find_first_not_of(" \t"); + size_t end = word.find_last_not_of(" \t"); + if (start != std::string::npos && end != std::string::npos) { + words.push_back(word.substr(start, end - start + 1)); + } + } + + return words; +}