Compare commits

...

3 Commits

Author SHA1 Message Date
97cb3b6639 day7 1 and 2 done 2024-12-15 12:57:12 +01:00
ff0b63dbdd make more clear function name 2024-12-14 22:16:23 +01:00
dd628868f1 refactoring 2024-12-14 22:15:16 +01:00
14 changed files with 408 additions and 278 deletions

View File

@ -1,78 +1,61 @@
#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");
std::string input;
std::ifstream inputfile("input");
std::string input;
std::vector<int> l_sl;
std::vector<int> r_sl;
if (inputfile.is_open()) {
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> 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::vector<int> l_sl;
std::vector<int> r_sl;
if (inputfile.is_open()) {
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> 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]));
}
// Close the file stream once all lines have been
// read.
inputfile.close();
// 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];
}
}
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;
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];
}
}
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";
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";
}

View File

@ -1,66 +1,48 @@
#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");
std::string input;
std::ifstream inputfile("input");
std::string input;
std::vector<int> l_sl;
std::vector<int> r_sl;
if (inputfile.is_open()) {
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> 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]));
}
// Close the file stream once all lines have been
// read.
inputfile.close();
std::vector<int> l_sl;
std::vector<int> r_sl;
if (inputfile.is_open()) {
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> 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";
}

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

View File

@ -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;
// 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;
}
if (x >= map.size() || y >= map[x].size()) {
return false;
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]);
}
}
return true;
}
int conditional_operation(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
default:
return a;
}
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");

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;

90
day7/day.cpp Normal file
View 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
View 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
View 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
View 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;
}