aboutsummaryrefslogtreecommitdiff
path: root/2024/02/day02b.cpp
blob: 2e3dcf40559948cd301b70addb77be9c615cd75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

bool isvalid_v(const vector<int> &v) {
	int b = v[0] > v[1] ? 1 : -1;
	for (unsigned i = 0; i < v.size()-1; i++) {
		int d = b * (v[i] - v[i+1]);
		if (d < 1 || d > 3)
			return false;
	}
	return true;
}

vector<int> getv(const string &line) {
	int x;
	vector<int> ret;
	istringstream s(line);

	while(s >> x)
		ret.push_back(x);

	return ret;
}

bool isvalid(const string &line) {
	vector v0 = getv(line);
	if (isvalid_v(v0))
		return true;
	for (unsigned i = 0; i < v0.size(); i++) {
		vector v = v0;
		v.erase(v.begin() + i);
		if (isvalid_v(v))
			return true;
	}
	return false;
}

int main() {
	int tot = 0;
	for (string line; getline(cin, line); )
		tot += isvalid(line);
	cout << tot << endl;
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net