-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.cpp
103 lines (100 loc) · 3.07 KB
/
day9.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
#include <fstream>
#include <iostream>
#include <vector>
class Group {
public:
std::vector<Group*> *children;
std::string *text;
bool is_garbage;
Group *parent;
Group(Group *parent, bool is_garbage = false) {
this->children = new std::vector<Group*>();
this->text = new std::string();
this->parent = parent;
this->is_garbage = is_garbage;
}
void addChild(Group *child) {
this->children->push_back(child);
}
void appendChar(char *c) {
this->text->append(c, 0, 1);
}
int calcScore(int start = 1) {
int sum = start;
for(auto &child: *this->children)
if(!child->is_garbage)
sum += child->calcScore(start + 1);
return sum;
}
int garbageLength() {
int sum = this->text->length();
for(auto &child: *this->children)
sum += child->garbageLength();
return sum;
}
};
Group *parseInputFile(std::ifstream *file) {
std::string line;
getline(*file, line);
Group *current = NULL;
for(std::string::iterator it = line.begin(); it < line.end(); it++) {
if(current != NULL && current->is_garbage) {
if(it < line.end() - 1 && *it == '!') {
it++;
continue;
}
if(*it != '>')
current->appendChar(&*it);
else {
if(current->parent == NULL)
return current;
current->parent->addChild(current);
current = current->parent;
}
continue;
}
switch(*it) {
case '<':
current = new Group(current, true);
break;
case '{':
current = new Group(current);
break;
case '!':
it++;
break;
case ',':
if(it < line.end() - 1 && *(it + 1) == ',')
return NULL;
break;
case '}':
if(current->parent == NULL)
return current;
current->parent->addChild(current);
current = current->parent;
break;
default:
return NULL;
}
}
return NULL;
}
int main(int argc, char** argv) {
Group *start;
std::ifstream *input_file = new std::ifstream();
if(argc < 2) {
std::cout << "Invalid Arguments!" << std::endl;
return 1;
}
input_file->open(argv[1]);
if(!input_file->is_open()) {
std::cout << "Error opening file!" << std::endl;
return 1;
}
if((start = parseInputFile(input_file)) == NULL) {
std::cout << "Error parsing file!" << std::endl;
return 1;
}
std::cout << "The total score is " << start->calcScore() << "!" << std::endl;
std::cout << "The input contains " << start->garbageLength() << " non-canceled characters!" << std::endl;
}