diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-02 09:08:01 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-02 09:08:01 +0100 |
| commit | dcca312c9b1b5311c30d1f1ade9765d2a102913d (patch) | |
| tree | 686a6779f29667ef601eeeaa46a7e9c97a8fc317 /src | |
| parent | f05f4b7f8846238b2402940731f7f2d2b7ba0f55 (diff) | |
| download | advent-of-code-dcca312c9b1b5311c30d1f1ade9765d2a102913d.tar.gz advent-of-code-dcca312c9b1b5311c30d1f1ade9765d2a102913d.tar.bz2 advent-of-code-dcca312c9b1b5311c30d1f1ade9765d2a102913d.zip | |
Bessere Dateinamen
Diffstat (limited to 'src')
| -rw-r--r-- | src/01a.cpp | 23 | ||||
| -rw-r--r-- | src/day01.cpp (renamed from src/01b.cpp) | 35 |
2 files changed, 30 insertions, 28 deletions
diff --git a/src/01a.cpp b/src/01a.cpp deleted file mode 100644 index 1fcc763..0000000 --- a/src/01a.cpp +++ /dev/null | |||
| @@ -1,23 +0,0 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <string> | ||
| 5 | using namespace std; | ||
| 6 | |||
| 7 | int | ||
| 8 | main() | ||
| 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/day01.cpp index 91ba7b7..ff357bc 100644 --- a/src/01b.cpp +++ b/src/day01.cpp | |||
| @@ -6,8 +6,26 @@ | |||
| 6 | #include <vector> | 6 | #include <vector> |
| 7 | using namespace std; | 7 | using namespace std; |
| 8 | 8 | ||
| 9 | int | 9 | void |
| 10 | main() | 10 | part1() |
| 11 | { | ||
| 12 | string digits{ "0123456789" }; | ||
| 13 | fstream input{ "data/day01.txt" }; | ||
| 14 | |||
| 15 | int sum = 0; | ||
| 16 | for ( string line; input >> line; ) { | ||
| 17 | auto first = find_first_of(begin(line), end(line), begin(digits), end(digits)); | ||
| 18 | auto last = find_first_of(rbegin(line), rend(line), begin(digits), end(digits)); | ||
| 19 | |||
| 20 | auto value = (*first - '0') * 10 + (*last - '0'); | ||
| 21 | |||
| 22 | sum += value; | ||
| 23 | } | ||
| 24 | cout << sum << endl; | ||
| 25 | } | ||
| 26 | |||
| 27 | void | ||
| 28 | part2() | ||
| 11 | { | 29 | { |
| 12 | map<string, int> map{ | 30 | map<string, int> map{ |
| 13 | { "0", 0 }, | 31 | { "0", 0 }, |
| @@ -31,11 +49,11 @@ main() | |||
| 31 | { "nine", 9 }, | 49 | { "nine", 9 }, |
| 32 | }; | 50 | }; |
| 33 | 51 | ||
| 34 | fstream input{ "data/01a.txt" }; | 52 | fstream input{ "data/day01.txt" }; |
| 35 | 53 | ||
| 36 | auto sum = 0; | 54 | auto sum = 0; |
| 37 | for ( string line; input >> line; ) { | 55 | for ( string line; input >> line; ) { |
| 38 | string sub; | 56 | string sub; |
| 39 | vector<int> digits; | 57 | vector<int> digits; |
| 40 | 58 | ||
| 41 | for ( auto ch: line ) { | 59 | for ( auto ch: line ) { |
| @@ -49,9 +67,16 @@ main() | |||
| 49 | } | 67 | } |
| 50 | 68 | ||
| 51 | auto first = digits.front(); | 69 | auto first = digits.front(); |
| 52 | auto last = digits.back(); | 70 | auto last = digits.back(); |
| 53 | 71 | ||
| 54 | sum += (first * 10) + last; | 72 | sum += (first * 10) + last; |
| 55 | } | 73 | } |
| 56 | cout << sum << endl; | 74 | cout << sum << endl; |
| 57 | } | 75 | } |
| 76 | |||
| 77 | int | ||
| 78 | main() | ||
| 79 | { | ||
| 80 | part1(); | ||
| 81 | part2(); | ||
| 82 | } | ||
