-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.cpp
61 lines (56 loc) · 1.55 KB
/
1.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <vector>
int part1(const std::string& line) {
const size_t n = line.size();
int first, last;
for (size_t i = 0; i < n; ++i) {
if (std::isdigit(line[i])) {
first = line[i] - '0';
break;
}
}
for (size_t i = n - 1; i >= 0; --i) {
if (std::isdigit(line[i])) {
last = line[i] - '0';
break;
}
}
return first * 10 + last;
}
const std::string digits[]{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
inline bool string_match(const std::string& line, const std::string& pat, int idx) {
for (int i = 0; i < pat.size(); ++i) {
if (idx + i >= line.size()) return false;
if (line[idx + i] != pat[i]) return false;
}
return true;
}
int part2(const std::string& line) {
std::vector<int> candidates;
int idx = 0;
while (idx < line.size()) {
if (std::isdigit(line[idx])) {
candidates.push_back(line[idx++] - '0');
} else {
for (int i = 1; i < 10; ++i) {
if (string_match(line, digits[i], idx)) {
candidates.push_back(i);
break;
}
}
++idx;
}
}
return candidates[0] * 10 + candidates.back();
}
int main() {
std::ifstream f("1.txt");
std::string line;
std::uint64_t ans = 0;
while (std::getline(f, line)) {
// ans += part1(line);
ans += part2(line);
}
std::cout << ans << std::endl;
}