-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5b.cpp
More file actions
111 lines (101 loc) · 2.1 KB
/
Copy pathday5b.cpp
File metadata and controls
111 lines (101 loc) · 2.1 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
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
#include <algorithm>
namespace day5b
{
int main()
{
std::string input0;
//std::ifstream ifile("day5_sample.txt");
std::ifstream ifile("day5.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 == '\n')
{
values.push_back(temp);
temp = "";
continue;
}
temp += c;
}
values.push_back(temp);
std::vector<std::pair<uint64_t, uint64_t>> data1;
std::vector<uint64_t> data2;
bool phase_two = false;
for (int i = 0; i < values.size(); i++)
{
if (values[i] == "")
phase_two = true;
if (!phase_two)
{
std::istringstream ss{ values[i] };
uint64_t v1;
char c1;
uint64_t v2;
ss >> v1;
ss >> c1;
ss >> v2;
data1.emplace_back(v1, v2);
}
else
{
std::istringstream ss{ values[i] };
uint64_t v1;
ss >> v1;
data2.emplace_back(v1);
}
}
// sort
std::sort(begin(data1), end(data1), [](auto& a, auto& b) -> bool { return a.first < b.first; });
// filter
for (int i = 0; i < data1.size() - 1; i++)
{
auto& curr = data1[i];
auto& next = data1[i + 1];
if (next.first <= curr.second)
{
curr.second = std::max(curr.second, next.second);
data1.erase(begin(data1) + i + 1);
i--;
}
}
// validate
for (int i = 0; i < data1.size() - 1; i++)
{
auto& curr = data1[i];
auto& next = data1[i + 1];
assert(curr.first < next.first);
assert(next.first > curr.second);
}
// accumulate
uint64_t total = 0;
for (int i = 0; i < data1.size(); i++)
{
auto& curr = data1[i];
total += (curr.second - curr.first + 1);
}
__debugbreak();
return 0;
}
}