-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.cpp
207 lines (184 loc) · 5.75 KB
/
18.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <fstream>
#include <memory>
#include <stack>
#include <optional>
#include <cmath>
#include <vector>
class Node;
using NodePtr = std::unique_ptr<Node>;
class Node {
public:
explicit Node(std::optional<int> value = std::nullopt,
NodePtr left = nullptr, NodePtr right = nullptr,
Node* parent = nullptr);
void print() const;
void reduce(bool verbose = false);
int magnitude() const;
private:
bool isReduced(int depth = 0) const;
void explode(int depth = 0);
bool split(bool used);
static Node* getPredecessor(Node* n);
static Node* getSuccessor(Node* n);
std::optional<int> value_;
NodePtr left_;
NodePtr right_;
Node* parent_;
};
Node::Node(std::optional<int> value, NodePtr left, NodePtr right, Node* parent) {
value_ = value;
left_ = std::move(left);
right_ = std::move(right);
parent_ = parent;
if (left_ != nullptr) left_->parent_ = this;
if (right_ != nullptr) right_->parent_ = this;
}
void Node::print() const {
if (left_ != nullptr) {
std::cout << '[';
left_->print();
}
if (value_.has_value()) std::cout << value_.value();
else std::cout << ',';
if (right_ != nullptr) {
right_->print();
std::cout << ']';
}
}
bool Node::isReduced(int depth) const {
bool reduced = true;
if (left_ != nullptr) reduced = left_->isReduced(depth + 1);
if (!reduced || depth > 4 || (value_.has_value() && value_.value() >= 10)) return false;
if (right_ != nullptr) reduced = right_->isReduced(depth + 1);
return reduced;
}
Node* Node::getPredecessor(Node* n) {
n = n->parent_;
while (n->parent_ != nullptr) {
if (n->parent_->right_.get() == n) {
n = n->parent_->left_.get();
while (n->right_ != nullptr) n = n->right_.get();
return n;
}
n = n->parent_;
}
return nullptr;
}
Node* Node::getSuccessor(Node *n) {
n = n->parent_;
while (n->parent_ != nullptr) {
if (n->parent_->left_.get() == n) {
n = n->parent_->right_.get();
while (n->left_ != nullptr) n = n->left_.get();
return n;
}
n = n->parent_;
}
return nullptr;
}
void Node::explode(int depth) {
if (depth == 4 && left_ != nullptr && right_ != nullptr) {
int value = left_->value_.value();
auto pred = getPredecessor(left_.get());
left_ = nullptr;
if (pred != nullptr) pred->value_.value() += value;
value = right_->value_.value();
auto succ = getSuccessor(right_.get());
right_ = nullptr;
if (succ != nullptr) succ->value_.value() += value;
value_ = 0;
return;
}
if (left_ != nullptr) left_->explode(depth + 1);
if (right_ != nullptr) right_->explode(depth + 1);
}
void Node::reduce(bool verbose) {
while (!isReduced()) {
if (verbose) std::cout << "Reducing..." << std::endl;
explode();
if (verbose) {
print();
std::cout << std::endl;
}
split(false);
if (verbose) {
print();
std::cout << std::endl;
}
}
}
bool Node::split(bool used) {
if (left_ != nullptr && !used) used = left_->split(used);
if (value_.has_value() && value_.value() >= 10) {
left_ = std::make_unique<Node>(floor((double) value_.value() / 2), nullptr, nullptr, this);
right_ = std::make_unique<Node>(ceil((double) value_.value() / 2), nullptr, nullptr, this);
value_ = std::nullopt;
return true;
}
if (right_ != nullptr && !used) used = right_->split(used);
return used;
}
int Node::magnitude() const {
int result;
if (left_ != nullptr) result = 3 * left_->magnitude();
if (value_.has_value()) return value_.value();
if (right_ != nullptr) result += 2 * right_->magnitude();
return result;
}
int charToInt(int c) {
return c - '0';
}
NodePtr createNumber(const std::string& line) {
std::stack<char> stack;
std::stack<NodePtr> nodes;
for (auto && c: line) {
if (c == '[' || c == ',') stack.push(c);
else if (std::isdigit(c)) nodes.push(std::make_unique<Node>(charToInt(c)));
else if (c == ']') {
NodePtr right = std::move(nodes.top());
nodes.pop();
NodePtr left = std::move(nodes.top());
nodes.pop();
nodes.push(std::make_unique<Node>(std::nullopt, std::move(left), std::move(right)));
stack.pop();
}
}
auto node = std::move(nodes.top());
nodes.pop();
return node;
}
void partOne(const std::vector<std::string>& lines) {
auto num = createNumber(lines[0]);
for (int i = 1; i < lines.size(); ++i) {
auto nextNum = createNumber(lines[i]);
num = std::make_unique<Node>(std::nullopt, std::move(num), std::move(nextNum));
num->reduce();
}
num->print();
std::cout << std::endl;
std::cout << num->magnitude() << std::endl;
}
void partTwo(const std::vector<std::string>& lines) {
int maxMagnitude = INT_MIN;
for (int i = 0; i < lines.size(); ++i) {
for (int j = 0; j < lines.size(); ++j) {
if (j != i) {
auto add = std::make_unique<Node>(std::nullopt, createNumber(lines[i]), createNumber(lines[j]));
add->reduce();
int magnitude = add->magnitude();
if (magnitude > maxMagnitude) maxMagnitude = magnitude;
}
}
}
std::cout << maxMagnitude << std::endl;
}
int main(int argc, char* argv[]) {
std::ifstream dataFile(argv[1]);
std::vector<std::string> lines;
std::string line;
while (std::getline(dataFile, line)) lines.push_back(line);
dataFile.close();
partOne(lines);
partTwo(lines);
}