From 849812d2e388b67ba08d8b30fbbb39711f93d864 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 29 Dec 2024 09:33:33 -0600 Subject: [PATCH] 2024-01 --- 2024/01.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2024/CMakeLists.txt | 3 +++ 2 files changed, 57 insertions(+) create mode 100644 2024/01.cpp diff --git a/2024/01.cpp b/2024/01.cpp new file mode 100644 index 0000000..34f9c23 --- /dev/null +++ b/2024/01.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include +#include +#include + +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 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"; +} diff --git a/2024/CMakeLists.txt b/2024/CMakeLists.txt index 943266a..e4d52ec 100644 --- a/2024/CMakeLists.txt +++ b/2024/CMakeLists.txt @@ -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)