refactoring

This commit is contained in:
PoliEcho 2024-12-14 22:15:16 +01:00
parent e7a7ab3cdf
commit dd628868f1
12 changed files with 203 additions and 284 deletions

View File

@ -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";
}

View File

@ -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";
}

View File

@ -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); });
CastStringVectorToIntVector(num_s, num);
// decreasing
bool safe = true;

View File

@ -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); });
CastStringVectorToIntVector(num_s, num);
// Check safety with Problem Dampener support
if (isSafeWithDampener(num)) {

View File

@ -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;

View File

@ -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;

View File

@ -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, ',');
CastStringVectorToIntVector(lineSplited, lineSplitedInt);
for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) {

View File

@ -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); });
CastStringVectorToIntVector(lineSplited, lineSplitedInt);
for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) {

View File

@ -1,28 +1,8 @@
#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;
}
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;

View File

@ -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;

37
day7/day.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#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;
int 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;
int EqRes;
while (std::getline(inputfile, line)) {
lineSplited = splitString(line);
sscanf(lineSplited[0].c_str(), "%d:", &EqRes);
lineSplited.erase(lineSplited.begin());
}
}

62
include/useful_funcs.h Normal file
View File

@ -0,0 +1,62 @@
#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 CastStringVectorToIntVector(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;
}