-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.cpp
More file actions
129 lines (108 loc) · 3.2 KB
/
day23.cpp
File metadata and controls
129 lines (108 loc) · 3.2 KB
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 "utils/helpers.hpp"
typedef std::set<std::string> Computers;
typedef std::map<std::string, Computers> Connections;
typedef std::set<Computers> LanSet;
void addNetwork3(const std::string& current, const Computers& comps, const Connections& conn, LanSet& lan) {
Computers node;
Connections::const_iterator found;
for (const std::string& comp: comps) {
found = conn.find(comp);
if (found != conn.end()) {
node = found->second;
for (const std::string& comp2: node) {
if (comps.find(comp2) != comps.end()) {
lan.insert(Computers({current, comp, comp2}));
}
}
}
}
}
void addNetwork(const std::string& current, const Connections& conn, LanSet& lans, Computers lan, Computers& checked) {
Connections::const_iterator found;
if (checked.find(current) != checked.end())
return;
checked.insert(current);
found = conn.find(current);
if (lan.empty()) {
lan.insert(current);
for (const std::string& c : found->second) {
addNetwork(c, conn, lans, lan, checked);
}
}
else {
found = conn.find(current);
for (const std::string& c : lan) {
if (found->second.find(c) == found->second.end()) {
lans.insert(lan);
return;
}
}
lan.insert(current);
for (const std::string& c : found->second) {
addNetwork(c, conn, lans, lan, checked);
}
}
}
ui countT(const LanSet& lan) {
ui count = 0;
for (const Computers& comps : lan) {
for (const std::string& c : comps) {
if (c[0] == 't') {
++count;
break;
}
}
}
return count;
}
std::string getHigherLan(const LanSet& l) {
std::string res;
LanSet::const_iterator bigger;
int biggerSize = 0;
for (LanSet::const_iterator c = l.begin(); c != l.end(); ++c) {
if (c->size() > biggerSize) {
biggerSize = c->size();
bigger = c;
}
}
for (const std::string& str: *bigger) {
res += str;
res += ',';
}
res.erase(res.end() - 1);
return res;
}
int main() {
ui part = 0;
std::ifstream input;
Computers checked;
Connections conn;
LanSet lans;
std::string line;
std::string right;
std::string left;
if (getFileAndPart(23, input, part)) {
return errno;
}
while (!input.eof()) {
getline(input, line);
right = line.substr(3, 2);
left = line.substr(0, 2);
conn[right].insert(left);
conn[left].insert(right);
}
if (part == 1) {
for (Connections::const_iterator cit = conn.begin(); cit != conn.end(); ++cit) {
addNetwork3(cit->first, cit->second, conn, lans);
}
std::cout << "result is " << countT(lans) << '\n';
}
else {
for (Connections::const_iterator cit = conn.begin(); cit != conn.end(); ++cit) {
checked.clear();
addNetwork(cit->first, conn, lans, Computers(), checked);
}
std::cout << "result is " << getHigherLan(lans) << '\n';
}
return 0;
}