108 lines
2.5 KiB
C++
108 lines
2.5 KiB
C++
#include <span>
|
|
#include <algorithm>
|
|
#include <ranges>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#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 <typename F>
|
|
auto operator|(std::string s, F&& f) -> decltype(auto) {
|
|
return std::forward<F>(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<char>(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<char>(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<char const*> 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<std::vector>();
|
|
|
|
i32 start = 50;
|
|
i32 zero_cross = 0;
|
|
auto sum = std::ranges::fold_left(rot_vec, 0, [&](auto a, auto t) {
|
|
auto const p = start;
|
|
auto const all_d = (100 + p - t);
|
|
start = all_d % 100;
|
|
|
|
auto const z = (all_d - 99 - start) / 100;
|
|
|
|
fmt::print("pos: {}, next: {}, {}, {} z: {}\n", p, start, t, all_d, z);
|
|
zero_cross += z;
|
|
|
|
if (start == 0) {
|
|
a += 1 + zero_cross;
|
|
zero_cross = 0;
|
|
}
|
|
|
|
return a;
|
|
});
|
|
|
|
fmt::print("res: {}, {}\n", 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;
|
|
}
|