-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18b.cpp
125 lines (113 loc) · 3.48 KB
/
day_18b.cpp
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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
std::deque<std::string> convertStringToDeque(const std::string& line) {
std::deque<std::string> cur_expr;
for (int i = 0; i < line.size(); i++) {
if (line[i] != ' ') {
cur_expr.push_back(std::string{line[i]});
}
}
return cur_expr;
}
std::deque<std::string> getSubExpr(std::deque<std::string>& expr) {
int brack_count = 0;
expr.pop_front();
std::deque<std::string> new_expr;
while (brack_count > 0 || expr.front() != ")") {
if (expr.front() == "(") {
brack_count++;
}
if (expr.front() == ")") {
brack_count--;
}
new_expr.push_back(expr.front());
expr.pop_front();
}
expr.pop_front();
return new_expr;
}
long long calcExpressionValue(std::deque<std::string>& expr) {
while (expr.size() > 1) {
long long operand1;
long long operand2;
std::string op;
if (expr.front() != "(") {
operand1 = std::stol(expr.front());
expr.pop_front();
} else {
auto new_expr = getSubExpr(expr);
operand1 = calcExpressionValue(new_expr);
}
op = expr.front();
expr.pop_front();
if (expr.front() != "(") {
operand2 = std::stol(expr.front());
expr.pop_front();
} else {
auto new_expr = getSubExpr(expr);
operand2 = calcExpressionValue(new_expr);
}
if (op == "+") expr.push_front(std::to_string(operand1 + operand2));
if (op == "*") expr.push_front(std::to_string(operand1 * operand2));
}
return std::stol(expr.front());
}
long long calcExpressionValueAdvanced(std::deque<std::string>& expr) {
std::deque<std::string> simplifiedEquation;
while (expr.size() > 1) {
long long operand1;
long long operand2;
std::string op;
if (expr.front() != "(") {
operand1 = std::stol(expr.front());
expr.pop_front();
} else {
auto new_expr = getSubExpr(expr);
operand1 = calcExpressionValueAdvanced(new_expr);
}
op = expr.front();
expr.pop_front();
if (expr.front() != "(") {
operand2 = std::stol(expr.front());
expr.pop_front();
} else {
auto new_expr = getSubExpr(expr);
operand2 = calcExpressionValueAdvanced(new_expr);
}
if (op == "+") expr.push_front(std::to_string(operand1 + operand2));
// If the operation is multiplcation, add the second operand back to the
// front of the original deque and add the first operand the the operator to
// the simplified equation to build up an equation that consists of only
// multiplication
if (op == "*") {
expr.push_front(std::to_string(operand2));
simplifiedEquation.push_back(std::to_string(operand1));
simplifiedEquation.push_back(op);
}
}
// Get the final operand from the equation into simplified equation
std::string finalOperand = expr.front();
simplifiedEquation.push_back(finalOperand);
expr.pop_front();
// Finally solve the remaining multiplication using our original
// solveEquation()
return calcExpressionValue(simplifiedEquation);
}
int main() {
std::fstream file{"../input/day_18_input"};
std::string line;
long long sum = 0;
while (std::getline(file, line)) {
line.erase(std::remove_if(std::begin(line), std::end(line),
[](auto c) { return !isprint(c) || c == ' '; }),
std::end(line));
auto d_line = convertStringToDeque(line);
sum += calcExpressionValueAdvanced(d_line);
}
std::cout << sum << '\n';
return sum;
}