aboutsummaryrefslogtreecommitdiff
path: root/2024/03/day03a.cpp
blob: 2fb04cf699edba2929267e7b6d8a171085f627f5 (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
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

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;
}

int mults(const string &line) {
	int result = 0;
	for (auto i = line.begin(); i != line.end(); i++)
		result += trymult(string_view(i, line.end()));
	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