diff options
Diffstat (limited to '2023/01/src/part2.cpp')
-rw-r--r-- | 2023/01/src/part2.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/2023/01/src/part2.cpp b/2023/01/src/part2.cpp new file mode 100644 index 0000000..77b5067 --- /dev/null +++ b/2023/01/src/part2.cpp | |||
@@ -0,0 +1,77 @@ | |||
1 | #include <iostream> | ||
2 | #include <fstream> | ||
3 | #include <vector> | ||
4 | |||
5 | int update(int status[9], char c) | ||
6 | { | ||
7 | static const std::vector<std::string> words{ | ||
8 | "one", "two", "three", "four", "five", | ||
9 | "six", "seven", "eight", "nine" | ||
10 | }; | ||
11 | |||
12 | int ret{}; | ||
13 | |||
14 | for (int a{}; a < words.size(); ++a) | ||
15 | { | ||
16 | if (words[a][status[a]] == c) | ||
17 | { | ||
18 | ++status[a]; | ||
19 | } | ||
20 | else | ||
21 | { | ||
22 | status[a] = words[a][0] == c; | ||
23 | } | ||
24 | |||
25 | if (status[a] == words[a].size()) | ||
26 | { | ||
27 | ret = a+1; | ||
28 | } | ||
29 | } | ||
30 | return ret; | ||
31 | } | ||
32 | |||
33 | int search(const std::string& line) | ||
34 | { | ||
35 | int status[9]{}; | ||
36 | |||
37 | int fst{}, lst{}; | ||
38 | for (char c : line) | ||
39 | { | ||
40 | int d = update(status, c); | ||
41 | if (std::isdigit(c)) | ||
42 | { | ||
43 | d = c - '0'; | ||
44 | } | ||
45 | |||
46 | if (d > 0) | ||
47 | { | ||
48 | if (fst <= 0) | ||
49 | { | ||
50 | fst = d; | ||
51 | } | ||
52 | lst = d; | ||
53 | } | ||
54 | } | ||
55 | return fst * 10 + lst; | ||
56 | } | ||
57 | |||
58 | |||
59 | int main(void) | ||
60 | { | ||
61 | int answer{}; | ||
62 | |||
63 | std::ifstream input{ "./resources/input.txt" }; | ||
64 | if (input) | ||
65 | { | ||
66 | std::string line; | ||
67 | while(not std::getline(input, line).eof()) | ||
68 | { | ||
69 | answer += search(line); | ||
70 | } | ||
71 | |||
72 | } | ||
73 | input.close(); | ||
74 | |||
75 | std::cout << answer << std::endl; | ||
76 | return 0; | ||
77 | } | ||