-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc1.cpp
93 lines (79 loc) · 1.84 KB
/
aoc1.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;
long long part1() {
string in;
long long sum = 0;
do {
cin >> in;
sum += (*(find_if(in.begin(), in.end(), [](const auto & ch) {
return ch >= '0' && ch <= '9';
}))-'0') * 10 + *(find_if(in.rbegin(), in.rend(), [](const auto & ch) {
return ch >= '0' && ch <= '9';
})) - '0';
}
while(in != "fourzqlhcjksixthreejrl9");
return sum;
}
vector<string> numStrings = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
template <class T>
pair<int, int> findStr(T l, T r, bool rev) {
int occurance = INT_MAX, number = -1;
for(int i = 0; i < 9; ++i) {
string & num = numStrings[i];
T found;
if (rev) {
found = search(l, r, num.rbegin(), num.rend());
}
else {
found = search(l, r, num.begin(), num.end());
}
if (found != r) {
int idx = found - l;
if(idx < occurance) {
occurance = idx;
number = i;
}
}
}
return {occurance, number+1};
}
long long part2() {
string in;
long long sum = 0;
do {
cin >> in;
// find left
int leftNumIdx = find_if(in.begin(), in.end(), [](const auto & ch) {
return ch >= '0' && ch <= '9';
}) - in.begin();
int rightNumIdx = find_if(in.rbegin(), in.rend(), [](const auto & ch) {
return ch >= '0' && ch <= '9';
}) - in.rbegin();
pair<int, int> leftStrIdx = findStr(in.begin(), in.end(), false);
pair<int, int> rightStrIdx = findStr(in.rbegin(), in.rend(), true);
if(leftNumIdx < leftStrIdx.first) {
sum += (in[leftNumIdx] - '0') * 10;
}
else {
sum += leftStrIdx.second * 10;
}
if(rightNumIdx < rightStrIdx.first) {
sum += (in[in.size() - 1 - rightNumIdx] - '0');
}
else {
sum += rightStrIdx.second;
}
}
while(in != "fourzqlhcjksixthreejrl9");
return sum;
}
int main() {
cout << part2();
return 0;
}