Skip to content

Commit

Permalink
Add AoC Day 1 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Dec 1, 2024
1 parent 0bb1d78 commit 5edab65
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions adventofcode/2024/day/01/historian_hysteria.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <utility>
#include <vector>

int main() {
std::pair<int, int> ans;
int left_number, right_number;
std::vector<int> left_numbers, right_numbers;
std::unordered_map<int, int> right_counter;
while(std::scanf("%d %d\n", &left_number, &right_number) != EOF) {
left_numbers.push_back(left_number);
right_numbers.push_back(right_number);
right_counter[right_number] += 1;
}
std::ranges::sort(left_numbers);
std::ranges::sort(right_numbers);
for (int i = 0; i < std::ssize(left_numbers); i++) {
ans.first += std::abs(left_numbers[i] - right_numbers[i]);
ans.second += left_numbers[i] * (right_counter[left_numbers[i]]);
}
std::printf("Part one: %d\nPart two: %d\n", ans.first, ans.second);
}

0 comments on commit 5edab65

Please sign in to comment.