Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <regex>
4 : : #include <string>
5 : : #include <iterator>
6 : : #include <cstdint>
7 : : using namespace std;
8 : :
9 : 2 : int main() {
10 : 2 : ifstream file("input.txt");
11 [ + + ]: 2 : if (!file) {
12 : 1 : cerr << "Error: Could not open input file.\n";
13 : 1 : return 1;
14 : : }
15 : 1 : string content((istreambuf_iterator<char>(file)), {});
16 : 1 : const regex r(R"(do\(\)|don't\(\)|mul\((\d{1,3}),(\d{1,3})\))");
17 : 1 : uint64_t sum = 0;
18 : 1 : bool enable = true;
19 : :
20 [ + + ]: 812 : for (sregex_iterator it(content.begin(), content.end(), r), end; it != end; ++it) {
21 [ + + ]: 811 : if (const string command = (*it).str(); command == "do()") {
22 : 38 : enable = true;
23 [ + + ]: 773 : } else if (command == "don't()") {
24 : 36 : enable = false;
25 : 811 : }
26 [ + + + + : 811 : sum += enable && (*it)[1].matched ? stoi((*it)[1]) * stoi((*it)[2]) : 0;
+ + + + ]
27 : 1 : }
28 : :
29 : 1 : cout << sum;
30 [ + + ]: 3 : }
|