This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_string_solution.cpp
More file actions
86 lines (80 loc) · 2.23 KB
/
raw_string_solution.cpp
File metadata and controls
86 lines (80 loc) · 2.23 KB
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
#include <iostream>
using namespace std;
int find_op(const string &str, const bool add, unsigned int &idx) {
// skip "add(" and "mul("
idx += 4;
// check the start character is digit
if (!isdigit(str[idx])) return 0;
int nums[] = {0, 0};
int num = 0;
while (idx < str.size()) {
// append digit to current number
if (isdigit(str[idx])) nums[num] = nums[num] * 10 + str[idx] - '0';
// switch to next number
else if (str[idx] == ',') {
num++;
// only 2 numbers allowed
if (num >= 2) return 0;
// check the next character is digit
if (idx + 1 < str.size() && !isdigit(str[idx + 1])) return 0;
}
// if exactly 2 valid numbers in function call
else if (str[idx] == ')' and num == 1) {
// move past ')', taken care of by next i++
// idx++;
return add? nums[0] + nums[1] : nums[0] * nums[1];
}
else {
return 0;
}
idx++;
}
return 0;
}
int process(const string &s) {
// state of do's does not change
static bool do_add = true, do_mul = true;
int result = 0;
for (unsigned int i = 0; i < s.size(); i++) {
// check upcoming substring
if (s.substr(i, 8) == "do_add()") {
do_add = true;
// skip this substring
i += 7;
}
else if (s.substr(i, 11) == "don't_add()") {
do_add = false;
i += 10;
}
if (s.substr(i, 8) == "do_mul()") {
do_mul = true;
i += 7;
}
else if (s.substr(i, 11) == "don't_mul()") {
do_mul = false;
i += 10;
}
// do operation if do is enabled
else if (s.substr(i, 4) == "add(" && do_add) {
result += find_op(s, true, i);
}
else if (s.substr(i, 4) == "mul(" && do_mul) {
result += find_op(s, false, i);
}
}
return result;
}
int main() {
unsigned int t;
cin >> t;
cin.ignore();
int result = 0;
// process each line
while (t--) {
string s;
getline(cin, s);
result += process(s);
}
cout << result << endl;
return 0;
}