diff options
Diffstat (limited to '2024/02/day02b.cpp')
| -rw-r--r-- | 2024/02/day02b.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/2024/02/day02b.cpp b/2024/02/day02b.cpp new file mode 100644 index 0000000..2e3dcf4 --- /dev/null +++ b/2024/02/day02b.cpp | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <sstream> | ||
| 3 | #include <vector> | ||
| 4 | #include <algorithm> | ||
| 5 | #include <string> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | bool isvalid_v(const vector<int> &v) { | ||
| 9 | int b = v[0] > v[1] ? 1 : -1; | ||
| 10 | for (unsigned i = 0; i < v.size()-1; i++) { | ||
| 11 | int d = b * (v[i] - v[i+1]); | ||
| 12 | if (d < 1 || d > 3) | ||
| 13 | return false; | ||
| 14 | } | ||
| 15 | return true; | ||
| 16 | } | ||
| 17 | |||
| 18 | vector<int> getv(const string &line) { | ||
| 19 | int x; | ||
| 20 | vector<int> ret; | ||
| 21 | istringstream s(line); | ||
| 22 | |||
| 23 | while(s >> x) | ||
| 24 | ret.push_back(x); | ||
| 25 | |||
| 26 | return ret; | ||
| 27 | } | ||
| 28 | |||
| 29 | bool isvalid(const string &line) { | ||
| 30 | vector v0 = getv(line); | ||
| 31 | if (isvalid_v(v0)) | ||
| 32 | return true; | ||
| 33 | for (unsigned i = 0; i < v0.size(); i++) { | ||
| 34 | vector v = v0; | ||
| 35 | v.erase(v.begin() + i); | ||
| 36 | if (isvalid_v(v)) | ||
| 37 | return true; | ||
| 38 | } | ||
| 39 | return false; | ||
| 40 | } | ||
| 41 | |||
| 42 | int main() { | ||
| 43 | int tot = 0; | ||
| 44 | for (string line; getline(cin, line); ) | ||
| 45 | tot += isvalid(line); | ||
| 46 | cout << tot << endl; | ||
| 47 | return 0; | ||
| 48 | } | ||
