Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <sstream>
4 : : #include <vector>
5 : : #include <unordered_map>
6 : : #include <unordered_set>
7 : : #include <algorithm>
8 : :
9 : : using namespace std;
10 : :
11 : 2 : int main() {
12 : 2 : ifstream inputFile("input.txt");
13 [ + + ]: 2 : if (!inputFile) {
14 : 1 : cerr << "Error opening file." << endl;
15 : 1 : return 1;
16 : : }
17 : :
18 : 1 : unordered_map<int, unordered_set<int>> rules;
19 : :
20 [ + - + + : 1177 : for (string line; getline(inputFile, line) && line.contains('|');) {
+ + ]
21 : : int from;
22 : : int to;
23 : : char delim;
24 : 1176 : stringstream(line) >> from >> delim >> to;
25 : 1176 : rules[from].insert(to);
26 : 1 : }
27 : :
28 : 1 : int middleSum = 0;
29 [ + + ]: 201 : for (string line; getline(inputFile, line);) {
30 : 200 : stringstream ss(line);
31 : 200 : vector<int> seq;
32 : 200 : unordered_set<int> pos;
33 : :
34 : 600 : middleSum += [&rules, &ss, &seq, &pos]() {
35 [ + + ]: 1758 : for (int n; ss >> n; ss.ignore(1, ',')) {
36 [ + + ]: 40390 : if (!ranges::all_of(rules[n], [&pos](int to) { return !pos.contains(to); })) {
37 : 110 : return 0;
38 : : }
39 : 1558 : seq.push_back(n);
40 : 1558 : pos.insert(n);
41 : : }
42 : 90 : return seq[seq.size() / 2];
43 : 200 : }();
44 : 201 : }
45 : :
46 : 1 : cout << middleSum << endl;
47 [ + + ]: 3 : }
|