You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AOC2022/04/solution.cpp

84 lines
1.9 KiB

#include <iostream>
#include <fstream>
#include <vector>
#include <regex>
#include <sstream>
using namespace std;
regex inputAStart {"\\d*-"};
regex inputAEnd {"-\\d*,"};
std::vector<int> getNumbersFromString(const std::string& str)
{
std::vector<int> numbers;
std::stringstream ss(str);
int number;
while (ss >> number)
numbers.push_back(number);
return numbers;
}
void parseLine(string input, int* aStart, int* aEnd, int* bStart, int* bEnd)
{
smatch m;
regex_search(input, m, inputAStart);
string output = m[0].str();
output.pop_back();
*aStart = stoi(output);
regex_search(input, m, inputAEnd);
output = m[0].str();
output.pop_back();
output.erase(0,1);
*aEnd = stoi(output);
regex_search(input, m, regex(",\\d*"));
output = m[0].str();
output.erase(0,1);
*bStart = stoi(output);
regex_search(input, m, regex("-\\d+$"));
output = m.str();
output.erase(0,1);
*bEnd = stoi(output);
}
bool isSubSet(int as, int ae, int bs, int be)
{
bool aInB = (as >= bs & ae <= be);
bool bInA = (bs >= as & be <= ae);
return (aInB | bInA);
}
bool isOverlapped(int as, int ae, int bs, int be)
{
return ((as >= bs & as <= be) | (bs >= as & bs <= ae));
}
int main(int argc, char *argv[])
{
string inputStr;
ifstream inputFile(argv[1]);
int pairs {0};
int overlaps {0};
while (getline(inputFile, inputStr))
{
cout << endl << inputStr;
int eAStart, eAEnd, eBStart, eBEnd;
parseLine(inputStr, &eAStart, &eAEnd, &eBStart, &eBEnd);
if (isSubSet(eAStart, eAEnd, eBStart, eBEnd))
{
cout << "\tMATCHES\tOVERLAPPED";
pairs++;
overlaps++;
}
else if (isOverlapped(eAStart, eAEnd, eBStart, eBEnd))
{
cout << "\tOVERLAPPED";
overlaps++;
}
}
cout << "\n\nPairs: " << pairs << "\nOverlaps: " << overlaps<< endl;
}