-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.cpp
More file actions
93 lines (85 loc) · 2.22 KB
/
day2.cpp
File metadata and controls
93 lines (85 loc) · 2.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "day2.hpp"
#include "util.hpp"
#include <cassert>
#include <cstddef>
#include <string>
#include <vector>
namespace Day2 {
using namespace std;
long solve_day2_pt1(const vector<string> &input) {
const string &line = input[0];
long result = 0;
for (const string &range : util::split(line, ",")) {
const vector<string> tokens = util::split(range, "-");
const long start = stol(tokens[0]);
const long end = stol(tokens[1]);
for (long i = start; i <= end; i++) {
const string str_rep = to_string(i);
const size_t length = str_rep.length();
if ((length % 2) != 0) {
continue;
}
const size_t half = length / 2;
bool is_invalid = true;
for (size_t j = 0; j < half; j++) {
if (str_rep[j] != str_rep[j + half]) {
is_invalid = false;
break;
}
}
if (is_invalid) {
result += i;
}
}
}
return result;
}
vector<size_t> possible_pattern_lengths(const string &number) {
vector<size_t> result;
const size_t length = number.length();
for (size_t i = 1; i <= length / 2; i++) {
if (length % i == 0) {
result.push_back(i);
}
}
return result;
}
bool has_pattern(const string &number, const size_t step) {
assert(step > 0);
const size_t length = number.length();
for (size_t i = 0; i < step; i++) {
const char digit = number[i];
for (size_t j = i + step; j < length; j += step) {
if (number[j] != digit) {
return false;
}
}
}
return true;
}
bool has_pattern(const long value) {
const string str_rep = to_string(value);
const vector<size_t> pattern_lengths = possible_pattern_lengths(str_rep);
for (const size_t step : pattern_lengths) {
if (has_pattern(str_rep, step)) {
return true;
}
}
return false;
}
long solve_day2_pt2(const vector<string> &input) {
const string &line = input[0];
long result = 0;
for (const string &range : util::split(line, ",")) {
const vector<string> tokens = util::split(range, "-");
const long start = stol(tokens[0]);
const long end = stol(tokens[1]);
for (long i = start; i <= end; i++) {
if (has_pattern(i)) {
result += i;
}
}
}
return result;
}
} // namespace Day2