diff options
Diffstat (limited to 'day03/solution.cpp')
| -rw-r--r-- | day03/solution.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/day03/solution.cpp b/day03/solution.cpp new file mode 100644 index 0000000..4ddbe07 --- /dev/null +++ b/day03/solution.cpp | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #include <iterator> | ||
| 2 | #include <print> | ||
| 3 | #include <iostream> | ||
| 4 | #include <ranges> | ||
| 5 | #include <regex> | ||
| 6 | #include <sstream> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | #include <map> | ||
| 10 | #include <unordered_map> | ||
| 11 | #include <set> | ||
| 12 | #include <unordered_set> | ||
| 13 | #include <numeric> | ||
| 14 | |||
| 15 | namespace views = std::views; | ||
| 16 | namespace ranges = std::ranges; | ||
| 17 | |||
| 18 | const auto parse_input() { | ||
| 19 | return std::string(std::istream_iterator<char>(std::cin), | ||
| 20 | std::istream_iterator<char>()); | ||
| 21 | } | ||
| 22 | |||
| 23 | void part1(const auto &input) { | ||
| 24 | uint64_t answer{0}; | ||
| 25 | std::regex re{"mul\\(([0-9]+),([0-9]+)\\)"}; | ||
| 26 | const auto begin = std::sregex_iterator(input.begin(), input.end(), re); | ||
| 27 | const auto end = std::sregex_iterator(); | ||
| 28 | |||
| 29 | for (auto it = begin; it != end; ++it) { | ||
| 30 | const std::smatch match{*it}; | ||
| 31 | answer += std::stoi(match[1].str()) * std::stoi(match[2].str()); | ||
| 32 | } | ||
| 33 | |||
| 34 | std::println("{}", answer); | ||
| 35 | } | ||
| 36 | |||
| 37 | void part2(const auto &input) { | ||
| 38 | uint64_t answer{0}; | ||
| 39 | std::regex re{"do\\(\\)|don't\\(\\)|mul\\(([0-9]+),([0-9]+)\\)"}; | ||
| 40 | const auto begin = std::sregex_iterator(input.begin(), input.end(), re); | ||
| 41 | const auto end = std::sregex_iterator(); | ||
| 42 | bool toggled = true; | ||
| 43 | |||
| 44 | for (auto it = begin; it != end; ++it) { | ||
| 45 | const std::smatch match{*it}; | ||
| 46 | if (match.str() == "do()") { | ||
| 47 | toggled = true; | ||
| 48 | } else if (match.str() == "don't()") { | ||
| 49 | toggled = false; | ||
| 50 | } else if (toggled) { | ||
| 51 | answer += std::stoi(match[1].str()) * std::stoi(match[2].str()); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | std::println("{}", answer); | ||
| 56 | } | ||
| 57 | |||
| 58 | int main() { | ||
| 59 | const auto input = parse_input(); | ||
| 60 | |||
| 61 | #ifndef NO_PART1 | ||
| 62 | part1(input); | ||
| 63 | #endif | ||
| 64 | |||
| 65 | #ifndef NO_PART2 | ||
| 66 | part2(input); | ||
| 67 | #endif | ||
| 68 | return 0; | ||
| 69 | } | ||
