aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day01.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-01 09:59:51 +0100
committerThomas Schmucker <ts@its1.de>2024-12-01 09:59:51 +0100
commitf00ce4b4ad3a542a9cc8d4f25a48d153b6a6abe7 (patch)
tree3d1e63d3692360ed67f631d4f1f89ce26ac82529 /2024/src/day01.cpp
parente308ad6691abd5be82e9a2b8819d0ec1549f96de (diff)
downloadadvent-of-code-f00ce4b4ad3a542a9cc8d4f25a48d153b6a6abe7.tar.gz
advent-of-code-f00ce4b4ad3a542a9cc8d4f25a48d153b6a6abe7.tar.bz2
advent-of-code-f00ce4b4ad3a542a9cc8d4f25a48d153b6a6abe7.zip
benutze std::accumulate statt einer Schleife
Diffstat (limited to '2024/src/day01.cpp')
-rw-r--r--2024/src/day01.cpp11
1 files changed, 5 insertions, 6 deletions
diff --git a/2024/src/day01.cpp b/2024/src/day01.cpp
index 61b3c29..6578c54 100644
--- a/2024/src/day01.cpp
+++ b/2024/src/day01.cpp
@@ -3,6 +3,7 @@
3#include <fstream> 3#include <fstream>
4#include <iostream> 4#include <iostream>
5#include <map> 5#include <map>
6#include <numeric>
6#include <tuple> 7#include <tuple>
7#include <vector> 8#include <vector>
8using namespace std; 9using namespace std;
@@ -39,8 +40,7 @@ part1(const vector<tuple<long, long>>& data)
39 40
40 long total_distance = 0; 41 long total_distance = 0;
41 for ( size_t i = 0; i != lefts.size(); ++i ) { 42 for ( size_t i = 0; i != lefts.size(); ++i ) {
42 long distance = abs(lefts[i] - rights[i]); 43 total_distance += abs(lefts[i] - rights[i]);
43 total_distance += distance;
44 } 44 }
45 cout << total_distance << endl; 45 cout << total_distance << endl;
46} 46}
@@ -56,10 +56,9 @@ part2(const vector<tuple<long, long>>& data)
56 rights[rhs]++; 56 rights[rhs]++;
57 } 57 }
58 58
59 long score = 0; 59 auto score = accumulate(lefts.begin(), lefts.end(), 0, [&](auto init, auto lhs) {
60 for ( const auto& lhs: lefts ) { 60 return init + lhs * rights[lhs];
61 score += lhs * rights[lhs]; 61 });
62 }
63 cout << score << endl; 62 cout << score << endl;
64} 63}
65 64