2025D01P1: done
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
#include <span>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -45,7 +43,7 @@ auto trim(std::string s) -> std::string {
|
||||
}
|
||||
|
||||
// starts at 50
|
||||
constexpr auto raw_str = R"(
|
||||
[[maybe_unused]]constexpr auto raw_str = R"(
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
@@ -59,6 +57,7 @@ L82
|
||||
)";
|
||||
|
||||
enum class rotdir {
|
||||
none,
|
||||
L,
|
||||
R
|
||||
};
|
||||
@@ -68,14 +67,48 @@ struct rot {
|
||||
u32 turns;
|
||||
};
|
||||
|
||||
auto map_char_to_rotdir(char c) -> rotdir {
|
||||
switch (c) {
|
||||
using enum rotdir;
|
||||
case 'L': return L;
|
||||
case 'R': return R;
|
||||
default: return none;
|
||||
}
|
||||
}
|
||||
|
||||
auto map_dir_to_i32(i32 value, rotdir dir) -> i32 {
|
||||
switch (dir) {
|
||||
using enum rotdir;
|
||||
case L: return -value;
|
||||
case R:
|
||||
default: return value;
|
||||
}
|
||||
}
|
||||
|
||||
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
|
||||
std::string str{raw_str};
|
||||
str = str | trim;
|
||||
fmt::print("{}\n", str);
|
||||
// std::string str{raw_str};
|
||||
// str = str | trim;
|
||||
auto str = aoc::read_text("./input.txt").value_or("") | trim;
|
||||
|
||||
auto rot_vec = str | std::views::split("\n"sv) | std::views::transform([](auto const& str) {
|
||||
return 0;
|
||||
return rot{
|
||||
.dir = map_char_to_rotdir(*std::begin(str)),
|
||||
.turns = u32(std::stoul(std::string{++std::begin(str), std::end(str)}))
|
||||
};
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
i32 start = 50;
|
||||
auto sum = std::ranges::fold_left(rot_vec, 0, [&start](auto a, auto b) {
|
||||
auto c = map_dir_to_i32(b.turns % 100, b.dir);
|
||||
|
||||
start = (100 + start + c) % 100;
|
||||
|
||||
if (start == 0) ++a;
|
||||
|
||||
return a;
|
||||
});
|
||||
|
||||
fmt::print("res: {}\n", sum);
|
||||
}
|
||||
|
||||
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
|
||||
|
||||
Reference in New Issue
Block a user