-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_05a.cpp
53 lines (50 loc) · 1.67 KB
/
day_05a.cpp
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 <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <unordered_map>
#include <vector>
int main(int argc, char* argv[]) {
std::string input = "../input/day_05_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
long long answer = 0;
std::unordered_map<int, std::vector<int>> rules;
std::vector<std::vector<int>> updates;
while(std::getline(file, line)) {
if (line.empty()) break;
const auto idx = line.find('|');
rules[std::stoi(line.substr(0, idx))].push_back(std::stoi(line.substr(idx + 1, line.size() - idx - 1)));
}
while(std::getline(file, line)) {
std::size_t current = 0;
std::size_t comma_idx = line.find(',');
updates.emplace_back();
auto& update = updates.back();
while(comma_idx != std::string::npos) {
update.push_back(std::stoi(line.substr(current, comma_idx - current)));
current = comma_idx + 1;
comma_idx = line.find(',', current);
}
update.push_back(std::stoi(line.substr(current, line.size() - current)));
}
for (auto& update : updates) {
bool follows_rules = true;
for (int i = 0; i < update.size() && follows_rules; i++) {
for (int j = i+1; j < update.size() && follows_rules; j++) {
if (std::ranges::contains(rules[update[j]], update[i])) {
follows_rules = false;
}
}
}
if (follows_rules) {
answer += update[update.size() / 2];
}
}
std::cout << answer << '\n';
return 0;
}