-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.cpp
More file actions
114 lines (92 loc) · 2.24 KB
/
day2.cpp
File metadata and controls
114 lines (92 loc) · 2.24 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<string> split(string& s, string& delimiter) {
// Identical to Python's string.split()
vector<string> tokens;
int idx = 0;
string token;
while ((idx = s.find(delimiter)) != string::npos) {
token = s.substr(0, idx);
tokens.push_back(token);
s.erase(0, idx + delimiter.length());
}
tokens.push_back(s);
return tokens;
}
int mainPart1() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ifstream infile("inputs/day2.txt");
if (!infile.is_open()) {
return -1;
}
string in;
infile >> in;
unsigned long long sol = 0;
string COMMA = ",";
string DASH = "-";
vector<string> id_ranges = split(in, COMMA);
for (auto & range: id_ranges) {
vector<string> ids = split(range, DASH);
unsigned long long id_start = stoll(ids[0]);
unsigned long long id_end = stoll(ids[1]);
for (unsigned long long i = id_start; i <= id_end; ++i) {
string as_string = to_string(i);
int size = as_string.size();
if (size % 2 == 0) {
if (as_string.substr(0, (size / 2)) == as_string.substr((size / 2))) {
sol += i;
}
}
}
}
cout << "Solution: " << sol << endl;
return 0;
}
bool isInvalid(string & s, int length) {
int n = s.size();
string prefix = s.substr(0, length);
int end = n / length;
if (n % length != 0) {
end++;
}
for (int i = 1; i < end; i++) {
if (s.substr(i * length, length) != prefix) {
return false;
}
}
return true;
}
int mainPart2() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ifstream infile("inputs/day2.txt");
if (!infile.is_open()) {
return -1;
}
string in;
infile >> in;
unsigned long long sol = 0;
string COMMA = ",";
string DASH = "-";
vector<string> id_ranges = split(in, COMMA);
for (auto & range: id_ranges) {
vector<string> ids = split(range, DASH);
unsigned long long id_start = stoll(ids[0]);
unsigned long long id_end = stoll(ids[1]);
for (unsigned long long i = id_start; i <= id_end; ++i) {
string as_string = to_string(i);
int size = as_string.size();
for (int length = 1; length <= size / 2; ++length) {
if (isInvalid(as_string, length)) {
sol += i;
break; // avoid double counting
}
}
}
}
cout << "Solution: " << sol << endl;
return 0;
}