1
0
Fork 0
This commit is contained in:
Marcus Penate 2022-12-04 01:28:35 -05:00
parent 63db42f55a
commit 78e28f6959
2 changed files with 65 additions and 0 deletions

33
Day4/part1.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
#include <algorithm>
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;
}

32
Day4/part2.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <string>
#include <algorithm>
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;
}