-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.cpp
65 lines (54 loc) · 1.8 KB
/
day01.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "../utils/files.cpp"
#include <algorithm>
#include <cmath>
#include <map>
#include <numeric>
#include <ranges>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
int calculateDiff(const std::pair<int, int>& pair) { return std::abs(pair.first - pair.second); }
std::map<int, int> createFrequencyMap(std::vector<int>& vec) {
std::map<int, int> frequency_map;
for (int v : vec) {
frequency_map[v]++;
}
return frequency_map;
}
int part01(std::vector<int> left, std::vector<int> right) {
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end()); // reverse: std::sort(right.begin(), right.end(), std::greater<int>());
auto diffs = std::views::zip(left, right) | std::views::transform(calculateDiff);
auto sum = std::accumulate(diffs.begin(), diffs.end(), 0);
return sum;
}
int part02(std::vector<int> left, std::vector<int> right) {
auto leftFrequencies = createFrequencyMap(left);
auto rightFrequencies = createFrequencyMap(right);
auto sum = 0;
for (const auto& [number, count] : leftFrequencies) {
sum += number * count * rightFrequencies[number];
}
return sum;
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
auto file = readFile("../generated/aoc/inputs/2024/day01.txt");
std::vector<int> left, right;
left.reserve(file.line_count);
right.reserve(file.line_count);
std::istringstream stream(file.content);
int l, r;
while (stream >> l >> r) {
left.push_back(l);
right.push_back(r);
}
auto part1 = part01(left, right);
auto part2 = part02(left, right);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration_ms = end - start;
std::cout << "Execution time: " << duration_ms.count() << " ms" << std::endl;
std::cout << part1 << std::endl;
std::cout << part2 << std::endl;
};