-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.2.cpp
39 lines (34 loc) · 1000 Bytes
/
day1.2.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(void) {
ifstream file("input.txt");
string line;
int sum = 0;
vector<int> calibration;
vector<string> numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
while (getline(file, line)) {
vector<int> digits;
for(int i=0; i<line.length(); i++) {
const auto c = line[i];
if(c>='0' && c<='9') {
digits.push_back(c-'0');
}
for (int j = 0; j < numbers.size(); j++) {
const auto& n = numbers[j];
if (line.size() >= i + n.size()) {
if (line.substr(i, n.size()) == n) {
digits.push_back(j+1);
}
}
}
}
calibration.push_back(digits[0] * 10 + digits.back());
}
for (int i: calibration)
sum += i;
cout << sum << endl;
file.close();
}