-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaySevenPartTwo.cpp
More file actions
149 lines (149 loc) · 5.28 KB
/
daySevenPartTwo.cpp
File metadata and controls
149 lines (149 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// COMMENTED OUT CODE DISCLAIMER
// To ensure compilation for the other files with the BigInt.hpp or any other external header files, this entire file is commented out.
// In the current structure that only uses one file at a time, header files are not perfectly organised.
// Having many files in a project you have no intention of using is not a normal use case, therefore organising this perfectly is not a priority.
// To test this file, get the BigInt hpp file from github and add it to the Header Files, in my case in a "libs" filter.
// Comment out any other cpp files using it. Only the .cpp files should need to be edited.
// /COMMENTED OUT CODE DISCLAIMER
//#include <cstdlib>
//#include <iostream>
//#include <fstream>
//#include <string>
//#include <vector>
//#include "BigInt.hpp" // https://github.com/faheel/BigInt
//// GMP would be applicable for production-ready applications. This one-file solution works fine for these leetcode-style problems
//
//namespace DaySevenPartTwo
//{
// namespace {
// void coutVectorContents(std::string prefix, std::vector<BigInt> vector);
//
// const bool toLog = false; // true false
//
// enum class Operator { Addition, Multiplication, Concatenation };
//
// bool recursiveOperationOrder(const std::vector<BigInt>& numbers, const BigInt operationRes, const size_t index, BigInt currentTotal,
// const size_t size, Operator operatorType)
// {
// if (currentTotal > operationRes) {
// return false;
// }
//
// if (index == size) {
// return currentTotal == operationRes;
// }
//
// BigInt nextValue = numbers[index];
// BigInt newTotal;
//
// switch (operatorType) {
// case Operator::Addition:
// newTotal = currentTotal + nextValue;
// break;
// case Operator::Multiplication:
// newTotal = currentTotal * nextValue;
// break;
// case Operator::Concatenation:
// newTotal = BigInt(currentTotal.to_string() + nextValue.to_string());
// break;
// }
//
// if (recursiveOperationOrder(numbers, operationRes, index + 1, newTotal, size, Operator::Addition)) {
// return true;
// }
// if (recursiveOperationOrder(numbers, operationRes, index + 1, newTotal, size, Operator::Multiplication)) {
// return true;
// }
// if (recursiveOperationOrder(numbers, operationRes, index + 1, newTotal, size, Operator::Concatenation)) {
// return true;
// }
//
// return false;
// }
//
// bool vectorVersionHasValidOperationOrder(const std::vector<BigInt>& numbers, const BigInt operationRes) {
// size_t size = numbers.size();
// if (recursiveOperationOrder(numbers, operationRes, 1, numbers[0], size, Operator::Addition)) return true;
// if (recursiveOperationOrder(numbers, operationRes, 1, numbers[0], size, Operator::Multiplication)) return true;
// if (recursiveOperationOrder(numbers, operationRes, 1, numbers[0], size, Operator::Concatenation)) return true;
// return false;
// }
//
// bool hasValidOperationOrder(const BigInt operationRes, const std::vector<BigInt>& numbers)
// { // possible complicated optimization: start with all multiplication and quick sort to the possible combination
// // There's likely an existing paper/phd thesis/decades of research put into an existing library for this
// return vectorVersionHasValidOperationOrder(numbers, operationRes);
//
// return false;
// }
//
// BigInt getValidResult(std::string& line)
// {
// size_t operationResPos = line.find(':');
// BigInt operationRes = BigInt(line.substr(0, operationResPos));
// std::vector<BigInt> numbers;
// size_t start = operationResPos + 1;
//
// size_t lineLen = line.size();
// while (start < lineLen) {
// size_t end = line.find(' ', start);
// if (end == std::string::npos) end = lineLen;
// if (end > start) {
// numbers.push_back(BigInt(line.substr(start, end - start)));
// }
// start = end + 1;
// }
// if (toLog) std::cout << "Testing: " << operationRes << ": ";
// if (toLog) coutVectorContents("", numbers);
// if (hasValidOperationOrder(operationRes, numbers))
// {
// if (toLog) std::cout << " yes\n";
// return operationRes;
// }
// if (toLog) std::cout << " nah\n";
// // if (toLog) std::cout << "No valid operation order found\n";
// return 0; // invalid result
// }
//
// void handleFile(std::ifstream& inputFile)
// {
// if (inputFile.is_open()) {
// std::string line;
// BigInt total = 0;
//
// while (getline(inputFile, line)) {
// total += getValidResult(line);
// // if (toLog) std::cout << "Total: " << total << "\n";
// if (!toLog) std::cout << '.';
// }
//
// std::cout << "\nTotal: " << total << '\n';
//
// std::cout << "Finished running program\n";
// }
// else {
// std::cout << "Unable to open file\n";
// }
// }
//
// void coutVectorContents(std::string prefix, std::vector<BigInt> vector)
// {
// std::cout << prefix;
// for (const auto& num : vector) {
// std::cout << num << ' ';
// }
// //std::cout << '\n';
// }
// }
// void daySevenPartTwo() {
// std::system("cls"); // clear terminal pre-printing
// std::cout << "Running program Day Seven Part Two" << '\n';
// const bool isFullFile = true; // true false
//
// std::string line;
// std::ifstream inputFile;
// (isFullFile) ? inputFile.open("daySevenFull.txt") : inputFile.open("daySevenTest.txt");
// handleFile(inputFile);
// }
//}
//// 21269673227726 too low