-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2a.cpp
More file actions
83 lines (75 loc) · 1.6 KB
/
Copy pathday2a.cpp
File metadata and controls
83 lines (75 loc) · 1.6 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
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
namespace day2a
{
// an invalid id has an even number of digits
// the first half matches the second half
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)
{
int digits = std::floor(std::log10(v)) + 1;
if (digits % 2 == 0)
{
uint64_t top = v % static_cast<uint64_t>(std::powl(10, digits / 2));
uint64_t bot = v / static_cast<uint64_t>(std::powl(10, digits / 2));
if (top == bot)
accumulator += v;
}
}
__debugbreak();
return 0;
}
}