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