Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <sstream>
5 : : #include <cmath>
6 : : #include <iterator>
7 : : using namespace std;
8 : :
9 : 2 : int main() {
10 : 2 : ifstream file("input.txt");
11 [ + + ]: 2 : if (!file) {
12 : 1 : cerr << "Error: Could not open file 'input.txt'" << endl;
13 : 1 : return 1;
14 : : }
15 : :
16 : 1000 : auto isValid = [](const vector<int>& nums) {
17 : 1000 : bool wrongSign = (nums[1] - nums[0]) > 0;
18 [ + + ]: 4307 : for (size_t i = 1; i < nums.size(); ++i) {
19 : 3773 : int diff = nums[i] - nums[i - 1];
20 [ + + + + : 3773 : if (abs(diff) < 1 || abs(diff) > 3 || signbit(diff) == wrongSign) {
+ + + + ]
21 : 466 : return false;
22 : : }
23 : : }
24 : 534 : return true;
25 : : };
26 : :
27 : 1 : int validCount = 0;
28 : :
29 [ + + ]: 1001 : for (string line; getline(file, line);) {
30 : 1000 : istringstream iss(line);
31 : 2000 : vector<int> nums{ istream_iterator<int>(iss), {} };
32 [ + + ]: 1000 : if (isValid(nums)) {
33 : 534 : ++validCount;
34 : : }
35 : 1001 : }
36 : :
37 : 1 : cout << validCount << endl;
38 [ + + ]: 3 : }
|