2024-01
This commit is contained in:
parent
45f2690a32
commit
849812d2e3
54
2024/01.cpp
Normal file
54
2024/01.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::find_if;
|
||||
using std::istream;
|
||||
using std::ostream;
|
||||
using std::size_t;
|
||||
using std::sort;
|
||||
using std::uint64_t;
|
||||
using std::vector;
|
||||
using std::distance;
|
||||
|
||||
auto Main(istream & in, ostream & out) -> void
|
||||
{
|
||||
vector<uint64_t> L1, L2;
|
||||
{
|
||||
uint64_t x, y;
|
||||
while (in >> x >> y) {
|
||||
L1.push_back(x);
|
||||
L2.push_back(y);
|
||||
}
|
||||
}
|
||||
|
||||
sort(begin(L1), end(L1));
|
||||
sort(begin(L2), end(L2));
|
||||
|
||||
uint64_t p1 = 0;
|
||||
for (size_t i = 0; i < L1.size(); i++) {
|
||||
p1 += L1[i] > L2[i] ? L1[i] - L2[i] : L2[i] - L1[i];
|
||||
}
|
||||
|
||||
uint64_t p2 = 0;
|
||||
for (auto i = begin(L1), j = begin(L2); i < end(L1) && j < end(L2); ) {
|
||||
if (*i < *j) {
|
||||
i++;
|
||||
} else if (*j < *i) {
|
||||
j++;
|
||||
} else {
|
||||
auto const p = [t = *i](auto const x) { return x != t; };
|
||||
auto const i_ = find_if(i, end(L1), p);
|
||||
auto const j_ = find_if(j, end(L2), p);
|
||||
p2 += *i * (i_ - i) * (j_ - j);
|
||||
i = i_;
|
||||
j = j_;
|
||||
}
|
||||
}
|
||||
|
||||
out << "Part 1: " << p1 << "\n"
|
||||
<< "Part 2: " << p2 << "\n";
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
add_executable(2024_01 01.cpp)
|
||||
target_link_libraries(2024_01 aocpp Boost::headers)
|
||||
|
||||
add_executable(2024_07 07.cpp)
|
||||
target_link_libraries(2024_07 aocpp Boost::headers)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user