-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_16a.cpp
69 lines (64 loc) · 1.89 KB
/
day_16a.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
struct Rule {
std::string name;
int min_1;
int max_1;
int min_2;
int max_2;
bool isValid(const int val) const {
return (val >= min_1 && val <= max_1) || (val >= min_2 && val <= max_2);
}
};
int main() {
std::ifstream file{"../input/day_16_input"};
std::string line;
// tore rules
std::vector<Rule> rules;
const std::regex rule_pattern(
R"(([a-zA-Z ]+): (\d+)-(\d+) or (\d+)-(\d+)(.*))");
while (std::getline(file, line)) {
if (line == "") break;
std::smatch rule_match;
std::regex_match(line, rule_match, rule_pattern);
Rule new_rule{rule_match[1], std::stoi(rule_match[2]),
std::stoi(rule_match[3]), std::stoi(rule_match[4]),
std::stoi(rule_match[5])};
rules.emplace_back(std::move(new_rule));
}
// store ticekts
const std::regex ticket_pattern(R"((\d+),?)");
std::vector<std::vector<int>> tickets;
while (std::getline(file, line)) {
if (auto it = std::remove_if(std::begin(line), std::end(line),
[](const char c) { return !isprint(c); });
it != std::end(line)) {
line.erase(it);
}
auto start =
std::sregex_iterator(std::begin(line), std::end(line), ticket_pattern);
const auto end = std::sregex_iterator();
std::vector<int> fields;
while (start != end) {
fields.push_back(std::stoi(std::smatch(*start).str()));
start++;
}
tickets.emplace_back(std::move(fields));
}
// check whether invalid and add to sum
int sum = 0;
for (const auto& ticket : tickets) {
for (const auto val : ticket) {
if (!std::any_of(std::begin(rules), std::end(rules),
[=](const auto& rule) { return rule.isValid(val); })) {
sum += val;
}
}
}
std::cout << sum << '\n';
return sum;
}