2024 day 1

This commit is contained in:
eriedaberrie 2024-11-30 23:37:34 -08:00
parent eefaac81dc
commit d795bc862e
2 changed files with 54 additions and 0 deletions

28
2024/1/main-1.cpp Normal file
View file

@ -0,0 +1,28 @@
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> l1, l2;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
int a, b;
l >> a >> b;
l1.push_back(a);
l2.push_back(b);
}
sort(l1.begin(), l1.end());
sort(l2.begin(), l2.end());
long long result = 0;
for (auto i = 0; i < l1.size(); i++) {
result += abs(l1[i] - l2[i]);
}
cout << result << '\n';
}

26
2024/1/main-2.cpp Normal file
View file

@ -0,0 +1,26 @@
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> l1;
multiset<int> l2;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
int a, b;
l >> a >> b;
l1.push_back(a);
l2.insert(b);
}
long long result = 0;
for (auto i : l1) {
result += i * l2.count(i);
}
cout << result << '\n';
}