diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-05 08:02:45 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-05 08:02:45 +0100 |
| commit | a937350ab20adc0f8172c4d99f4d711e92dfd947 (patch) | |
| tree | 77db9835272d828adbe0ea9f9c3989dff9c9eb5c /2024/05/day05a.cpp | |
| parent | 59892b65a9810044ca7f135e39c8d44094224c85 (diff) | |
| download | aoc-a937350ab20adc0f8172c4d99f4d711e92dfd947.tar.gz aoc-a937350ab20adc0f8172c4d99f4d711e92dfd947.zip | |
Day 5 2024
Diffstat (limited to '2024/05/day05a.cpp')
| -rw-r--r-- | 2024/05/day05a.cpp | 53 |
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> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | vector<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 | |||
| 20 | bool 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 | |||
| 29 | int 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 | } | ||
