aoc24: day02a complete
This commit is contained in:
69
aoc.cpp
69
aoc.cpp
@@ -8,12 +8,12 @@
|
||||
|
||||
#include "fmt/format.h"
|
||||
|
||||
using levels_t = std::vector<std::uint32_t>;
|
||||
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 {
|
||||
constexpr auto filename = "./dat/24/ex/02.txt";
|
||||
std::ifstream strm{filename, std::ios::in};
|
||||
std::ifstream strm{filename, std::ios::in | std::ios::binary};
|
||||
if (!strm.is_open()) {
|
||||
fmt::print("Error opening file: {}\n", filename);
|
||||
return 1;
|
||||
@@ -21,7 +21,7 @@ auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
|
||||
|
||||
reports_t reports{};
|
||||
|
||||
levels_t levels{};
|
||||
levels_t tmp{};
|
||||
std::string str{};
|
||||
while (strm) {
|
||||
auto const c = char(strm.peek());
|
||||
@@ -29,20 +29,63 @@ auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
|
||||
str += char(strm.get());
|
||||
continue;
|
||||
}
|
||||
if (c == ' ') {
|
||||
if (!str.empty()) {
|
||||
levels.emplace_back(std::stoi(str));
|
||||
str.clear();
|
||||
}
|
||||
if (c == ' ' && !str.empty()) {
|
||||
tmp.emplace_back(std::stoi(str));
|
||||
str.clear();
|
||||
}
|
||||
if (c == '\n') {
|
||||
if (!str.empty()) {
|
||||
reports.emplace_back(std::move(levels));
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user