diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2024-12-01 20:27:19 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2024-12-01 20:27:19 +0800 |
| commit | af9b4431f36a28a0b52bc2a313e1d8ba98bc47d8 (patch) | |
| tree | 928b212664b8fc85f675740a9e432bdcdfe945ac /2024/1.cpp | |
| parent | 4f9b62dbf6e8d2df2616c28d147e3e522728f488 (diff) | |
init: 2024
Diffstat (limited to '2024/1.cpp')
| -rw-r--r-- | 2024/1.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/2024/1.cpp b/2024/1.cpp new file mode 100644 index 0000000..9cffc3b --- /dev/null +++ b/2024/1.cpp @@ -0,0 +1,41 @@ +#include <bits/stdc++.h> +#include "readfile.hpp" + + +int main(int argc, char const *argv[]) +{ + int res = 0; + std::vector<std::string> file = readfile("1.in"); + if (file.empty()) { + return 1; + } + std::vector<int> left, right; + for (const auto &line : file) { + int n1, n2; + // put to vector + std::istringstream iss(line); + if (!(iss >> n1 >> n2)) { + std::cerr << "Invalid input line: " << line << std::endl; + return 1; + } + left.push_back(n1); + right.push_back(n2); + } + + // sort + std::sort(left.begin(), left.end()); + std::sort(right.begin(), right.end()); + + if (left.size() != right.size()) { + std::cerr << "size mismatch: " << left.size() << " vs " << right.size() << std::endl; + return 1; + } + + // calc total distant + for (int i = 0; i < left.size(); i++) { + res += std::abs(left[i] - right[i]); + } + + std::cout << "Result: " << res << std::endl; + return 0; +} |
