-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06b.cpp
38 lines (34 loc) · 836 Bytes
/
day_06b.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
#include <array>
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char * argv[]) {
std::string input = "../input/day_06_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::getline(file, line);
constexpr int n = 14;
int count = 0;
std::array<int, 26> counter = {};
// ALWAYS initialize std::array.
for (int i = 0; i < n - 1; i++) {
if (counter[line[i] - 'a'] == 0) count++;
counter[line[i] - 'a'] += 1;
}
for (int i = n - 1; i < line.size(); i++) {
if (counter[line[i] - 'a'] == 0) count++;
counter[line[i] - 'a'] += 1;
if (count == n) {
std::cout << i + 1 << '\n';
break;
}
counter[line[i- n + 1] - 'a'] -= 1;
if (counter[line[i- n + 1] - 'a'] == 0) {
count--;
}
}
return 0;
}