day7 1 and 2 done

This commit is contained in:
PoliEcho 2024-12-15 12:57:12 +01:00
parent ff0b63dbdd
commit 97cb3b6639
5 changed files with 231 additions and 21 deletions

View File

@ -3,6 +3,35 @@
#include <iostream>
#include <vector>
// 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;
}
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");
std::string input;

View File

@ -1,37 +1,90 @@
#include "../include/color.h"
#include "../include/useful_funcs.h"
#include <cstdio>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <sys/types.h>
#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);
// 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;
}
return words;
// 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;
int res = 0;
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;
int EqRes;
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(), "%d:", &EqRes);
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 */

View File

@ -1,8 +1,9 @@
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
inline std::vector<std::string> splitString(const std::string &str) {
std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
@ -14,15 +15,13 @@ inline std::vector<std::string> splitString(const std::string &str) {
return words;
}
inline void
ConvertStringVectorToIntVector(const std::vector<std::string> &strVec,
std::vector<int> &intVec) {
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) {
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &TwoDCharVec) {
if (x < 0 || y < 0) {
return false;
}
@ -32,19 +31,42 @@ inline bool AreWeInBounds(int x, int y,
return true;
}
inline int conditional_operation(int a, int b, char op) {
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;
}
}
inline std::vector<std::string> splitStringByChar(const std::string &str,
char c) {
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);