Compare commits
3 Commits
e7a7ab3cdf
...
97cb3b6639
| Author | SHA1 | Date | |
|---|---|---|---|
| 97cb3b6639 | |||
| ff0b63dbdd | |||
| dd628868f1 |
27
day1/day.cpp
27
day1/day.cpp
@ -1,25 +1,11 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <errno.h>
|
||||
|
||||
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;
|
||||
}
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
@ -41,8 +27,7 @@ int main() {
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
@ -73,6 +58,4 @@ int main() {
|
||||
res += std::abs(min_l - min_r);
|
||||
}
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
|
||||
|
||||
}
|
||||
@ -1,25 +1,11 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <errno.h>
|
||||
|
||||
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;
|
||||
}
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
@ -41,8 +27,7 @@ int main() {
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
@ -57,10 +42,7 @@ int main() {
|
||||
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";
|
||||
|
||||
|
||||
}
|
||||
17
day2/day.cpp
17
day2/day.cpp
@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <errno.h>
|
||||
@ -7,18 +8,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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); });
|
||||
ConvertStringVectorToIntVector(num_s, num);
|
||||
|
||||
// decreasing
|
||||
bool safe = true;
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <errno.h>
|
||||
@ -7,18 +8,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool isSafe(const std::vector<int> &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); });
|
||||
ConvertStringVectorToIntVector(num_s, num);
|
||||
|
||||
// Check safety with Problem Dampener support
|
||||
if (isSafeWithDampener(num)) {
|
||||
|
||||
22
day4/day.cpp
22
day4/day.cpp
@ -1,4 +1,5 @@
|
||||
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <asm-generic/errno.h>
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
@ -6,27 +7,6 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
bool AreWeInBounds(int i, int j, std::vector<std::vector<char>> &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<std::vector<char>> &wordsearch, int i, int j) {
|
||||
int xmases = 0;
|
||||
char op_i;
|
||||
|
||||
@ -1,36 +1,15 @@
|
||||
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <asm-generic/errno.h>
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define RANGE 1
|
||||
|
||||
int res;
|
||||
|
||||
bool AreWeInBounds(int i, int j, std::vector<std::vector<char>> &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<std::vector<char>> &wordsearch, int i, int j) {
|
||||
int xmases = 0;
|
||||
char op_i;
|
||||
|
||||
27
day5/day.cpp
27
day5/day.cpp
@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <codecvt>
|
||||
@ -8,24 +9,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::vector<std::string> 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<int> &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, ',');
|
||||
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
for (int i = 0; i < rules.size(); i++) {
|
||||
for (int j = 0; j < lineSplitedInt.size(); j++) {
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <codecvt>
|
||||
@ -8,24 +9,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::vector<std::string> 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<int> &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); });
|
||||
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
for (int i = 0; i < rules.size(); i++) {
|
||||
for (int j = 0; j < lineSplitedInt.size(); j++) {
|
||||
|
||||
41
day6/day.cpp
41
day6/day.cpp
@ -1,27 +1,36 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
|
||||
if (x < 0 || y < 0) {
|
||||
return false;
|
||||
}
|
||||
if (x >= map.size() || y >= map[x].size()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
// this is defenitely my code
|
||||
void heapPermutation(std::vector<char> &a, int size,
|
||||
std::vector<std::vector<char>> &results) {
|
||||
if (size == 1) {
|
||||
results.push_back(a); // Store the current permutation
|
||||
return;
|
||||
}
|
||||
|
||||
int conditional_operation(int a, int b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
default:
|
||||
return a;
|
||||
for (int i = 0; i < size; i++) {
|
||||
heapPermutation(a, size - 1, results); // Recur with reduced size
|
||||
|
||||
// If size is odd, swap the first element with the last element
|
||||
if (size % 2 == 1) {
|
||||
std::swap(a[0], a[size - 1]);
|
||||
} else { // If size is even, swap the current element with the last element
|
||||
std::swap(a[i], a[size - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>>
|
||||
generatePermutations(const std::vector<char> &input) {
|
||||
std::vector<char> chars = input; // Create a mutable copy of input
|
||||
std::vector<std::vector<char>> results; // To store all permutations
|
||||
heapPermutation(chars, chars.size(), results); // Generate permutations
|
||||
return results; // Return all permutations
|
||||
}
|
||||
// end of defenitely my code
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
|
||||
@ -1,29 +1,9 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &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<std::vector<char>> map) {
|
||||
char op_x;
|
||||
|
||||
90
day7/day.cpp
Normal file
90
day7/day.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdio>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
// this is defenitely my code
|
||||
void generatePermutation(const std::vector<char> &chars,
|
||||
std::vector<char> current, int length,
|
||||
std::vector<std::vector<char>> &results) {
|
||||
// Base case: if the current vector reaches the desired length
|
||||
if (current.size() == length) {
|
||||
results.push_back(current); // Store the current vector
|
||||
return;
|
||||
}
|
||||
|
||||
// Recursive case: add each character and recurse
|
||||
for (char c : chars) {
|
||||
current.push_back(c); // Append character
|
||||
generatePermutation(chars, current, length, results); // Recurse
|
||||
current.pop_back(); // Backtrack to explore other combinations
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>>
|
||||
generateAllPermutations(const std::vector<char> &chars, int length) {
|
||||
std::vector<std::vector<char>> results; // To store all generated vectors
|
||||
generatePermutation(chars, {}, length,
|
||||
results); // Start the recursion with an empty vector
|
||||
return results; // Return all generated vectors
|
||||
}
|
||||
// end of defenitely my code
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::string line;
|
||||
std::vector<std::string> lineSplited;
|
||||
std::vector<int> lineSplitedInt;
|
||||
std::vector<std::vector<char>> AllPossiblePermutations;
|
||||
std::vector<unsigned long long> PossibleResults;
|
||||
const std::vector<char> operators = {'+', '*'};
|
||||
unsigned long long EqRes;
|
||||
unsigned long long TmpRes;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
lineSplited.clear();
|
||||
lineSplitedInt.clear();
|
||||
AllPossiblePermutations.clear();
|
||||
PossibleResults.clear();
|
||||
|
||||
lineSplited = splitString(line);
|
||||
sscanf(lineSplited[0].c_str(), "%llu:", &EqRes);
|
||||
lineSplited.erase(lineSplited.begin());
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
AllPossiblePermutations =
|
||||
generateAllPermutations(operators, lineSplitedInt.size() - 1);
|
||||
|
||||
for (std::vector<char> &permutation : AllPossiblePermutations) {
|
||||
TmpRes = lineSplitedInt[0];
|
||||
// cicle from one since TmpRes is set to the first number
|
||||
for (int i = 1; i < lineSplitedInt.size(); i++) {
|
||||
TmpRes = conditional_operation_ull(
|
||||
TmpRes, static_cast<unsigned long long>(lineSplitedInt[i]),
|
||||
permutation[i - 1]);
|
||||
}
|
||||
PossibleResults.push_back(TmpRes);
|
||||
}
|
||||
for (unsigned long long &result : PossibleResults) {
|
||||
if (result == EqRes) {
|
||||
res += EqRes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// I added colors, because I can
|
||||
std::cout << CYAN "Sum of valid equasions is: " << YELLOW << res
|
||||
<< RESET "\n";
|
||||
}
|
||||
89
day7/second.cpp
Normal file
89
day7/second.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdio>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
// this is defenitely my code
|
||||
void generatePermutation(const std::vector<char> &chars,
|
||||
std::vector<char> current, int length,
|
||||
std::vector<std::vector<char>> &results) {
|
||||
// Base case: if the current vector reaches the desired length
|
||||
if (current.size() == length) {
|
||||
results.push_back(current); // Store the current vector
|
||||
return;
|
||||
}
|
||||
|
||||
// Recursive case: add each character and recurse
|
||||
for (char c : chars) {
|
||||
current.push_back(c); // Append character
|
||||
generatePermutation(chars, current, length, results); // Recurse
|
||||
current.pop_back(); // Backtrack to explore other combinations
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>>
|
||||
generateAllPermutations(const std::vector<char> &chars, int length) {
|
||||
std::vector<std::vector<char>> results; // To store all generated vectors
|
||||
generatePermutation(chars, {}, length,
|
||||
results); // Start the recursion with an empty vector
|
||||
return results; // Return all generated vectors
|
||||
}
|
||||
// end of defenitely my code
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::string line;
|
||||
std::vector<std::string> lineSplited;
|
||||
std::vector<int> lineSplitedInt;
|
||||
std::vector<std::vector<char>> AllPossiblePermutations;
|
||||
std::vector<unsigned long long> PossibleResults;
|
||||
const std::vector<char> operators = {'+', '*', 'c'};
|
||||
unsigned long long EqRes;
|
||||
unsigned long long TmpRes;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
lineSplited.clear();
|
||||
lineSplitedInt.clear();
|
||||
AllPossiblePermutations.clear();
|
||||
PossibleResults.clear();
|
||||
|
||||
lineSplited = splitString(line);
|
||||
sscanf(lineSplited[0].c_str(), "%llu:", &EqRes);
|
||||
lineSplited.erase(lineSplited.begin());
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
AllPossiblePermutations =
|
||||
generateAllPermutations(operators, lineSplitedInt.size() - 1);
|
||||
|
||||
for (std::vector<char> &permutation : AllPossiblePermutations) {
|
||||
TmpRes = lineSplitedInt[0];
|
||||
// cicle from one since TmpRes is set to the first number
|
||||
for (int i = 1; i < lineSplitedInt.size(); i++) {
|
||||
TmpRes = conditional_operation_ull(
|
||||
TmpRes, static_cast<unsigned long long>(lineSplitedInt[i]),
|
||||
permutation[i - 1]);
|
||||
}
|
||||
PossibleResults.push_back(TmpRes);
|
||||
}
|
||||
for (unsigned long long &result : PossibleResults) {
|
||||
if (result == EqRes) {
|
||||
res += EqRes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << CYAN "Sum of valid equasions is: " << YELLOW << res
|
||||
<< RESET "\n";
|
||||
}
|
||||
17
include/color.h
Normal file
17
include/color.h
Normal file
@ -0,0 +1,17 @@
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m" /* Black */
|
||||
#define RED "\033[31m" /* Red */
|
||||
#define GREEN "\033[32m" /* Green */
|
||||
#define YELLOW "\033[33m" /* Yellow */
|
||||
#define BLUE "\033[34m" /* Blue */
|
||||
#define MAGENTA "\033[35m" /* Magenta */
|
||||
#define CYAN "\033[36m" /* Cyan */
|
||||
#define WHITE "\033[37m" /* White */
|
||||
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
|
||||
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
|
||||
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
|
||||
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
||||
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
|
||||
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
|
||||
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
|
||||
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
||||
85
include/useful_funcs.h
Normal file
85
include/useful_funcs.h
Normal file
@ -0,0 +1,85 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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); });
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int conditional_operation(int a, int b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
case '*':
|
||||
return a * b;
|
||||
case '/':
|
||||
return a / b;
|
||||
case 'c':
|
||||
return std::stoi(std::to_string(a) + std::to_string(b));
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long long conditional_operation_ull(unsigned long long a,
|
||||
unsigned long long b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
case '*':
|
||||
return a * b;
|
||||
case '/':
|
||||
return a / b;
|
||||
case 'c':
|
||||
return std::stoull(std::to_string(a) + std::to_string(b));
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user