-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_04b.cpp
55 lines (51 loc) · 1.14 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
48
49
50
51
52
53
54
55
#include <fstream>
#include <iostream>
#include <string>
bool meetsConditions(int num) {
int prev_val = 10;
bool repeat = false;
int n_repeats = 0;
while (num > 0) {
const auto val = num % 10;
num = int(num / 10);
if (val > prev_val) {
return false;
}
if (prev_val == val) {
n_repeats += 1;
} else {
if (n_repeats == 1) {
repeat = true;
}
n_repeats = 0;
}
prev_val = val;
}
if (n_repeats == 1) {
repeat = true;
}
return repeat;
}
int main(int argc, char *argv[]) {
// Get input
std::string input = "../input/day_04_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string input_str;
std::getline(file, input_str);
const auto delimiter_index = input_str.find("-");
auto min_val = std::stoi(input_str.substr(0, delimiter_index));
auto max_val = std::stoi(input_str.substr(
delimiter_index + 1, input_str.size() - delimiter_index - 1));
// Solve
size_t count = 0;
for (int num = min_val; num < max_val; ++num) {
if (meetsConditions(num)) {
count += 1;
}
}
std::cout << count << '\n';
return count;
}