-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_04b.cpp
47 lines (46 loc) · 1.51 KB
/
day_04b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_04_input" ;
std::ifstream file(input);
std::string line;
int n_valid = 0;
while(std::getline(file, line)) {
bool valid = true;
std::size_t start = 0;
std::size_t end = line.find(' ', start);
std::unordered_set<std::string> words;
while(end != std::string::npos) {
auto word = line.substr(start, end - start);
std::cout << '|' << word << '|' << '\n';
std::sort(std::begin(word), std::end(word));
std::cout << '|' << word << '|' << '\n';
if(!words.insert(word).second) {
std::cout << "Duplicate word " << word << " detected. Invalid passphrase." << '\n';
valid = false;
break;
}
start = end + 1;
end = line.find(' ', start);
}
if (valid) {
auto word = line.substr(start, line.size() - start);
std::cout << '|' << word << '|' << '\n';
std::sort(std::begin(word), std::end(word));
std::cout << '|' << word << '|' << '\n';
if(!words.insert(word).second) {
std::cout << "Duplicate word " << word << " detected. Invalid passphrase." << '\n';
valid = false;
}
}
if (valid) {
std::cout << "Valid passphrase." << '\n';
n_valid++;
}
}
std::cout << "Number of valid passphrases: " << n_valid << '\n';
return 0;
}