diff --git a/Day4/part1.cpp b/Day4/part1.cpp new file mode 100644 index 0000000..4e6b17a --- /dev/null +++ b/Day4/part1.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + int total = 0; + + string line; + while(getline(cin, line)) + { + string::size_type n = line.find(','); + string half1 = line.substr(0,n); + string half2 = line.substr(n+1); + + n = half1.find('-'); + int half1_start = stoi(half1.substr(0,n)); + int half1_end = stoi(half1.substr(n+1)); + + n = half2.find('-'); + int half2_start = stoi(half2.substr(0,n)); + int half2_end = stoi(half2.substr(n+1)); + + if ((half1_start <= half2_start && half1_end >= half2_end) || + (half2_start <= half1_start && half2_end >= half1_end)) + { + total++; + } + } + cout << total << endl; +} \ No newline at end of file diff --git a/Day4/part2.cpp b/Day4/part2.cpp new file mode 100644 index 0000000..dca6806 --- /dev/null +++ b/Day4/part2.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + int total = 0; + + string line; + while(getline(cin, line)) + { + string::size_type n = line.find(','); + string half1 = line.substr(0,n); + string half2 = line.substr(n+1); + + n = half1.find('-'); + int half1_start = stoi(half1.substr(0,n)); + int half1_end = stoi(half1.substr(n+1)); + + n = half2.find('-'); + int half2_start = stoi(half2.substr(0,n)); + int half2_end = stoi(half2.substr(n+1)); + + if (!(half1_end < half2_start || half2_end < half1_start)) + { + total++; + } + } + cout << total << endl; +} \ No newline at end of file