-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.cpp
168 lines (151 loc) · 4.44 KB
/
day4.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <regex>
#include <fstream>
#include <string>
bool check_validity(const std::string& key, const std::string& value)
{
if(key == "byr")
{
int year = std::stoi(value);
if (year < 1920 || year > 2002)
{
return false;
}
}else if(key == "iyr")
{
int year = std::stoi(value);
if (year < 2010 || year > 2020)
{
return false;
}
}else if(key == "eyr")
{
int year = std::stoi(value);
if (year < 2020 || year > 2030)
{
return false;
}
}else if(key == "hgt")
{
if(value.substr(value.length() - 2 ) == "cm")
{
int height = std::stoi(value.substr(0, value.length()-2));
if (height < 150 || height > 193)
{
return false;
}
}
else if(value.substr(value.length() - 2 ) == "in")
{
int height = std::stoi(value.substr(0, value.length()-2));
if (height < 59 || height > 76)
{
return false;
}
}
}else if(key == "hcl")
{
if(value[0] != '#' || value.length() != 7)
{
return false;
}
std::regex re("#([0-9a-f]{6})");
std::smatch matches;
if (!std::regex_search(value, matches, re))
{
return false;
}
}else if(key == "ecl")
{
if (!(value.find("amb") != std::string::npos || value.find("blu") != std::string::npos || value.find("brn") != std::string::npos
|| value.find("gry") != std::string::npos || value.find("grn") != std::string::npos || value.find("hzl") != std::string::npos
|| value.find("oth") != std::string::npos))
{
return false;
}
}else if(key == "pid")
{
if (!(regex_match(value.cbegin(), value.cend(), std::regex("[0-9]{9}+"))))
{
return false;
}
}
return true;
}
int part_one(const std::vector<std::string>& passports)
{
int valid = 0;
std::regex regex("([a-z]+):");
std::smatch matches;
for(int i = 0; i < passports.size(); i++)
{
bool byr = false, iyr = false, eyr = false, hgt = false, hcl = false, ecl = false, pid = false;
byr = passports[i].find("byr:") != std::string::npos;
iyr = passports[i].find("iyr:") != std::string::npos;
eyr = passports[i].find("eyr:") != std::string::npos;
hgt = passports[i].find("hgt:") != std::string::npos;
hcl = passports[i].find("hcl:") != std::string::npos;
ecl = passports[i].find("ecl:") != std::string::npos;
pid = passports[i].find("pid:") != std::string::npos;
if (byr && iyr && eyr && hgt && hcl && ecl && pid)
{
valid++;
}
}
return valid;
}
int part_two(const std::vector<std::string>& passports)
{
int valid = 0;
const std::regex regex("([a-zA-Z0-9_]+):([^\r\n\t\f\v ]+)");
for(const std::string& passport: passports)
{
std::vector<std::string> tmp;
tmp.push_back(passport);
if (part_one(tmp) != 1)
{
continue;
}
std::smatch match;
std::string::const_iterator search(passport.cbegin());
bool valid_all = true;
while (regex_search(search, passport.cend(), match, regex))
{
//std::cout << match[1] << " - " << match[2] << " is: " << check_validity(match[1].str(), match[2].str()) << std::endl;
valid_all &= check_validity(match[1].str(), match[2].str());
search = match.suffix().first;
}
if (valid_all)
{
valid++;
//std::cout << passport << std::endl;
}
}
return valid;
}
int main()
{
std::ifstream file("input");
std::vector<std::string> passports{};
std::string passport{};
for(std::string line; std::getline(file, line);)
{
if(line.empty())
{
passports.push_back(passport);
passport = "";
continue;
}else if (passport.length() > 0)
{
passport += " ";
}
passport += line;
}
// check the last one and include it if the file doesn't end with empty line
if (passport.length() > 0)
{
passports.push_back(passport);
}
std::cout << part_one(passports) << std::endl;
std::cout << part_two(passports) << std::endl;
}