day7 1 and 2 done
This commit is contained in:
+65
-12
@@ -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";
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
Reference in New Issue
Block a user