From 4953706f31ac155790183c3d32c46c8a94f3bdce Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Mon, 1 Sep 2025 04:46:41 +0300 Subject: day03: done --- day03/solution.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 day03/solution.cpp (limited to 'day03/solution.cpp') 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 @@ +#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; +} -- cgit v1.2.3