summaryrefslogtreecommitdiffstats
path: root/day01/solution.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'day01/solution.cpp')
-rw-r--r--day01/solution.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/day01/solution.cpp b/day01/solution.cpp
new file mode 100644
index 0000000..b19504e
--- /dev/null
+++ b/day01/solution.cpp
@@ -0,0 +1,51 @@
1#include <print>
2#include <vector>
3#include <iostream>
4#include <algorithm>
5#include <ranges>
6#include <map>
7
8void part1(std::vector<int> A, std::vector<int> B) {
9 std::sort(A.begin(), A.end());
10 std::sort(B.begin(), B.end());
11
12 int sum = 0;
13 for (auto [x, y] : std::views::zip(A, B)) {
14 sum += std::abs(x - y);
15 }
16
17 std::println("{}", sum);
18}
19
20void part2(std::vector<int> A, std::vector<int> B) {
21 std::map<int, int> _A, _B;
22
23 for (auto a : A) {
24 _A[a]++;
25 }
26 for (auto b : B) {
27 _B[b]++;
28 }
29
30 int sum = 0;
31 for (auto a : _A) {
32 sum += a.first * a.second * _B[a.first];
33 }
34
35 std::println("{}", sum);
36}
37
38int main() {
39 std::vector<int> A, B;
40
41 int a, b;
42 while (std::cin >> a >> b) {
43 A.push_back(a);
44 B.push_back(b);
45 }
46
47 part1(A, B);
48 part2(A, B);
49
50 return 0;
51}