-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.cpp
52 lines (41 loc) · 1.31 KB
/
day3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
int main() {
// Get input
string input; // removes newlines before pasting into terminal
string line;
while (getline(cin, line) && !line.empty()) {
input += line;
}
// Part 1
regex mulPattern(R"(mul\((\d+),(\d+)\))");
smatch match;
auto begin = input.cbegin();
auto end = input.cend();
long long result = 0;
while (regex_search(begin, end, match, mulPattern)) {
int x = stoi(match[1].str());
int y = stoi(match[2].str());
result += x * y;
begin = match.suffix().first; // move to the next match
}
cout << result << endl;
// Day 2
begin = input.cbegin();
regex mulDoDontPattern(R"(mul\((\d+),(\d+)\)|do\(\)|don't\(\))");
result = 0;
bool doFlag = true; // do is a fucking reserved keyword lol
while (regex_search(begin, end, match, mulDoDontPattern)) {
if (match[1].matched && doFlag) {
int x = stoi(match[1].str());
int y = stoi(match[2].str());
result += x * y;
} else if (match[0].str() == "do()") {
doFlag = true;
} else if (match[0].str() == "don't()") {
doFlag = false;
}
begin = match.suffix().first; // move to the next match
}
cout << result << endl;
}