-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.cpp
42 lines (36 loc) · 1.12 KB
/
day02.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
#include <bits/stdc++.h>
#include <ranges>
using namespace std;
struct PasswordLine
{
int min_count;
int max_count;
char letter;
string password;
};
istream& operator>>(istream& in, PasswordLine& pwdline)
{
char dummy;
in >> pwdline.min_count >> dummy >> pwdline.max_count >> pwdline.letter >> dummy >> pwdline.password;
return in;
}
int a(const vector<PasswordLine>& passwords)
{
return ranges::count_if(passwords, [](const PasswordLine& pwdline) {
auto occ = ranges::count(pwdline.password, pwdline.letter);
return occ >= pwdline.min_count && occ <= pwdline.max_count;
});
}
int b(const vector<PasswordLine>& passwords)
{
return ranges::count_if(passwords, [](const PasswordLine& pwdline) {
return (pwdline.password.at(pwdline.min_count - 1) == pwdline.letter)
^ (pwdline.password.at(pwdline.max_count - 1) == pwdline.letter);
});
}
int main()
{
ifstream file("inputs/day02.txt");
vector<PasswordLine> passwords(istream_iterator<PasswordLine>{file}, istream_iterator<PasswordLine>{});
cout << a(passwords) << "\n" << b(passwords) << endl;
}