Compare commits

3 Commits

Author SHA1 Message Date
616f6bd610 aoc24: day02b not working 2024-12-03 01:49:01 +01:00
242f599c8a aoc24: day02a complete 2024-12-02 21:31:24 +01:00
311ccc0e71 not complete 2024-12-02 17:23:49 +01:00
3 changed files with 1109 additions and 33 deletions

132
aoc.cpp
View File

@@ -8,19 +8,21 @@
#include "fmt/format.h" #include "fmt/format.h"
#include "aoc/24/01.hpp" using levels_t = std::vector<std::int32_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}; constexpr auto filename = "./tmp.txt";
std::ifstream strm{filename, std::ios::in | std::ios::binary};
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 tmp{};
std::string str{}; std::string str{};
while (strm) { while (strm) {
auto const c = char(strm.peek()); auto const c = char(strm.peek());
@@ -28,45 +30,113 @@ auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
str += char(strm.get()); str += char(strm.get());
continue; continue;
} }
if (c == ' ') { if (c == ' ' && !str.empty()) {
if (!str.empty()) { tmp.emplace_back(std::stoi(str));
a.emplace_back(std::stoi(str));
str.clear(); str.clear();
} }
} if (c == '\n' && !str.empty()) {
if (c == '\n') { tmp.emplace_back(std::stoi(str));
if (!str.empty()) { reports.emplace_back(std::move(tmp));
b.emplace_back(std::stoi(str));
str.clear(); str.clear();
} }
} strm.get();
[[discard]]strm.get();
} }
fmt::print("a: {}, b: {}\n", a.size(), b.size()); auto diff = reports | std::views::transform([](auto const& levels) {
return levels | std::views::adjacent_transform<2>([](auto const& a, auto const& b) {
std::sort(std::begin(a), std::end(a)); return b - 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)); // condition: all increasing or all decreasing.
auto incdec = diff | std::views::filter([](auto const& levels) {
auto const n = std::ranges::all_of(levels, [](auto v) { return v < 0; });
auto const p = std::ranges::all_of(levels, [](auto v) { return v > 0; });
return n || p;
});
auto values = a | std::views::transform([&b](auto v) { // condition: Any two adjacent levels differ by at least one and at most three.
return std::pair{v, b | std::views::filter([v](auto c) { auto adjdiff = incdec | std::views::filter([](auto const& levels){
return v == c; // Filter elements in `b` equal to `v` return std::ranges::all_of(levels, [](auto v) {
}) | std::ranges::to<std::vector>()}; return std::abs(v) <= 3;
});
});
auto const safe_reports = std::ranges::distance(adjdiff);
fmt::print("Part A: {}\n", safe_reports);
auto lefts = std::views::zip(diff, reports) | std::views::filter([](auto const& pair) {
auto const& [d, l] = pair;
auto const n = std::ranges::all_of(d, [](auto v) { return v < 0; });
auto const p = std::ranges::all_of(d, [](auto v) { return v > 0; });
return !(n || p);
}) | std::views::transform([](auto const& pair) {
auto const& [d, l] = pair;
return l;
});
auto masks = diff | std::views::filter([](auto const& vec) {
auto const n = std::ranges::all_of(vec, [](auto v) { return v < 0; });
auto const p = std::ranges::all_of(vec, [](auto v) { return v > 0; });
return !(n || p);
}) | std::views::transform([](auto const& vec) {
auto const p = std::count_if(std::begin(vec), std::end(vec), [](auto const& v) { return v >= 0; });
auto const n = std::count_if(std::begin(vec), std::end(vec), [](auto const& v) { return v < 0; });
auto mask = vec | std::views::transform([&](auto const& v) {
if (p > n) return v >= 0 ? 1 : 0;
else return v < 0 ? 1 : 0;
}) | std::ranges::to<std::vector>();
mask.push_back(1);
return mask;
});
auto const remain_reports = std::views::zip(masks, lefts)
| std::views::filter([](auto const& pair) {
auto const& [mask, level] = pair;
auto const n = std::count_if(std::begin(mask), std::end(mask), [](auto const& v) { return v == 0; });
return n == 1;
})
| std::views::transform([](auto const& pair){
auto const& [mask, level] = pair;
return std::views::zip(mask, level) | std::views::filter([](auto const& pair) {
auto const& [m, v] = pair;
return m == 1;
}) | std::views::transform([](auto const& pair) {
auto const& [m, v] = pair;
return v;
}) | std::ranges::to<std::vector>();
})
| std::ranges::to<std::vector>();
auto rem_diff = remain_reports | std::views::transform([](auto const& vec) {
return vec | std::views::adjacent_transform<2>([](auto const& a, auto const& b) { return b - a; });
});
auto safe_rems_vec = rem_diff | std::views::transform([](auto const& vec) {
return vec | std::views::filter([](auto const& a) {
return std::abs(a) <= 3;
}) | std::ranges::to<std::vector>();
}) | 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) { auto safe_rems = safe_rems_vec | std::views::filter([](auto const& vec) {
return acc + std::int32_t(pair.first * pair.second.size()); return vec.size() > 0;
}) | std::views::filter([](auto const& vec) {
auto const n = std::ranges::all_of(vec, [](auto v) { return v < 0; });
auto const p = std::ranges::all_of(vec, [](auto v) { return v > 0; });
return n || p;
}); });
fmt::print("Part B: {}\n", meow); auto const safe_reports_rem = std::ranges::distance(safe_rems);
// for (auto rep : safe_rems) {
// for (auto v : rep) {
// fmt::print("{} ", v);
// }
// fmt::print("\n");
// }
fmt::print("Part B: {}\n", safe_reports + safe_reports_rem);
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