summaryrefslogtreecommitdiff
path: root/2023/01/src
diff options
context:
space:
mode:
authorFederico Igne <undyamon@disroot.org>2023-12-01 23:56:13 +0100
committerFederico Igne <undyamon@disroot.org>2023-12-01 23:56:13 +0100
commit17d6a4cb508cc9d5aa806a1e1df24bdd14621e41 (patch)
tree67d65aae1d8f7553b8936c792972c90d1ca74689 /2023/01/src
parentdd44df24524a4861379415ff10c8f6d11eb9bbcd (diff)
downloadaoc-17d6a4cb508cc9d5aa806a1e1df24bdd14621e41.tar.gz
aoc-17d6a4cb508cc9d5aa806a1e1df24bdd14621e41.zip
aoc(2301): Trebuchet?!
Diffstat (limited to '2023/01/src')
-rw-r--r--2023/01/src/part1.cpp37
-rw-r--r--2023/01/src/part2.cpp77
2 files changed, 114 insertions, 0 deletions
diff --git a/2023/01/src/part1.cpp b/2023/01/src/part1.cpp
new file mode 100644
index 0000000..3034d43
--- /dev/null
+++ b/2023/01/src/part1.cpp
@@ -0,0 +1,37 @@
1#include <iostream>
2#include <fstream>
3
4template<typename Iter>
5int search(Iter from, Iter to)
6{
7 while(from < to)
8 {
9 char c = *(from++);
10 if (std::isdigit(c))
11 {
12 return c - '0';
13 }
14 }
15 return 0;
16}
17
18int main(void)
19{
20 int answer{};
21
22 std::ifstream input{ "./resources/input.txt" };
23 if (input)
24 {
25 std::string line;
26 while(not std::getline(input, line).eof())
27 {
28 answer += 10 * search(line.begin(), line.end());
29 answer += search(line.rbegin(), line.rend());
30 }
31
32 }
33 input.close();
34
35 std::cout << answer << std::endl;
36 return 0;
37}
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
5int 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
33int 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
59int 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}