-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.cpp
68 lines (56 loc) · 1.46 KB
/
part1.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
#include "Day05.h"
bool is_nice_string(string& str);
bool has_at_least_three_vowels(string& str);
bool has_double_letter(string& str);
bool contains_naughty_substrings(string& str);
bool Day05::solve_p1(string& result){
unsigned ctr = 0;
for(unsigned ii = 0; ii < data.size(); ii++){
if( is_nice_string(data[ii]) ){
ctr++;
}
}
result = to_string(ctr);
return true;
}
bool is_nice_string(string& str){
return has_at_least_three_vowels(str)
&& has_double_letter(str)
&& ! contains_naughty_substrings(str);
}
bool has_at_least_three_vowels(string& str){
unsigned vowels = 0;
for(unsigned ii = 0; ii < str.length(); ii++){
switch(str[ii]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
}
}
return vowels >= 3;
}
bool has_double_letter(string& str){
char last = '\0';
for(unsigned ii = 0; ii < str.length(); ii++){
char cur = str[ii];
if( cur == last ){
return true;
} else {
last = cur;
}
}
return false;
}
bool contains_naughty_substrings(string& str){
vector<string> naughty_strings = {"ab", "cd", "pq", "xy"};
unsigned check = 0;
for(unsigned ii = 0; ii < naughty_strings.size(); ii++){
if( str.find(naughty_strings[ii]) == string::npos ){
check++;
}
}
return check != naughty_strings.size();
}