diff options
Diffstat (limited to '2024/02/day02a.cpp')
| -rw-r--r-- | 2024/02/day02a.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/2024/02/day02a.cpp b/2024/02/day02a.cpp new file mode 100644 index 0000000..cdd9efa --- /dev/null +++ b/2024/02/day02a.cpp | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <sstream> | ||
| 3 | #include <vector> | ||
| 4 | #include <algorithm> | ||
| 5 | #include <string> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | bool isvalid(const string &line) { | ||
| 9 | int x, y, b; | ||
| 10 | istringstream s(line); | ||
| 11 | |||
| 12 | s >> x >> y; | ||
| 13 | b = x > y ? 1 : -1; | ||
| 14 | do { | ||
| 15 | int d = b * (x-y); | ||
| 16 | if (d < 1 || d > 3) | ||
| 17 | return false; | ||
| 18 | x = y; | ||
| 19 | } while (s >> y); | ||
| 20 | |||
| 21 | return true; | ||
| 22 | } | ||
| 23 | |||
| 24 | int main() { | ||
| 25 | int tot = 0; | ||
| 26 | for (string line; getline(cin, line); ) | ||
| 27 | tot += isvalid(line); | ||
| 28 | cout << tot << endl; | ||
| 29 | return 0; | ||
| 30 | } | ||
