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
|
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
auto
read_file(string_view filename)
{
fstream input{ filename };
vector<tuple<long, long, char, string>> data;
for ( string line; getline(input, line); ) {
long min = 0;
long max = 0;
char chr = 0;
char password[100];
sscanf(line.data(), "%ld-%ld %c: %99s", &min, &max, &chr, password); // NOLINT
data.emplace_back(min, max, chr, password);
}
return data;
}
void
part1(vector<tuple<long, long, char, string>>& data)
{
long sum = 0;
for ( const auto& [min, max, chr, password]: data ) {
const auto amount = count(password.begin(), password.end(), chr);
if ( min <= amount && amount <= max ) {
++sum;
}
}
cout << sum << endl;
}
void
part2(vector<tuple<long, long, char, string>>& data)
{
long sum = 0;
for ( const auto& [min, max, chr, password]: data ) {
if ( (password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr) ) {
++sum;
}
}
cout << sum << endl;
}
int
main()
{
auto input = read_file("data/day02.txt");
part1(input);
part2(input);
}
|