-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_04b.cpp
40 lines (36 loc) · 1.18 KB
/
day_04b.cpp
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
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
std::tuple<std::string, std::string> split(const std::string& str, const char delim) {
const auto delim_i = str.find(delim);
const auto s1 = str.substr(0, delim_i);
const auto s2 = str.substr(delim_i + 1, str.size() - delim_i);
return {s1, s2};
}
std::tuple<int, int> split_with_int(const std::string& str, const char delim) {
const auto [s1, s2] = split(str, delim);
return {std::stoi(s1), std::stoi(s2)};
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_04_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
int total = 0;
while(std::getline(file, line)) {
// Account for CRLF if required
// line.erase(std::remove_if(std::begin(line), std::end(line), [](auto c) { return !isprint(c); }), std::end(line));
const auto [e1, e2] = split(line, ',');
const auto [min_1, max_1] = split_with_int(e1, '-');
const auto [min_2, max_2] = split_with_int(e2, '-');
if ((min_1 >= min_2 && min_1 <= max_2) ||
(min_1 <= min_2 && max_1 >= min_2)) {
total += 1;
}
}
std::cout << total << '\n';
return 0;
}