advent-of-code-2024/include/useful_funcs.h

64 lines
1.6 KiB
C++

#include <algorithm>
#include <sstream>
#include <vector>
inline std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
while (iss >> word) {
words.push_back(word);
}
return words;
}
inline void
ConvertStringVectorToIntVector(const std::vector<std::string> &strVec,
std::vector<int> &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<std::vector<char>> &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<std::string> splitStringByChar(const std::string &str,
char c) {
std::vector<std::string> 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;
}