-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathday_02b.cpp
More file actions
75 lines (71 loc) · 2.31 KB
/
day_02b.cpp
File metadata and controls
75 lines (71 loc) · 2.31 KB
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
69
70
71
72
73
74
75
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Range {
std::size_t start;
std::size_t end;
};
int main(int argc, char* argv[])
{
std::string input = "../input/day_02_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::size_t start = 0;
std::vector<Range> ranges;
std::getline(file, line);
std::size_t end_idx = 0;
while(end_idx != line.size()) {
const std::size_t dash_idx = line.find('-', start);
end_idx = line.find(',', start);
if (end_idx == std:: string::npos) end_idx = line.size();
Range current;
current.start = std::stoull(line.substr(start, dash_idx));
current.end = std::stoull(line.substr(dash_idx+1, end_idx));
ranges.push_back(current);
start = end_idx+1;
}
std::size_t sum = 0;
for (const auto& r : ranges) {
for (std::size_t number = r.start; number <= r.end; number++) {
// Get nuber of digits
std::size_t n_digits = 0;
{
std::size_t n = number;
while (n > 0) {
n /= 10;
n_digits++;
}
}
// Check for all the possible sequence sizes
for (std::size_t seq_size = 1; seq_size < n_digits; seq_size++) {
if (n_digits % seq_size != 0) continue;
std::size_t order_of_sequence = 1; // order of magnitue of the sequence, 10^n_parts
for (int i = 0; i < seq_size; i++) {
order_of_sequence *= 10;
}
const std::size_t sequence = number % order_of_sequence;
std::size_t n = number;
bool is_pattern = true;
while (n > 0) {
// Check whether the last n_digits != sequence
if(n % order_of_sequence != sequence) {
is_pattern = false;
break;
}
n /= order_of_sequence;
}
if (is_pattern) {
sum += number;
break;
}
}
}
}
std::cout << sum << '\n';
return 0;
}