day02 part2
This commit is contained in:
parent
c3f0c2039e
commit
fb592df019
1 changed files with 63 additions and 0 deletions
63
2024/day02/part2.cc
Normal file
63
2024/day02/part2.cc
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
class report {
|
||||||
|
std::vector<int> levels;
|
||||||
|
public:
|
||||||
|
bool safe();
|
||||||
|
friend std::istream& operator>>(std::istream& stream, report& r);
|
||||||
|
friend std::ostream& operator<<(std::ostream& stream, report& r);
|
||||||
|
};
|
||||||
|
|
||||||
|
std::istream& operator>>(std::istream& stream, report& r) {
|
||||||
|
r.levels.clear();
|
||||||
|
std::string s;
|
||||||
|
std::getline(stream, s, '\n');
|
||||||
|
std::stringstream ss(s);
|
||||||
|
int level;
|
||||||
|
while (ss >> level) {
|
||||||
|
r.levels.emplace_back(level);
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& stream, report& r) {
|
||||||
|
stream << "report{";
|
||||||
|
for (auto elem : r.levels) {
|
||||||
|
stream << elem << ",";
|
||||||
|
}
|
||||||
|
stream << "}";
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool report::safe() {
|
||||||
|
for (size_t index = 0; index < levels.size(); index++) {
|
||||||
|
std::vector<int> tmp(levels);
|
||||||
|
tmp.erase(tmp.begin() + index);
|
||||||
|
if (std::adjacent_find(tmp.begin(), tmp.end(), [](int a, int b) {
|
||||||
|
return a - b < 1 || a - b > 3;
|
||||||
|
}) == tmp.end()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (std::adjacent_find(tmp.begin(), tmp.end(), [](int a, int b) {
|
||||||
|
return b - a < 1 || b - a > 3;
|
||||||
|
}) == tmp.end()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
report r;
|
||||||
|
int count = 0;
|
||||||
|
while (std::cin >> r) {
|
||||||
|
if (r.safe()) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << count << std::endl;
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue