-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.cpp
129 lines (112 loc) · 2.96 KB
/
day18.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
#include <iostream>
#include "data.h"
#include <sstream>
std::map<char, std::string> getReplacements()
{
std::map<char, std::string> ret{};
return ret;
}
bool part2 = false;
int64_t eval(std::string l, bool override = false)
{
int64_t v;
int64_t last;
std::cout << "evaluating " << l << std::endl;
for(int64_t i = 0; i < l.size(); i++)
{
if (l[i] =='(')
{
last = i;
}
if (l[i] ==')')
{
std::string sub = l.substr(last + 1, i - (last + 1));
v = eval(sub);
sub = l.substr(0, last) + std::to_string(v) + l.substr(i + 1, l.size() - i - 1);
return eval(sub);
}
}
if(!part2 || override || l.find('+') == std::string::npos || l.find('*') == std::string::npos)
{
int op = 0;
int64_t acc = 0;
int64_t acc0 = 0;
for(int i=0; i<l.size(); i++)
{
if (l[i] >= '0' && l[i] <= '9')
{
acc *= 10;
acc += (l[i] - '0');
}
if (!op && l[i] == '+')
{
op = 1;
acc0 = acc;
acc = 0;
}
else if (!op && l[i] == '*')
{
op = 2;
acc0 = acc;
acc = 0;
}
else if (op && (l[i] == '*' || l[i] == '+'))
{
//std::cout << "acc " << acc << " acc0 " << acc0 << std::endl;
if (op == 1) acc0 = acc + acc0;
if (op == 2) acc0 = acc * acc0;
//std::cout << "partial " << acc0 << std::endl;
acc = 0;
op = (l[i] == '*' ? 2 : 1);
}
}
//std::cout << "acc " << acc << " acc0 " << acc0 << std::endl;
if (op == 1) acc0 = acc + acc0;
if (op == 2) acc0 = acc * acc0;
//std::cout << "partial " << acc0 << std::endl;
return acc0;
}
std::string was = l;
int lasts = -1;
for(int i=0; i<l.size(); i++)
{
if (l[i] == '*') lasts = i;
if (l[i] == '+')
{
if (lasts != -1)
{
l[lasts + 1] = '(';
int j;
bool done = false;
for(j=i;j<l.size();j++)
{
if (l[j] == '*')
{
l[j-1] = ')';
done = true;
i = j-1;
break;
}
}
if (!done)
{
l = l + ")";
i = l.size();
}
}
}
}
return eval(l, true);
}
void process(Data& data)
{
part2 = true;
uint64_t sum = 0;
for(auto l:data.raw)
{
sum += eval(l);
std::cout << l << std::endl;
std::cout << sum << std::endl << std::endl;;
}
std::cout << sum << std::endl;
}