Skip to content

Commit

Permalink
solve(day 1): Historian hysteria part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-xero committed Dec 1, 2024
0 parents commit 06641dc
Show file tree
Hide file tree
Showing 4 changed files with 1,069 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runfile
50 changes: 50 additions & 0 deletions 01/01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vecLL;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);

ll l1 = 0;
ll l2 = 0;

vecLL list1 = {};
vecLL list2 = {};

// Read input file
ifstream inputFile("./01/input.txt");

if (!inputFile.is_open()) {
cerr << "Error: Could not open file.\n";
return 1;
}

// Build both lists from input
while (inputFile >> l1 >> l2) {
cout << "l1: " << l1 << " l2: " << l2 << '\n';
list1.push_back(l1);
list2.push_back(l2);
}

// Close file (free memory)
inputFile.close();

// Sort both lists
sort(list1.begin(), list1.end());
sort(list2.begin(), list2.end());

// Compare each element and sum absolute diff (both lists are same size)
ll diff = 0;

for (size_t i = 0; i < list1.size(); i++) {
diff += abs(list1[i] - list2[i]);
}

cout << "diff: " << diff << '\n';

return 0;
}
Loading

0 comments on commit 06641dc

Please sign in to comment.