not complete

This commit is contained in:
2024-12-02 17:23:49 +01:00
parent 94d3ffe921
commit 311ccc0e71
3 changed files with 1014 additions and 32 deletions

40
aoc.cpp
View File

@@ -8,19 +8,20 @@
#include "fmt/format.h" #include "fmt/format.h"
#include "aoc/24/01.hpp" using levels_t = std::vector<std::uint32_t>;
using reports_t = std::vector<levels_t>;
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
constexpr auto filename = "./dat/24/re/01.txt"; constexpr auto filename = "./dat/24/ex/02.txt";
std::ifstream strm{filename, std::ios::in}; std::ifstream strm{filename, std::ios::in};
if (!strm.is_open()) { if (!strm.is_open()) {
fmt::print("Error opening file: {}\n", filename); fmt::print("Error opening file: {}\n", filename);
return 1; return 1;
} }
std::vector<std::uint32_t> a{}; reports_t reports{};
std::vector<std::uint32_t> b{};
levels_t levels{};
std::string str{}; std::string str{};
while (strm) { while (strm) {
auto const c = char(strm.peek()); auto const c = char(strm.peek());
@@ -30,43 +31,18 @@ auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
} }
if (c == ' ') { if (c == ' ') {
if (!str.empty()) { if (!str.empty()) {
a.emplace_back(std::stoi(str)); levels.emplace_back(std::stoi(str));
str.clear(); str.clear();
} }
} }
if (c == '\n') { if (c == '\n') {
if (!str.empty()) { if (!str.empty()) {
b.emplace_back(std::stoi(str)); reports.emplace_back(std::move(levels));
str.clear(); str.clear();
} }
} }
[[discard]]strm.get(); strm.get();
} }
fmt::print("a: {}, b: {}\n", a.size(), b.size());
std::sort(std::begin(a), std::end(a));
std::sort(std::begin(b), std::end(b));
auto diff_view = std::views::zip(a, b) | std::views::transform([](auto const& p) {
auto const [x, y] = p;
return x > y ? x - y : y - x;
});
auto const sum = std::accumulate(std::begin(diff_view), std::end(diff_view), 0);
fmt::print("Part A: {}\n", std::abs(sum));
auto values = a | std::views::transform([&b](auto v) {
return std::pair{v, b | std::views::filter([v](auto c) {
return v == c; // Filter elements in `b` equal to `v`
}) | std::ranges::to<std::vector>()};
}) | std::ranges::to<std::vector>();
auto const meow = std::accumulate(std::begin(values), std::end(values), 0, [](auto const acc, auto const& pair) {
return acc + std::int32_t(pair.first * pair.second.size());
});
fmt::print("Part B: {}\n", meow);
return 0; return 0;
} }

6
dat/24/ex/02.txt Normal file
View File

@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

1000
dat/24/re/02.txt Normal file

File diff suppressed because it is too large Load Diff