-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (54 loc) · 1.44 KB
/
main.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
#include <fstream>
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
std::vector<std::vector<std::string>> readFile()
{
std::ifstream in { "input.txt" };
std::vector<std::vector<std::string>> result;
while (in.good()) {
std::vector<std::string> group;
std::string string;
while (std::getline(in, string), !string.empty()) {
group.emplace_back(string);
string.clear();
}
result.emplace_back(group);
}
return result;
}
size_t f1(const std::vector<std::vector<std::string>> &groups)
{
size_t result = 0;
for (const auto &group : groups) {
std::unordered_set<char> set;
for (const auto &answer : group) {
for (auto item : answer) set.insert(item);
}
result += set.size();
}
return result;
}
size_t f2(const std::vector<std::vector<std::string>> &groups)
{
size_t result = 0;
for (const auto &group : groups) {
std::unordered_map<char, size_t> map;
for (const auto &answer : group) {
for (auto item : answer) ++map[item];
}
size_t allAnswered = 0;
for (char i = 'a'; i <= 'z'; ++i) {
allAnswered += (map[i] == group.size());
}
result += allAnswered;
}
return result;
}
int main()
{
auto input = readFile();
std::cout << f1(input) << std::endl;
std::cout << f2(input) << std::endl;
}