blob: 4ddbe07757578fce9bf19e2e7598dca3ddff4e61 (
plain) (
blame)
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 <iterator>
#include <print>
#include <iostream>
#include <ranges>
#include <regex>
#include <sstream>
#include <utility>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <numeric>
namespace views = std::views;
namespace ranges = std::ranges;
const auto parse_input() {
return std::string(std::istream_iterator<char>(std::cin),
std::istream_iterator<char>());
}
void part1(const auto &input) {
uint64_t answer{0};
std::regex re{"mul\\(([0-9]+),([0-9]+)\\)"};
const auto begin = std::sregex_iterator(input.begin(), input.end(), re);
const auto end = std::sregex_iterator();
for (auto it = begin; it != end; ++it) {
const std::smatch match{*it};
answer += std::stoi(match[1].str()) * std::stoi(match[2].str());
}
std::println("{}", answer);
}
void part2(const auto &input) {
uint64_t answer{0};
std::regex re{"do\\(\\)|don't\\(\\)|mul\\(([0-9]+),([0-9]+)\\)"};
const auto begin = std::sregex_iterator(input.begin(), input.end(), re);
const auto end = std::sregex_iterator();
bool toggled = true;
for (auto it = begin; it != end; ++it) {
const std::smatch match{*it};
if (match.str() == "do()") {
toggled = true;
} else if (match.str() == "don't()") {
toggled = false;
} else if (toggled) {
answer += std::stoi(match[1].str()) * std::stoi(match[2].str());
}
}
std::println("{}", answer);
}
int main() {
const auto input = parse_input();
#ifndef NO_PART1
part1(input);
#endif
#ifndef NO_PART2
part2(input);
#endif
return 0;
}
|