#include #include #include #include #include #include #include #include "fmt/format.h" using levels_t = std::vector; using reports_t = std::vector; auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { constexpr auto filename = "./dat/24/ex/02.txt"; std::ifstream strm{filename, std::ios::in | std::ios::binary}; if (!strm.is_open()) { fmt::print("Error opening file: {}\n", filename); return 1; } reports_t reports{}; levels_t tmp{}; std::string str{}; while (strm) { auto const c = char(strm.peek()); if (std::isdigit(c)) { str += char(strm.get()); continue; } if (c == ' ' && !str.empty()) { tmp.emplace_back(std::stoi(str)); str.clear(); } if (c == '\n' && !str.empty()) { tmp.emplace_back(std::stoi(str)); reports.emplace_back(std::move(tmp)); str.clear(); } strm.get(); } // auto diff = reports | std::views::transform([](auto const& levels) { // return levels | std::views::slide(2) | std::views::transform([](auto pair) { // return pair[1] - pair[0]; // }); // }); auto diff = reports | std::views::transform([](auto const& levels) { return levels | std::views::adjacent_transform<2>([](auto const& a, auto const& b) { return b - a; }); }); // 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; }); // condition: Any two adjacent levels differ by at least one and at most three. auto adjdiff = incdec | std::views::filter([](auto const& levels){ return std::ranges::all_of(levels, [](auto v) { return std::abs(v) <= 3; }); }); auto const safe_reports = std::ranges::distance(adjdiff); fmt::print("Part A: {}\n", safe_reports); for (auto levels : reports) { for (auto level : levels) { fmt::print("{} ", level); } fmt::print("\n"); } fmt::print("--------------------------------------------------------------------------------\n"); for (auto levels : diff) { for (auto level : levels) { fmt::print("{} ", level); } fmt::print("\n"); } return 0; }