Day 4 complete

master
Griffiths Lott 3 years ago
parent 1f87f1bbde
commit 90363bd9b2
  1. 5
      04/.vscode/settings.json
  2. BIN
      04/answer
  3. 1000
      04/input.txt
  4. 84
      04/solution.cpp
  5. 6
      04/test_input.txt

@ -0,0 +1,5 @@
{
"files.associations": {
"ostream": "cpp"
}
}

Binary file not shown.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,84 @@
#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;
}

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
Loading…
Cancel
Save