-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC7.cpp
285 lines (243 loc) · 4.74 KB
/
AoC7.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// AoC7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Lib.h"
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
#include <algorithm>
#include<map>
using std::map;
class program{
public:
void op(){
cout << "name " << name << " weight " << weight;
for (unsigned int i = 0;i!=subs.size() ; i++)
{
cout << " sub " << subs[i];
}
cout << "\n";
}
void op(map<string, int> &a){
cout << "name " << name << " weight " << weight;
cout << " branch weight " << branchWeight;
for (unsigned int i = 0; i != subs.size(); i++)
{
cout << " sub " << subs[i] << "(" << a[subs[i]] <<")";
}
cout << "\n";
}
void regBelows(vector<string> &b)
{
for (unsigned int i = 0; i != subs.size(); i++)
{
b.push_back(subs[i]);
}
}
bool checkWeights(map<string, int> &weightMap)
{
bool found = false;
int sWeight = 0;
for (auto sIter = subs.begin() ; sIter!=subs.end() ; sIter++)
{
if (!found)
{
found = true;
sWeight = weightMap[*sIter];
}
else{
if (weightMap[*sIter] != sWeight)
{
return false;
}
}
}
return true;
}
string name;
int weight = 0;
int branchWeight = 0;
bool branchWeightSet = false;
vector<string> subs;
int below = false;
};
program parseLine(string &a)
{
program np;
string name;
int weight = 0;
unsigned int i = 0;
while (a[i] >= 'a' && a[i] <= 'z'){
name.append(1, a[i]);
i++;
}
np.name = name;
while (a[i] == ' ' || a[i] == '\t'){
i++;
}
if (a[i] == '(')
{
i++;
}
while (a[i] >= '0' && a[i] <= '9')
{
weight *= 10;
weight += a[i] - '0';
i++;
}
np.weight = weight;
if (a[i] == ')')
{
i++;
}
if (i == a.length())
{
return np;
}
while (a[i] == ' ' || a[i] == '\t'){
i++;
}
if (a[i] == '-' && a[i + 1] == '>')
{
i += 2;
}
while (a[i] == ' ' || a[i] == '\t'){
i++;
}
vector<string> sub;
string buf;
while (i < a.length())
{
char b = a[i];
if (b >= 'a' && b <= 'z')
{
buf.append(1, b);
i++;
}
else
{
if (buf.length() > 0)
{
sub.push_back(buf);
buf = string();
}
i++;
}
}
if (buf.length() > 0)
{
sub.push_back(buf);
buf = string();
}
np.subs = sub;
return np;
}
vector<program> readPrograms(vector<string> &lines)
{
vector<program> coll;
for (auto lIter = lines.begin() ; lIter!= lines.end() ; lIter++)
{
coll.push_back(parseLine(*lIter));
}
return coll;
}
map<string, int> createWeightMap(vector<program> &progs)
{
map<string, int> mp;
for (auto pIter = progs.begin() ; pIter!=progs.end() ; pIter++)
{
mp[(*pIter).name] = (*pIter).weight;
}
return mp;
}
program getProg(vector<program> &pl, string &name)
{
for (unsigned int i = 0; i!=pl.size() ; i++)
{
if (pl[i].name == name){
return pl[i];
}
}
return program();
}
int calcBranchWeight(string &a, vector<program> &programs)
{
program b = getProg(programs, a);
int tw = 0;
tw += b.weight;
for (auto sIter = b.subs.begin(); sIter!=b.subs.end() ; sIter++)
{
tw += calcBranchWeight(*sIter, programs);
}
return tw;
}
void traceUnbalanced(string &a, map<string, bool> &balanceMap, map<string, int> &weightMap, vector<program> &programs)
{
program b = getProg(programs, a);
bool unbalancedChildFound = false;
for (auto sIter = b.subs.begin(); sIter != b.subs.end(); sIter++)
{
if (balanceMap[(*sIter)] == false)
{
cout << "Unbalance trace : " << a << " Calling sub " << *sIter << "\n";
traceUnbalanced(*sIter, balanceMap, weightMap, programs);
unbalancedChildFound = true;
}
}
if (!unbalancedChildFound)
{
cout << "All children balanced: " << a << "\n";
b.op(weightMap);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
try{
vector<string> lines = readFileAsLines("in_7.txt");
vector<program> ps = readPrograms(lines);
vector<string> belows;
for (auto iIt = ps.begin(); iIt != ps.end(); iIt++)
{
(*iIt).regBelows(belows);
}
std::sort(belows.begin(), belows.end());
for (auto iIt = belows.begin(); iIt != belows.end(); iIt++)
{
for (auto bIt = ps.begin(); bIt!=ps.end(); bIt++)
{
if (bIt->name == *iIt)
{
(*bIt).below = true;
}
}
}
string sol;
for (auto iIt = ps.begin() ; iIt != ps.end() ; iIt++)
{
if (!(*iIt).below)
{
(*iIt).op();
sol = (*iIt).name;
}
}
map<string, int> weightMap;
for (auto pIt = ps.begin() ; pIt!=ps.end() ; pIt++)
{
int bWeight = calcBranchWeight((*pIt).name, ps);
(*pIt).branchWeight = bWeight;
weightMap[(*pIt).name] = bWeight;
(*pIt).branchWeightSet = true;
}
map<string, bool> balanceMap;
for (auto pIt = ps.begin(); pIt != ps.end(); pIt++)
{
balanceMap[(*pIt).name] = (*pIt).checkWeights(weightMap);
}
traceUnbalanced(sol, balanceMap, weightMap, ps);
}
catch (FileReadException &e)
{
}
return 0;
}