-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2b.cpp
More file actions
115 lines (105 loc) · 2.32 KB
/
Copy pathday2b.cpp
File metadata and controls
115 lines (105 loc) · 2.32 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
115
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
namespace day2b
{
// an invalid id has an even number of digits
// the first half matches the second half
// now you can split it up to n times, where n is the # of digits
// so we need to find the factors of the # of digits
// le epic brute force solution takes ~30 seconds
int main()
{
std::string input0;
//std::ifstream ifile("day2a_sample.txt");
std::ifstream ifile("day2a.txt");
if (ifile.is_open())
{
while (!ifile.eof())
{
std::string line;
std::getline(ifile, line);
input0 += line + '\n';
}
assert(input0.back() == '\n');
input0.pop_back(); // no trailing newline character please
//__debugbreak();
}
else
{
assert(0);
}
std::vector<std::string> values;
std::string temp;
for (char c : input0)
{
if (c == '-' || c == ',')
{
values.push_back(temp);
temp = "";
continue;
}
temp += c;
}
values.push_back(temp);
std::vector<std::pair<uint64_t, uint64_t>> ranges;
for (int i = 0; i < values.size(); i += 2)
{
ranges.emplace_back(
std::stoull(values[i]),
std::stoull(values[i + 1])
);
}
std::vector<uint64_t> ranges2;
for (auto& r : ranges)
{
for (uint64_t i = r.first; i <= r.second; i++)
{
ranges2.push_back(i);
}
}
uint64_t accumulator = 0;
for (auto v : ranges2)
{
std::vector<int> factors;
int digits = std::floor(std::log10(v)) + 1;
for (int i = 1; i < digits; i++)
{
if (digits % i == 0)
{
factors.push_back(i);
}
}
for (int f : factors)
{
std::vector<uint64_t> parts;
uint64_t v_temp = v;
while (v_temp > 0)
{
uint64_t top = v_temp % static_cast<uint64_t>(std::powl(10, f));
parts.push_back(top);
v_temp = v_temp / static_cast<uint64_t>(std::powl(10, f));
}
bool all_equal = true;
assert(parts.size());
for (int i = 0; i < parts.size() - 1; i++) // when size is zero, then zero - 1 is an underflow... cute
{
if (parts[i] != parts[i + 1])
{
all_equal = false;
break;
}
}
if (all_equal)
{
accumulator += v;
break;
}
}
}
__debugbreak();
return 0;
}
}