#include #include #include #include #include #include #include #include #include #include #include #include #include namespace views = std::views; namespace ranges = std::ranges; const auto parse_input() { return std::string(std::istream_iterator(std::cin), std::istream_iterator()); } 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; }