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 <algorithm>
#include <iostream> #include <errno.h>
#include <string> #include <fstream>
#include <vector>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector>
#include <string> #include <string>
#include <fstream> #include <vector>
#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;
}
int main() { int main() {
std::ifstream inputfile("input"); std::ifstream inputfile("input");
@ -41,15 +27,14 @@ int main() {
// Close the file stream once all lines have been // Close the file stream once all lines have been
// read. // read.
inputfile.close(); inputfile.close();
} } else {
else {
// Print an error message to the standard error // Print an error message to the standard error
// stream if the file cannot be opened. // stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl; std::cerr << "Unable to open file!" << std::endl;
return ENOENT; return ENOENT;
} }
std::cout << "right: \n"; std::cout << "right: \n";
for (int i = 0; i< r_sl.size(); i++) { for (int i = 0; i < r_sl.size(); i++) {
std::cout << i << ": " << r_sl[i] << "\n"; std::cout << i << ": " << r_sl[i] << "\n";
} }
int min_l; int min_l;
@ -57,22 +42,20 @@ int main() {
int res = 0; int res = 0;
while (l_sl.size() != 0) { while (l_sl.size() != 0) {
min_l = l_sl[0]; min_l = l_sl[0];
for(int i = 0; i < l_sl.size(); i++) { for (int i = 0; i < l_sl.size(); i++) {
if(l_sl[i] < min_l) { if (l_sl[i] < min_l) {
min_l = l_sl[i]; min_l = l_sl[i];
} }
} }
min_r = r_sl[0]; min_r = r_sl[0];
for(int i = 0; i < r_sl.size(); i++) { for (int i = 0; i < r_sl.size(); i++) {
if(r_sl[i] < min_r) { if (r_sl[i] < min_r) {
min_r = r_sl[i]; min_r = r_sl[i];
} }
} }
l_sl.erase(find(l_sl.begin(), l_sl.end(), min_l)); l_sl.erase(find(l_sl.begin(), l_sl.end(), min_l));
r_sl.erase(find(r_sl.begin(), r_sl.end(), min_r)); r_sl.erase(find(r_sl.begin(), r_sl.end(), min_r));
res+= std::abs(min_l - min_r); res += std::abs(min_l - min_r);
} }
std::cout << "\n\n" << res << "\n\n"; std::cout << "\n\n" << res << "\n\n";
} }

View File

@ -1,25 +1,11 @@
#include "../include/useful_funcs.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <errno.h>
#include <string> #include <fstream>
#include <vector>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector>
#include <string> #include <string>
#include <fstream> #include <vector>
#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;
}
int main() { int main() {
std::ifstream inputfile("input"); std::ifstream inputfile("input");
@ -41,26 +27,22 @@ int main() {
// Close the file stream once all lines have been // Close the file stream once all lines have been
// read. // read.
inputfile.close(); inputfile.close();
} } else {
else {
// Print an error message to the standard error // Print an error message to the standard error
// stream if the file cannot be opened. // stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl; std::cerr << "Unable to open file!" << std::endl;
return ENOENT; return ENOENT;
} }
std::cout << "right: \n"; std::cout << "right: \n";
for (int i = 0; i< r_sl.size(); i++) { for (int i = 0; i < r_sl.size(); i++) {
std::cout << i << ": " << r_sl[i] << "\n"; std::cout << i << ": " << r_sl[i] << "\n";
} }
int min_l; int min_l;
int min_r; int min_r;
int res = 0; int res = 0;
for (int i = 0; i < l_sl.size(); i++ ) { for (int i = 0; i < l_sl.size(); i++) {
res += l_sl[i] * std::count(r_sl.begin(), r_sl.end(), l_sl[i]);
res+= l_sl[i] * std::count(r_sl.begin(), r_sl.end(), l_sl[i]);
} }
std::cout << "\n\n" << res << "\n\n"; std::cout << "\n\n" << res << "\n\n";
} }

View File

@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <errno.h> #include <errno.h>
@ -7,18 +8,6 @@
#include <string> #include <string>
#include <vector> #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() { int main() {
std::ifstream inputfile("input"); std::ifstream inputfile("input");
std::string input; std::string input;
@ -33,9 +22,7 @@ int main() {
num_s = splitString(line); num_s = splitString(line);
num.clear(); num.clear();
// Convert strings to integers CastStringVectorToIntVector(num_s, num);
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
[](const std::string &s) { return std::stoi(s); });
// decreasing // decreasing
bool safe = true; bool safe = true;

View File

@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <errno.h> #include <errno.h>
@ -7,18 +8,6 @@
#include <string> #include <string>
#include <vector> #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) { bool isSafe(const std::vector<int> &num, bool checkIncreasing) {
for (size_t i = 1; i < num.size(); ++i) { for (size_t i = 1; i < num.size(); ++i) {
int diff = checkIncreasing ? (num[i] - num[i - 1]) : (num[i - 1] - num[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_s = splitString(line);
num.clear(); num.clear();
// Convert strings to integers CastStringVectorToIntVector(num_s, num);
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
[](const std::string &s) { return std::stoi(s); });
// Check safety with Problem Dampener support // Check safety with Problem Dampener support
if (isSafeWithDampener(num)) { if (isSafeWithDampener(num)) {

View File

@ -1,4 +1,5 @@
#include "../include/useful_funcs.h"
#include <asm-generic/errno.h> #include <asm-generic/errno.h>
#include <cstdlib> #include <cstdlib>
#include <errno.h> #include <errno.h>
@ -6,27 +7,6 @@
#include <iostream> #include <iostream>
#include <vector> #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 Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
int xmases = 0; int xmases = 0;
char op_i; char op_i;

View File

@ -1,36 +1,15 @@
#include "../include/useful_funcs.h"
#include <asm-generic/errno.h> #include <asm-generic/errno.h>
#include <cstdlib> #include <cstdlib>
#include <errno.h> #include <errno.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#define RANGE 1 #define RANGE 1
int res; 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 Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
int xmases = 0; int xmases = 0;
char op_i; char op_i;

View File

@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <codecvt> #include <codecvt>
@ -8,24 +9,6 @@
#include <string> #include <string>
#include <vector> #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) { void MoveElementByIndex(std::vector<int> &vec, int index, int newIndex) {
if (index == newIndex) { if (index == newIndex) {
return; return;
@ -64,11 +47,9 @@ int main() {
sscanf(line.c_str(), "%d|%d", &RuleTmp[0], &RuleTmp[1]); sscanf(line.c_str(), "%d|%d", &RuleTmp[0], &RuleTmp[1]);
rules.push_back(RuleTmp); rules.push_back(RuleTmp);
} else { } else {
lineSplited = splitString(line); lineSplited = splitStringByChar(line, ',');
// transform strings to ints
std::transform(lineSplited.begin(), lineSplited.end(), CastStringVectorToIntVector(lineSplited, lineSplitedInt);
std::back_inserter(lineSplitedInt),
[](const std::string &s) { return std::stoi(s); });
for (int i = 0; i < rules.size(); i++) { for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) { for (int j = 0; j < lineSplitedInt.size(); j++) {

View File

@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <codecvt> #include <codecvt>
@ -8,24 +9,6 @@
#include <string> #include <string>
#include <vector> #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) { void MoveElementByIndex(std::vector<int> &vec, int index, int newIndex) {
if (index == newIndex) { if (index == newIndex) {
return; return;
@ -65,10 +48,8 @@ int main() {
rules.push_back(RuleTmp); rules.push_back(RuleTmp);
} else { } else {
lineSplited = splitString(line); lineSplited = splitString(line);
// transform strings to ints
std::transform(lineSplited.begin(), lineSplited.end(), CastStringVectorToIntVector(lineSplited, lineSplitedInt);
std::back_inserter(lineSplitedInt),
[](const std::string &s) { return std::stoi(s); });
for (int i = 0; i < rules.size(); i++) { for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) { for (int j = 0; j < lineSplitedInt.size(); j++) {

View File

@ -1,28 +1,8 @@
#include "../include/useful_funcs.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <vector> #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() { int main() {
std::ifstream inputfile("input"); std::ifstream inputfile("input");
std::string input; std::string input;

View File

@ -1,29 +1,9 @@
#include "../include/useful_funcs.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <vector> #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, bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
std::vector<std::vector<char>> map) { std::vector<std::vector<char>> map) {
char op_x; 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;
}