-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs.cpp
161 lines (156 loc) · 3.26 KB
/
pairs.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
#include<iostream>
#include<queue>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<map>
#include<list>
#include<climits>
#include<set>
#include<sstream>
#include<vector>
#include<stack>
using namespace std;
enum RAction { explode, split, none };
class RResult {
public:
RAction action;
int lexplode, rexplode;
RResult(RAction _action, int _lexplode = 0, int _rexplode = 0) {
action = _action;
lexplode = _lexplode;
rexplode = _rexplode;
}
};
class Pair {
public:
int x;
Pair *l;
Pair *r;
Pair(int _x) {
x = _x;
l = NULL;
r = NULL;
}
Pair(Pair *_l, Pair *_r) {
x = -1;
l = _l;
r = _r;
}
~Pair() {
if (x == -1) {
delete l;
delete r;
}
}
static Pair* parse(string::iterator &it) {
Pair *l, *r;
char c = *(it++);
while (c == ',' || c == ']') c = *(it++);
if (c == '[') {
l = Pair::parse(it);
r = Pair::parse(it);
return new Pair(l, r);
} else {
int x = 0;
while (true) {
if (c == ',' || c == ']') {
return new Pair(x);
}
x *= 10;
x += c - '0';
c = *(it++);
}
}
}
void addr(int xx) {
if (x != -1) x += xx;
else r->addr(xx);
}
void addl(int xx) {
if (x != -1) x += xx;
else l->addl(xx);
}
void reduce() {
RAction a = RAction::explode;
while(a != RAction::none) {
a = reducestep().action;
if (a == RAction::none) {
a = reducestep(4, true).action;
}
}
}
RResult reducestep(int lv = 4, bool allowsplit = false) {
if (x != -1) {
if (x >= 10 && allowsplit) {
l = new Pair(x / 2);
r = new Pair(x / 2 + x % 2);
x = -1;
return RResult(RAction::split);
}
return RResult(RAction::none);
} else {
if (lv == 0) {
int lx = l->x, rx = r->x;
delete l;
l = NULL;
delete r;
r = NULL;
x = 0;
return RResult(RAction::explode, lx, rx);
}
auto res = l->reducestep(lv - 1, allowsplit);
if (res.action == RAction::split) {
return res;
} else if (res.action == RAction::explode) {
if(res.rexplode != 0) r->addl(res.rexplode);
res.rexplode = 0;
return res;
} else {
res = r->reducestep(lv - 1, allowsplit);
if (res.action == RAction::explode){
if (res.lexplode != 0) l->addr(res.lexplode);
res.lexplode = 0;
}
return res;
}
}
}
Pair *copy() {
if (x != -1) return new Pair(x);
return new Pair(l->copy(), r->copy());
}
long long int mag() {
if (x != -1) return x;
return l->mag() * 3LL + r->mag() * 2LL;
}
void print() {
if (x != -1) cout << x;
else {
cout << "[";
l->print();
cout<< ",";
r->print();
cout<< "]";
}
}
};
int main(){
string s;
vector <Pair*> pairs;
while(cin >> s) {
auto it = s.begin();
pairs.push_back(Pair::parse(it));
}
long long maxmag = 0;
for (int i = 0; i < pairs.size(); ++i){
for (int j = 0; j < pairs.size(); ++j){
Pair *p = new Pair(pairs[i]->copy(), pairs[j]->copy());
p->reduce();
int mag = p->mag();
delete p;
maxmag = mag > maxmag ? mag : maxmag;
}
}
cout << maxmag << endl;
}