diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-02 06:46:28 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-02 06:46:28 +0100 |
| commit | 5a23d15ff9bb69d9e9b52e543596e75af3b090f9 (patch) | |
| tree | c89e8120d48962c77dd74144616a8b6982f8822b /2024/02/day02a.cpp | |
| parent | 0aefd410a18acf826af5187cdb038fd9b4f25fc5 (diff) | |
| download | aoc-5a23d15ff9bb69d9e9b52e543596e75af3b090f9.tar.gz aoc-5a23d15ff9bb69d9e9b52e543596e75af3b090f9.zip | |
Day 2 2024
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 | } | ||
