summaryrefslogtreecommitdiffstats
path: root/day01/solution.cpp
blob: b19504ebea4c573fbd413e1c82d6fff9f20a48c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <print>
#include <vector>
#include <iostream>
#include <algorithm>
#include <ranges>
#include <map>

void part1(std::vector<int> A, std::vector<int> B) {
  std::sort(A.begin(), A.end());
  std::sort(B.begin(), B.end());

  int sum = 0;
  for (auto [x, y] : std::views::zip(A, B)) {
    sum += std::abs(x - y);
  }

  std::println("{}", sum);
}

void part2(std::vector<int> A, std::vector<int> B) {
  std::map<int, int> _A, _B;

  for (auto a : A) {
    _A[a]++;
  }
  for (auto b : B) {
    _B[b]++;
  }

  int sum = 0;
  for (auto a : _A) {
    sum += a.first * a.second * _B[a.first];
  }

  std::println("{}", sum);
}

int main() {
  std::vector<int> A, B;

  int a, b;
  while (std::cin >> a >> b) {
    A.push_back(a);
    B.push_back(b);
  }

  part1(A, B);
  part2(A, B);

  return 0;
}