aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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