aboutsummaryrefslogtreecommitdiff
path: root/2024/03/day03b.cpp
blob: 06c8f2135b3ad18d43194013f4bba58e53d39efe (plain)
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
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

bool state = true;

int trymult(const string_view &s) {
	if (s.substr(0, 4) != "mul(")
		return 0;

	auto t = s.substr(4, s.length()-4);

	int x = 0, y = 0;
	int i = 0;
	for (; i < 3; i++)
		if (t[i] >= '0' && t[i] <= '9')
			x = (x*10) + t[i]-'0';
		else break;
	if (i == 0 || t[i] != ',')
		return 0;
	i++;
	int j = 0;
	for (; j < 3; j++)
		if (t[i+j] >= '0' && t[i+j] <= '9')
			y = (y*10) + t[i+j]-'0';
		else break;
	if (j == 0 || t[j+i] != ')')
		return 0;

	return x*y;
}

bool trytogglestate(bool current_state, const string_view &s) {
	if (s.substr(0, 4) == "do()")
		return true;
	if (s.substr(0, 7) == "don't()")
		return false;
	return current_state;
}

int mults(const string &line) {
	int result = 0;
	for (auto i = line.begin(); i != line.end(); i++) {
		string_view s(i, line.end());
		state = trytogglestate(state, s);
		if (state)
			result += trymult(s);
	}
	return result;
}

int main() {
	string line;
	int result = 0;
	while (cin >> line)
		result += mults(line);
	cout << result << endl;
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net