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.

57 lines
1.1 KiB

2 years ago
#include <iostream>
#include <vector>
#include <array>
using namespace std;
class Range {
public:
unsigned min;
unsigned max;
bool contains(const Range &in) {
return min <= in.min && max >= in.max;
}
bool overlaps(const Range &in) {
return (min <= in.min && in.min <= max)
2 years ago
|| (in.min <= min && min <= in.max);
2 years ago
}
};
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)
{
unsigned count = 0;
for (auto i : input) if (i[0].contains(i[1]) || i[1].contains(i[0])) count++;
return count;
}
unsigned p2(const Input &input)
{
unsigned count = 0;
for (auto i : input) if (i[0].overlaps(i[1])) count++;
return count;
}
int main()
{
Input input = parse();
cout << p1(input) << endl;
cout << p2(input) << endl;
}