aboutsummaryrefslogtreecommitdiff
path: root/src/day15.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/day15.cpp')
-rw-r--r--src/day15.cpp20
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
17inline string& 18void
18rtrim(string& s, const char* t = " \t\n\r\f\v") 19rtrim(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
24vector<string> 24vector<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)
48void 49void
49part1() 50part1()
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
60void 56void