day02 part1
This commit is contained in:
parent
214a1b62d9
commit
c3f0c2039e
2 changed files with 1054 additions and 0 deletions
1000
2024/day02/input
Normal file
1000
2024/day02/input
Normal file
File diff suppressed because it is too large
Load diff
54
2024/day02/part1.cc
Normal file
54
2024/day02/part1.cc
Normal file
|
@ -0,0 +1,54 @@
|
|||
#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() {
|
||||
return std::adjacent_find(levels.begin(), levels.end(), [](int a, int b) {
|
||||
return a - b < 1 || a - b > 3;
|
||||
}) == levels.end()
|
||||
|| std::adjacent_find(levels.begin(), levels.end(), [](int a, int b) {
|
||||
return b - a < 1 || b - a > 3;
|
||||
}) == levels.end();
|
||||
}
|
||||
|
||||
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