-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
60 lines (53 loc) · 1.82 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
//
// Created by daniel on 01.12.20.
//
#include <fstream>
#include <vector>
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
std::ifstream infile("../input.txt");
std::string line;
vector<string> lines;
std::string buffer;
while (std::getline(infile, line)) {
if (line.empty()) {
lines.push_back(buffer);
buffer = "";
} else {
if (!buffer.empty()) buffer.append(" ");
buffer.append(line);
}
}
int valid1 = 0;
int valid2 = 0;
vector<regex> patterns1 = {regex("(\\s|^)byr:\\S"), regex("(\\s|^)iyr:\\S"),
regex("(\\s|^)eyr:\\S"), regex("(\\s|^)hcl:\\S"),
regex("(\\s|^)ecl:\\S"), regex("(\\s|^)pid:\\S"),
regex("(\\s|^)hgt:\\S")};
vector<regex> patterns2 = {regex("byr:(19[2-9][0-9]|200[0-2])"),
regex("iyr:(201[0-9]|2020)"),
regex("eyr:(202[0-9]|2030)"),
regex("hgt:((1[5-8][0-9]|19[0-3])cm|(59|6[0-9]|7[0-6])in)"),
regex("hcl:#[0-9a-f]{6}(?!\\w)"),
regex("ecl:(amb|blu|brn|gry|grn|hzl|oth)"),
regex("pid:[0-9]{9}(?!\\w)")
};
for (const auto &s : lines) {
int matches1 = 0;
int matches2 = 0;
for (const auto &pattern : patterns2) {
if (std::regex_search(s, pattern)) matches2++;
}
for (const auto &pattern : patterns1) {
if (std::regex_search(s, pattern)) matches1++;
}
if (matches1 == patterns1.size()) valid1++;
if (matches2 == patterns2.size()) valid2++;
}
cout << valid1 << endl;
cout << valid2;
return 0;
}