diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-15 14:18:29 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-15 14:18:29 +0100 |
| commit | 5c46738f7efa082010c44e918f2bf47627ccddca (patch) | |
| tree | 9374cc92f7610ffedc6ae0da9f1caa116b39ff3f /src | |
| parent | e88af2c16d719e998afdd74c3501caf236fe6cf1 (diff) | |
| download | advent-of-code-5c46738f7efa082010c44e918f2bf47627ccddca.tar.gz advent-of-code-5c46738f7efa082010c44e918f2bf47627ccddca.tar.bz2 advent-of-code-5c46738f7efa082010c44e918f2bf47627ccddca.zip | |
cleanup code
Diffstat (limited to 'src')
| -rw-r--r-- | src/day15.cpp | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/src/day15.cpp b/src/day15.cpp index 607f5f2..3c8163f 100644 --- a/src/day15.cpp +++ b/src/day15.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #include <fstream> | 2 | #include <fstream> |
| 3 | #include <iostream> | 3 | #include <iostream> |
| 4 | #include <list> | 4 | #include <list> |
| 5 | #include <numeric> | ||
| 5 | #include <sstream> | 6 | #include <sstream> |
| 6 | #include <string> | 7 | #include <string> |
| 7 | #include <vector> | 8 | #include <vector> |
| @@ -14,11 +15,10 @@ read_file(string_view filename) | |||
| 14 | return { istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} }; | 15 | return { istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} }; |
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | inline string& | 18 | void |
| 18 | rtrim(string& s, const char* t = " \t\n\r\f\v") | 19 | rtrim(string& str) |
| 19 | { | 20 | { |
| 20 | s.erase(s.find_last_not_of(t) + 1); | 21 | str.erase(find_if(str.rbegin(), str.rend(), [](auto chr) { return !isspace(chr); }).base(), str.end()); |
| 21 | return s; | ||
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | vector<string> | 24 | vector<string> |
| @@ -28,7 +28,8 @@ split(const string& line, char sep) | |||
| 28 | stringstream input{ line }; | 28 | stringstream input{ line }; |
| 29 | 29 | ||
| 30 | for ( string part; getline(input, part, sep); ) { | 30 | for ( string part; getline(input, part, sep); ) { |
| 31 | parts.emplace_back(rtrim(part)); | 31 | rtrim(part); |
| 32 | parts.emplace_back(part); | ||
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | return parts; | 35 | return parts; |
| @@ -48,13 +49,8 @@ calculate_hash(string_view str) | |||
| 48 | void | 49 | void |
| 49 | part1() | 50 | part1() |
| 50 | { | 51 | { |
| 51 | const auto line = read_file("data/day15.txt"); | 52 | const auto parts = split(read_file("data/day15.txt"), ','); |
| 52 | const auto parts = split(line, ','); | 53 | cout << accumulate(parts.begin(), parts.end(), 0UL, [](auto init, const auto& str) { return init + calculate_hash(str); }) << endl; |
| 53 | unsigned long value = 0; | ||
| 54 | for ( const auto& part: parts ) { | ||
| 55 | value += calculate_hash(part); | ||
| 56 | } | ||
| 57 | cout << value << endl; | ||
| 58 | } | 54 | } |
| 59 | 55 | ||
| 60 | void | 56 | void |
