Advent of Code 2022 - 2nd attempt in c++
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.
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <array>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
struct Range {
|
|
|
|
unsigned min;
|
|
|
|
unsigned max;
|
|
|
|
|
|
|
|
bool contains(const Range &in) const {
|
|
|
|
return min <= in.min && max >= in.max;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool overlaps(const Range &in) const {
|
|
|
|
return min <= in.max && in.min <= max;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef vector<array<Range, 2>> Input;
|
|
|
|
|
|
|
|
Input parse()
|
|
|
|
{
|
|
|
|
Input data;
|
|
|
|
for (string line; getline(cin, line);) {
|
|
|
|
array<Range, 2> entry;
|
|
|
|
sscanf(line.c_str(), "%u-%u,%u-%u",
|
|
|
|
&entry[0].min, &entry[0].max,
|
|
|
|
&entry[1].min, &entry[1].max);
|
|
|
|
data.push_back(entry);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned p1(const Input &input)
|
|
|
|
{
|
|
|
|
return count_if(input.begin(), input.end(), [](decltype(input[0]) i){
|
|
|
|
return i[0].contains(i[1]) || i[1].contains(i[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned p2(const Input &input)
|
|
|
|
{
|
|
|
|
return count_if(input.begin(), input.end(), [](decltype(input[0]) i){
|
|
|
|
return i[0].overlaps(i[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
auto input = parse();
|
|
|
|
cout << p1(input) << endl;
|
|
|
|
cout << p2(input) << endl;
|
|
|
|
}
|