-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_3.cpp
89 lines (71 loc) · 1.85 KB
/
day_3.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
pair<int, int> parse_mul(string& s, pair<int, int> start_end_idx) {
int start_idx = start_end_idx.first;
int end_idx = start_end_idx.second;
if (start_idx >= s.size() || end_idx >= s.size()) return {-9999, -9999};
string temp = s.substr(start_idx, 4);
if (s[end_idx] != ')' || temp != "mul(") return {-9999, -9999};
int idx = start_idx + 4;
string first, second;
while (idx < end_idx && s[idx] != ',') {
first += s[idx];
idx++;
}
idx++;
while (idx < end_idx && s[idx] != ',') {
second += s[idx];
idx++;
}
if (first.empty() || second.empty()) return {-9999, -9999};
return {stoi(first), stoi(second)};
}
bool is_do(string& s, int begin) {
int end = begin + 3;
if (end >= s.size()) return false;
return s.substr(begin, 4) == "do()";
}
bool is_dont(string& s, int begin) {
int end = begin + 6;
if (end >= s.size()) return false;
return s.substr(begin, 7) == "don't()";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
int first_out = 0;
int second_out = 0;
bool do_op = true;
while(getline(cin, s)) {
for (int i = 0; i < s.size(); i++) {
if (is_do(s, i)) {
do_op = true;
} else if (is_dont(s, i)) {
do_op = false;
}
pair<int, int> t;
int basic_end = i + 7;
for (int j = 0; j <= 4; j++) {
t = parse_mul(s, {i, basic_end + j});
if (t.first != -9999 && t.second != -9999)
break;
}
if (t.first != -9999 && t.second != -9999) {
first_out += t.first * t.second;
if (do_op) {
second_out += t.first * t.second;
}
continue;
}
}
}
cout << first_out << '\n';
cout << second_out << '\n';
return 0;
}