first commit
This commit is contained in:
commit
214a1b62d9
3 changed files with 1047 additions and 0 deletions
1000
2024/day01/input
Normal file
1000
2024/day01/input
Normal file
File diff suppressed because it is too large
Load diff
25
2024/day01/part1.cc
Normal file
25
2024/day01/part1.cc
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
|
||||
int main() {
|
||||
std::vector<int> left_list, right_list;
|
||||
int left, right;
|
||||
while (std::cin >> left >> right) {
|
||||
left_list.emplace_back(left);
|
||||
right_list.emplace_back(right);
|
||||
}
|
||||
std::sort(left_list.begin(), left_list.end());
|
||||
std::sort(right_list.begin(), right_list.end());
|
||||
assert(left_list.size() == right_list.size());
|
||||
int diff = 0;
|
||||
for(auto [left_elem, right_elem] : std::views::zip(left_list, right_list)) {
|
||||
diff += std::abs(left_elem - right_elem);
|
||||
}
|
||||
std::cout << diff << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
22
2024/day01/part2.cc
Normal file
22
2024/day01/part2.cc
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
|
||||
int main() {
|
||||
std::vector<int> left_list;
|
||||
std::unordered_map<int, int> right_list;
|
||||
int left, right;
|
||||
while (std::cin >> left >> right) {
|
||||
left_list.emplace_back(left);
|
||||
right_list[right]++;
|
||||
}
|
||||
int score = 0;
|
||||
for(auto left_elem : left_list) {
|
||||
score += left_elem * right_list[left_elem];
|
||||
}
|
||||
std::cout << score << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Add table
Reference in a new issue