aboutsummaryrefslogtreecommitdiff
path: root/2024/05/day05a.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2024/05/day05a.cpp')
-rw-r--r--2024/05/day05a.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/2024/05/day05a.cpp b/2024/05/day05a.cpp
new file mode 100644
index 0000000..be2037e
--- /dev/null
+++ b/2024/05/day05a.cpp
@@ -0,0 +1,53 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9vector<int> readints(string &s) {
10 vector<int> v;
11 stringstream sin(s);
12 int x;
13
14 while (sin >> x)
15 v.push_back(x);
16
17 return v;
18}
19
20bool valid(vector<int> &v, vector<pair<int, int>> &rules) {
21 for (unsigned i = 0; i < v.size(); i++)
22 for (unsigned j = i+1; j < v.size(); j++)
23 for (auto r : rules)
24 if (v[i] == r.second && v[j] == r.first)
25 return false;
26 return true;
27}
28
29int main() {
30 string line;
31 vector<pair<int, int>> rules;
32 while (getline(cin, line)) {
33 if (line == "")
34 break;
35
36 replace(line.begin(), line.end(), '|', ' ');
37 auto v = readints(line);
38 int x = v[0];
39 int y = v[1];
40 rules.push_back(make_pair(x, y));
41 }
42
43
44 int tot = 0;
45 while (getline(cin, line)) {
46 replace(line.begin(), line.end(), ',', ' ');
47 auto v = readints(line);
48 if (valid(v, rules))
49 tot += v[v.size()/2];
50 }
51 cout << tot << endl;
52 return 0;
53}

Generated with cgit - Back to sebastiano.tronto.net