-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.cpp
More file actions
97 lines (82 loc) · 2.14 KB
/
day5.cpp
File metadata and controls
97 lines (82 loc) · 2.14 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
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
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/day5.txt");
if (!infile.is_open()) {
return -1;
}
string DASH = "-";
string in;
vector<vector<long>> ranges;
while (getline(infile, in) && in != "") {
// cout << "In: " << in << endl;
vector<string> range = split(in, DASH);
ranges.push_back({stol(range[0]), stol(range[1])});
}
sort(ranges.begin(), ranges.end(), [](auto & a, auto & b) { return a[0] < b[0]; });
int n = ranges.size();
int sol = 0;
while (infile >> in) {
long input = stol(in);
for (int i = 0; i < n; ++i) {
if (input >= ranges[i][0] && input <= ranges[i][1]) {
sol++;
break;
}
}
}
cout << "Solution: " << sol << endl;
return 0;
}
int mainPart2() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ifstream infile("inputs/day5.txt");
if (!infile.is_open()) {
return -1;
}
string DASH = "-";
string in;
vector<vector<long>> ranges;
while (getline(infile, in) && in != "") {
vector<string> range = split(in, DASH);
ranges.push_back({stol(range[0]), stol(range[1])});
}
sort(ranges.begin(), ranges.end(), [](auto & a, auto & b) { return a[0] < b[0]; });
int n = ranges.size();
vector<vector<long>> merged_intervals;
merged_intervals.push_back(ranges[0]);
for (int i = 1; i < n; ++i) {
if (ranges[i][0] <= merged_intervals.back()[1]) {
vector<long> back = merged_intervals.back();
merged_intervals.pop_back();
merged_intervals.push_back({min(back[0], ranges[i][0]), max(back[1], ranges[i][1])});
} else {
merged_intervals.push_back(ranges[i]);
}
}
long sol = 0;
for (auto interval: merged_intervals) {
sol += (interval[1] - interval[0] + 1);
}
cout << "Solution: " << sol << endl;
return 0;
}