#include #include #include #include #include #include #include #include "fmt/std.h" #include "aoc/types.hpp" #include "aoc/utils.hpp" using namespace std::string_view_literals; using namespace aoc::types; // simple pipe for std::string by value template auto operator|(std::string s, F&& f) -> decltype(auto) { return std::forward(f)(std::move(s)); } auto ltrim(std::string s) -> std::string { s.erase( std::begin(s), std::find_if(s.begin(), s.end(), [](char ch) { return !std::isspace(static_cast(ch)); })); return s; } auto rtrim(std::string s) -> std::string { s.erase( std::find_if(s.rbegin(), s.rend(), [](char ch) { return !std::isspace(static_cast(ch)); }).base(), s.end()); return s; } auto trim(std::string s) -> std::string { return ltrim(rtrim(std::move(s))); } // starts at 50 [[maybe_unused]]constexpr auto raw_str = R"( L68 L30 R48 L5 R60 L55 L1 L99 R14 L82 )"; auto map_char_to_rotdir(char c) -> i32 { switch (c) { case 'L': return -1; case 'R': default: return 1; } } static auto entry([[maybe_unused]]std::span const& args) -> void { // std::string str{raw_str}; auto str = aoc::read_text("./input.txt").value_or(""); str = str | trim; auto rot_vec = str | std::views::split("\n"sv) | std::views::transform([](auto const& str) { return i32(std::stoi(std::string{++std::begin(str), std::end(str)})) * map_char_to_rotdir(*std::begin(str)); }) | std::ranges::to(); i32 start = 50; i32 zero_cross = 0; // auto sum = std::ranges::fold_left(rot_vec, 0, [&](auto a, auto t) { // auto const p = start; // start = (100 + p - t) % 100; // // zero_cross += (std::abs((p - t + 1) % 100) + std::abs(t)) / 100; // // fmt::print("pos: {}, next: {}, t: {}\n", p, start, t); // // if (start == 0) { // a += 1; // } // // return a; // }); auto sum = std::ranges::fold_left(rot_vec, 0, [&](auto a, auto t) { auto const dir = t < 0 ? -1 : 1; auto const steps = std::abs(t); for (i32 i = 0; i < steps; ++i) { auto next = start + dir; if (next < 0) next = 99; next = next % 100; if (next % 100 == 0) ++zero_cross; start = next; } if (start % 100 == 0) { ++a; if (zero_cross > 0) zero_cross -= 1; } return a; }); fmt::print("res: {}, {} sum = {}\n", sum, zero_cross, sum + zero_cross); } auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { try { entry({argv, std::next(argv, argc)}); } catch (std::exception const& e) { fmt::print(stderr, "{}\n", e.what()); return 1; } return 0; }