-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_04a.cpp
43 lines (42 loc) · 1.13 KB
/
day_04a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::ifstream file;
file.open("../input/day_04_input");
std::string text;
int count = 0;
int fields = 0;
std::unordered_set<std::string> key_fields{"byr", "iyr", "eyr", "hgt",
"hcl", "ecl", "pid"};
while (std::getline(file, text)) {
text.erase(std::remove_if(std::begin(text), std::end(text),
[](char c) { return !std::isprint(c); }),
std::end(text));
if (text.empty()) {
if (fields == 7) {
++count;
}
fields = 0;
continue;
}
const std::string delimiter = " :";
size_t start = 0;
size_t end = text.find_first_of(delimiter);
while (end != std::string::npos) {
const std::string substr = text.substr(start, end - start);
if (key_fields.find(substr) != key_fields.end()) {
++fields;
}
start = end + 1;
end = text.find_first_of(delimiter, start);
}
}
if (fields == 7) {
++count;
}
std::cout << count << '\n';
return count;
}