summaryrefslogtreecommitdiffstats
path: root/day03/solution.cpp
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2025-09-01 04:46:41 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2025-09-01 04:51:37 +0300
commit4953706f31ac155790183c3d32c46c8a94f3bdce (patch)
tree1a41f0e96f08487d4bc1cf612a9b24d54e3c46af /day03/solution.cpp
parentAdd .editorconfig (diff)
downloadaoc24-4953706f31ac155790183c3d32c46c8a94f3bdce.tar.gz
aoc24-4953706f31ac155790183c3d32c46c8a94f3bdce.zip
day03: done
Diffstat (limited to 'day03/solution.cpp')
-rw-r--r--day03/solution.cpp69
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
15namespace views = std::views;
16namespace ranges = std::ranges;
17
18const auto parse_input() {
19 return std::string(std::istream_iterator<char>(std::cin),
20 std::istream_iterator<char>());
21}
22
23void 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
37void 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
58int 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}