-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathday_01a.cpp
More file actions
32 lines (30 loc) · 817 Bytes
/
day_01a.cpp
File metadata and controls
32 lines (30 loc) · 817 Bytes
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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <ranges>
#include <vector>
int main(int argc, char* argv[])
{
std::string input = "../input/day_01_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<int> l1;
std::vector<int> l2;
while(std::getline(file, line)) {
std::size_t space_idx = line.find(' ');
l1.push_back(std::stoi(line.substr(0, space_idx)));
l2.push_back(std::stoi(line.substr(space_idx + 1, line.size() - space_idx - 1)));
}
std::ranges::sort(l1);
std::ranges::sort(l2);
long long sum = 0;
for (const auto& [n1, n2] : std::views::zip(l1, l2)) {
sum += std::abs(n1 - n2);
}
std::cout << sum << '\n';
return 0;
}