aboutsummaryrefslogtreecommitdiff
path: root/2024/05/day05a.cpp
blob: be2037efcfb7b93b25ebfddee00140622b57e2d4 (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
49
50
51
52
53
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

vector<int> readints(string &s) {
	vector<int> v;
	stringstream sin(s);
	int x;

	while (sin >> x)
		v.push_back(x);

	return v;
}

bool valid(vector<int> &v, vector<pair<int, int>> &rules) {
	for (unsigned i = 0; i < v.size(); i++)
		for (unsigned j = i+1; j < v.size(); j++)
			for (auto r : rules)
				if (v[i] == r.second && v[j] == r.first)
					return false;
	return true;
}

int main() {
	string line;
	vector<pair<int, int>> rules;
	while (getline(cin, line)) {
		if (line == "")
			break;

		replace(line.begin(), line.end(), '|', ' ');
		auto v = readints(line);
		int x = v[0];
		int y = v[1];
		rules.push_back(make_pair(x, y));
	}


	int tot = 0;
	while (getline(cin, line)) {
		replace(line.begin(), line.end(), ',', ' ');
		auto v = readints(line);
		if (valid(v, rules))
			tot += v[v.size()/2];
	}
	cout << tot << endl;
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net