aoc24/01/main.cc

77 lines
1.5 KiB
C++
Raw Normal View History

2024-12-01 15:05:24 +01:00
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
void parseFile(const char *path, vector<int> &list1, vector<int> &list2) {
ifstream input_stream(path);
int a;
int b;
while(input_stream >> a >> b) {
list1.push_back(a);
list2.push_back(b);
}
}
void bubbleSort(vector<int> &list) {
bool changed = false;
for (int i = 0; i < list.size() - 1; i++) {
int temp = list[i];
if (list[i] > list[i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
changed = true;
}
}
if (changed) bubbleSort(list);
}
int count(int n, vector<int> list) {
int cnt = 0;
for (int i = 0; i < list.size(); i++) {
if (list[i] == n) cnt++;
}
return cnt;
}
int main(int argc, char *argv[]) {
vector<int> list1;
vector<int> list2;
// parse input
parseFile("input", list1, list2);
// sort lists
bubbleSort(list1);
bubbleSort(list2);
// PART 1
/// sum up differences
int sum = 0;
for (int i = 0; i < list1.size(); i++) {
cout << list1[i] << " " << list2[i] << endl;
int difference = list1[i] - list2[i];
if (difference > 0) {
sum += difference;
} else {
sum -= difference;
}
}
cout << "distance between lists is: " << sum << endl;
// PART 2
/// sum up similarities
int sim_score = 0;
for (int i = 0; i < list1.size(); i++) {
sim_score += list1[i] * count(list1[i], list2);
}
cout << "similarity score is: " << sim_score << endl;
return 0;
}