aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-01 21:27:31 +0100
committerThomas Schmucker <ts@its1.de>2023-12-01 21:27:31 +0100
commit9a164c3f65c01e31a9d1a8578e29b358972ddfed (patch)
treed32346277a335540af4967ebdfee38a787529690 /src
downloadadvent-of-code-9a164c3f65c01e31a9d1a8578e29b358972ddfed.tar.gz
advent-of-code-9a164c3f65c01e31a9d1a8578e29b358972ddfed.tar.bz2
advent-of-code-9a164c3f65c01e31a9d1a8578e29b358972ddfed.zip
Lösung für Tag1
Diffstat (limited to 'src')
-rw-r--r--src/01a.cpp23
-rw-r--r--src/01b.cpp69
2 files changed, 92 insertions, 0 deletions
diff --git a/src/01a.cpp b/src/01a.cpp
new file mode 100644
index 0000000..1fcc763
--- /dev/null
+++ b/src/01a.cpp
@@ -0,0 +1,23 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <string>
5using namespace std;
6
7int
8main()
9{
10 string digits{ "0123456789" };
11 fstream input{ "data/01a.txt" };
12
13 int sum = 0;
14 for ( string line; input >> line; ) {
15 auto first = find_first_of(begin(line), end(line), begin(digits), end(digits));
16 auto last = find_first_of(rbegin(line), rend(line), begin(digits), end(digits));
17
18 auto value = (*first - '0') * 10 + (*last - '0');
19
20 sum += value;
21 }
22 cout << sum << endl;
23}
diff --git a/src/01b.cpp b/src/01b.cpp
new file mode 100644
index 0000000..f87da3a
--- /dev/null
+++ b/src/01b.cpp
@@ -0,0 +1,69 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <map>
5#include <string>
6#include <vector>
7using namespace std;
8
9string
10replace_all(string str, const string search, const string with)
11{
12 for ( ;; ) {
13 auto it = str.find(search);
14 if ( it == string::npos )
15 break;
16 str.replace(it, search.length(), with);
17 }
18 return str;
19}
20
21int
22main()
23{
24 map<string, int> map{
25 { "0", 0 },
26 { "1", 1 },
27 { "2", 2 },
28 { "3", 3 },
29 { "4", 4 },
30 { "5", 5 },
31 { "6", 6 },
32 { "7", 7 },
33 { "8", 8 },
34 { "9", 9 },
35 { "one", 1 },
36 { "two", 2 },
37 { "three", 3 },
38 { "four", 4 },
39 { "five", 5 },
40 { "six", 6 },
41 { "seven", 7 },
42 { "eight", 8 },
43 { "nine", 9 },
44 };
45
46 fstream input{ "data/01a.txt" };
47
48 int sum = 0;
49 for ( string line; input >> line; ) {
50 string sub;
51 vector<int> digits;
52
53 for ( auto ch: line ) {
54 sub += ch;
55
56 for ( const auto& word: map ) {
57 if ( sub.ends_with(word.first) ) {
58 digits.push_back(word.second);
59 }
60 }
61 }
62
63 auto first = digits.front();
64 auto last = digits.back();
65
66 sum += (first * 10) + last;
67 }
68 cout << sum << endl;
69}