-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.cpp
39 lines (37 loc) · 1.01 KB
/
day1.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
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc, char* argv[])
{
if(argc != 2) {
std::cerr << "\nUsage:\n"
<< "The programm requires the path to the file.";
return 1;
}
std::string_view filename{argv[1]};
std::ifstream ifile(filename.data());
if(!ifile) {
std::cerr << "\nFile cannot be open";
return 1;
}
std::vector<int> leftNums, rightNums;
int leftNum{}, rightNum{};
std::string line;
while((ifile >> leftNum >> rightNum)) {
leftNums.push_back(leftNum);
rightNums.push_back(rightNum);
}
if(leftNums.size() != rightNums.size()) {
std::cerr << "\nThe size columns must be equal";
return 1;
}
std::sort(leftNums.begin(), leftNums.end());
std::sort(rightNums.begin(), rightNums.end());
uint64_t sum{};
for(size_t i = 0; i < leftNums.size(); ++i) {
sum += abs(leftNums[i] - rightNums[i]);
}
std::cout << sum;
return 0;
}