-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01_part01.ixx
45 lines (35 loc) · 895 Bytes
/
day01_part01.ixx
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
#include <vector>
#include <string>
#include <regex>
import Utilities;
using namespace std;
export module day01_part01;
namespace day01_part01 {
vector<int> static findNumbers(string line) {
vector<int> numbers;
regex e("\\d"); // matches digits
// iterator
regex_iterator<string::iterator> rit(line.begin(), line.end(), e);
regex_iterator<string::iterator> rend;
while (rit != rend) {
numbers.push_back(stoi(rit->str()));
++rit;
}
return numbers;
}
int static extractNumber (vector<int> numbers) {
if (numbers.size() == 0)
return 0;
else
return numbers[0] * 10 + numbers[numbers.size() - 1];
}
export int execute() {
vector<string> lines = Utilities::readTextFile("day01/day01_input.txt");
int result = 0;
for (string line : lines) {
vector<int> numbers = findNumbers(line);
result += extractNumber(numbers);
}
return result;
}
}