This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve18.cc
170 lines (154 loc) · 3.64 KB
/
solve18.cc
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
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#include <vector>
class SFNum {
public:
bool literally;
long long val; // let's not use a union, thanks
SFNum * p[2]; // first, second elements, which we now own
SFNum(long long l) : literally(true), val(l), p{0,0} {}
SFNum(SFNum * head, SFNum * tail) : literally(false), val(0), p{head,tail} { }
~SFNum() {
delete p[1];
delete p[0];
}
bool isLiteral() const { return literally; }
void print(bool lf = true) const;
void reduce();
long long magnitude() const;
SFNum * clone() const;
static SFNum * read(FILE * fp);
private:
bool splitFirst();
SFNum * findFirstLiteral(unsigned direction = 0);
bool explodeFirst(unsigned depth = 0, SFNum * llb = 0, SFNum * fla = 0);
};
void SFNum::print(bool lf) const
{
if (isLiteral()) {
printf("%llu", val);
}
else {
putchar('[');
p[0]->print(false);
putchar(',');
p[1]->print(false);
putchar(']');
}
if (lf) putchar('\n');
}
bool SFNum::splitFirst()
{
if (isLiteral()) {
if (val >= 10) {
p[0] = new SFNum(val >> 1);
p[1] = new SFNum((val+1) >> 1);
literally = false;
val = 0;
return true;
}
return false;
}
else {
return p[0]->splitFirst() || p[1]->splitFirst();
}
}
SFNum * SFNum::findFirstLiteral(unsigned direction)
{
if (isLiteral()) return this;
return p[!!direction]->findFirstLiteral(direction);
}
bool SFNum::explodeFirst(unsigned depth, SFNum * llb, SFNum * fla)
{
if (isLiteral()) return false;
if (p[0]->isLiteral() && p[1]->isLiteral() && depth == 4) {
if (llb) llb->val += p[0]->val;
if (fla) fla->val += p[1]->val;
delete p[0];
delete p[1];
p[0] = 0;
p[1] = 0;
literally = true;
val = 0;
return true;
}
else {
return p[0]->explodeFirst(depth+1, llb, p[1]->findFirstLiteral(0))
|| p[1]->explodeFirst(depth+1, p[0]->findFirstLiteral(1), fla);
}
}
void SFNum::reduce()
{
do {
while (explodeFirst()) ;
} while (splitFirst());
}
long long SFNum::magnitude() const
{
if (isLiteral()) return val;
return 3*p[0]->magnitude() + 2*p[1]->magnitude();
}
SFNum * SFNum::clone() const
{
if (isLiteral()) return new SFNum(val);
return new SFNum(p[0]->clone(), p[1]->clone());
}
SFNum * SFNum::read(FILE * fp)
{
int c;
do c = fgetc(fp); while (c > 0 && isspace(c));
if (c < 0) return 0; // EOF
else if (c=='[') {
SFNum *a = read(fp);
do c = fgetc(fp); while (c > 0 && isspace(c));
assert(c==',');
SFNum *b = read(fp);
do c = fgetc(fp); while (c > 0 && isspace(c));
assert(c==']');
return new SFNum(a,b);
}
else {
assert(isdigit(c));
ungetc(c, fp);
long long l;
fscanf(fp, "%lld", &l);
return new SFNum(l);
}
}
int main()
{
std::vector<SFNum *> inputs;
SFNum * theNumber = SFNum::read(stdin);
assert(theNumber);
inputs.push_back(theNumber->clone());
for (;;) {
SFNum * nextNumber = SFNum::read(stdin);
if (!nextNumber) break;
inputs.push_back(nextNumber->clone());
theNumber = new SFNum(theNumber, nextNumber); // NB takes ownership!
theNumber->reduce();
}
theNumber->print();
printf("Final magnitude: %lld\n", theNumber->magnitude());
delete theNumber;
unsigned i, j;
long long maxmag = 0;
for(i = 0; i < inputs.size(); ++i) {
for(j = 0; j < inputs.size(); ++j) {
if (i != j) {
SFNum sum(inputs[i]->clone(), inputs[j]->clone());
sum.reduce();
long long mag = sum.magnitude();
if (mag > maxmag) maxmag = mag;
}
}
}
printf("Max magnitude: %lld\n", maxmag);
while (inputs.size()) {
delete inputs.back();
inputs.pop_back();
}
return 0;
}