diff options
Diffstat (limited to '2024/src')
| -rw-r--r-- | 2024/src/day05.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/2024/src/day05.cpp b/2024/src/day05.cpp new file mode 100644 index 0000000..37d74c6 --- /dev/null +++ b/2024/src/day05.cpp | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <numeric> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<long> | ||
| 11 | split(const string& line, char sep = ',') | ||
| 12 | { | ||
| 13 | vector<long> parts; | ||
| 14 | stringstream input{ line }; | ||
| 15 | |||
| 16 | for ( string part; getline(input, part, sep); ) { | ||
| 17 | parts.emplace_back(stol(part)); | ||
| 18 | } | ||
| 19 | |||
| 20 | return parts; | ||
| 21 | } | ||
| 22 | |||
| 23 | tuple<vector<tuple<long, long>>, vector<vector<long>>> | ||
| 24 | read_file(string_view filename) | ||
| 25 | { | ||
| 26 | fstream input{ filename }; | ||
| 27 | |||
| 28 | vector<tuple<long, long>> rules; | ||
| 29 | for ( string line; getline(input, line); ) { | ||
| 30 | if ( line.empty() ) { | ||
| 31 | break; | ||
| 32 | } | ||
| 33 | long lhs = 0; | ||
| 34 | long rhs = 0; | ||
| 35 | sscanf(line.c_str(), "%ld|%ld", &lhs, &rhs); // NOLINT | ||
| 36 | rules.emplace_back(lhs, rhs); | ||
| 37 | } | ||
| 38 | |||
| 39 | vector<vector<long>> pages; | ||
| 40 | for ( string line; getline(input, line); ) { | ||
| 41 | pages.emplace_back(split(line)); | ||
| 42 | } | ||
| 43 | return { rules, pages }; | ||
| 44 | } | ||
| 45 | |||
| 46 | bool | ||
| 47 | is_valid(const vector<tuple<long, long>>& rules, const vector<long>& page) | ||
| 48 | { | ||
| 49 | set<long> processed; | ||
| 50 | |||
| 51 | bool invalid = false; | ||
| 52 | for ( const auto& num: page ) { | ||
| 53 | processed.emplace(num); | ||
| 54 | |||
| 55 | auto pred = [num](const tuple<long, long>& rule) { | ||
| 56 | return get<0>(rule) == num; | ||
| 57 | }; | ||
| 58 | |||
| 59 | for ( auto it = find_if(rules.begin(), rules.end(), pred); it != rules.end(); it = find_if(it + 1, rules.end(), pred) ) { | ||
| 60 | const auto foo = get<1>(*it); | ||
| 61 | |||
| 62 | if ( processed.contains(foo) ) { | ||
| 63 | invalid = true; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return !invalid; | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | part1(const vector<tuple<long, long>>& rules, const vector<vector<long>>& pages) | ||
| 72 | { | ||
| 73 | long sum = 0; | ||
| 74 | for ( const auto& page: pages ) { | ||
| 75 | if ( is_valid(rules, page) ) { | ||
| 76 | sum += page.at(page.size() / 2); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | cout << sum << endl; | ||
| 80 | } | ||
| 81 | |||
| 82 | void | ||
| 83 | part2(const vector<tuple<long, long>>& rules, const vector<vector<long>>& pages) | ||
| 84 | { | ||
| 85 | long sum = 0; | ||
| 86 | for ( auto page: pages ) { | ||
| 87 | if ( !is_valid(rules, page) ) { | ||
| 88 | ranges::sort(page, [&](auto lhs, auto rhs) -> bool { | ||
| 89 | return ranges::find(rules, make_tuple(lhs, rhs)) != rules.end(); | ||
| 90 | }); | ||
| 91 | |||
| 92 | sum += page.at(page.size() / 2); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | cout << sum << endl; | ||
| 96 | } | ||
| 97 | |||
| 98 | int | ||
| 99 | main() | ||
| 100 | { | ||
| 101 | auto [rules, pages] = read_file("data/day05.txt"); | ||
| 102 | part1(rules, pages); | ||
| 103 | part2(rules, pages); | ||
| 104 | } | ||
