-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.cpp
219 lines (188 loc) · 4.44 KB
/
day7.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
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <chrono>
using namespace std;
const int MAX_PROGRAMS = 1500;
const int MAX_CHILDREN = 10;
const int MAX_LINE_LENGTH = 1000;
const int MAX_PROGRAM_LENGTH = 10;
const char FILENAME[] = "input.txt";
int parseNum(const char* s)
{
size_t len = strlen(s);
int num = 0;
for (size_t i = 0; i < len; i++)
{
num += (s[i] - '0') * static_cast<int>(pow(10, len - 1 - i));
}
return num;
}
int getProgramByName(const char programs[][MAX_PROGRAM_LENGTH], const int nPrograms, const char name[])
{
for (int i = 0; i < nPrograms; i++)
if (strcmp(programs[i], name) == 0)
return i;
return -1;
}
class Tree;
struct Node
{
Node(Tree*, char*, int);
char name[MAX_PROGRAM_LENGTH];
int weight;
void addChild(Node*);
Tree* tree;
Node* children[MAX_CHILDREN];
int nChildren;
bool hasParent;
};
Node::Node(Tree* tr, char* nm, int wt)
{
tree = tr;
strcpy(name, nm);
weight = wt;
nChildren = 0;
hasParent = false;
}
void Node::addChild(Node* ptr)
{
children[nChildren] = ptr;
nChildren++;
}
class Tree
{
public:
Tree();
~Tree();
void display();
Node* addNode(char*, int);
Node* node(int);
Node* node(char*);
Node* root();
private:
Node* m_programs[MAX_PROGRAMS];
int m_nPrograms;
};
Tree::Tree()
{
m_nPrograms = 0;
}
Tree::~Tree()
{
for (int i = 0; i < m_nPrograms; i++)
delete m_programs[i];
}
Node* Tree::addNode(char* nm, int wt = 0)
{
Node* ptr = new Node(this, nm, wt);
m_programs[m_nPrograms] = ptr;
m_nPrograms++;
return ptr;
}
Node* Tree::node(int key)
{
if (key < m_nPrograms)
return m_programs[key];
return nullptr;
}
Node* Tree::node(char* nm)
{
for (int i = 0; i < m_nPrograms; i++)
if (strcmp(m_programs[i]->name, nm) == 0)
return m_programs[i];
return nullptr;
}
Node* Tree::root()
{
for (int i = 0; i < m_nPrograms; i++)
if (!m_programs[i]->hasParent)
return m_programs[i];
return nullptr;
}
void Tree::display()
{
for (int i = 0; i < m_nPrograms; i++)
{
cout << m_programs[i]->name << ": " << m_programs[i]->weight << endl;
if (m_programs[i]->nChildren)
{
for (int j = 0; j < m_programs[i]->nChildren; j++)
cout << '\t' << m_programs[i]->children[j]->name << endl;
}
}
}
int totalWeight(Node* node, int weight = 0)
{
weight += node->weight;
if (node->nChildren)
for (int i = 0; i < node->nChildren; i++)
weight += totalWeight(node->children[i]);
return weight;
}
int main()
{
auto start = chrono::high_resolution_clock::now();
Tree* tree = new Tree;
fstream stream;
stream.open(FILENAME);
if (stream.fail())
{
cout << "Couldn't open file" << endl;
exit(1);
}
char line[MAX_LINE_LENGTH];
char* token;
const char delimiters[] = " ()->,";
int tokenCount;
int programCount = 0;
// add all the nodes
while (stream.getline(line, MAX_LINE_LENGTH))
{
tokenCount = 0;
token = strtok(line, delimiters);
while (token != nullptr)
{
if (tokenCount == 0)
tree->addNode(token);
else if (tokenCount == 1)
tree->node(programCount)->weight = parseNum(token);
else
break;
token = strtok(nullptr, delimiters);
tokenCount++;
}
programCount++;
}
stream.clear();
stream.seekg(0, ios::beg);
programCount = 0;
// add children to nodes
while (stream.getline(line, MAX_LINE_LENGTH))
{
tokenCount = 0;
token = strtok(line, delimiters);
while (token != nullptr)
{
if (tokenCount > 1)
{
Node* child = tree->node(token);
tree->node(programCount)->addChild(child);
child->hasParent = true;
}
token = strtok(nullptr, delimiters);
tokenCount++;
}
programCount++;
}
stream.close();
cout << tree->root()->name << endl;
cout << totalWeight(tree->node("tknk")) << endl;
delete tree;
auto finish = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = finish - start;
cout << elapsed.count() << endl;
}