89 lines
2.9 KiB
C++
89 lines
2.9 KiB
C++
#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 = condop_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";
|
|
} |