2025 day 2 p1
This commit is contained in:
@@ -22,18 +22,62 @@ using namespace aoc::types;
|
||||
)";
|
||||
|
||||
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("");
|
||||
// std::string str{raw_str};
|
||||
auto str = aoc::read_text("./input.txt").value_or("");
|
||||
str = aoc::trim(str);
|
||||
|
||||
auto ids_str = str | std::views::split(","sv) | std::views::transform([](auto const& str) {
|
||||
return aoc::trim({std::begin(str), std::end(str)});
|
||||
std::vector<u64> range{};
|
||||
range.resize(2);
|
||||
|
||||
// std::ranges::transform(
|
||||
// aoc::trim(std::string{std::begin(str), std::end(str)}) | std::views::split("-"sv),
|
||||
// std::begin(range),
|
||||
// [](auto const& value) {
|
||||
// return aoc::trim(std::string{std::begin(value), std::end(value)});
|
||||
// });
|
||||
|
||||
std::ranges::transform(
|
||||
aoc::trim(std::string{std::begin(str), std::end(str)}) | std::views::split("-"sv),
|
||||
std::begin(range),
|
||||
[](auto const& value) {
|
||||
return u64(std::stoul(aoc::trim(std::string{std::begin(value), std::end(value)})));
|
||||
});
|
||||
|
||||
return range;
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
for (auto const& id : ids_str) {
|
||||
std::print("{}\n", id);
|
||||
auto is_valid_id = [](u64 id) {
|
||||
auto const str = fmt::format("{}", id);
|
||||
if (str.size() % 2 != 0) return true;
|
||||
bool is_invalid = true;
|
||||
for (usize i = 0; i < str.size() / 2; ++i) {
|
||||
if (str[i] != str[str.size() / 2 + i]) {
|
||||
is_invalid = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return !is_invalid;
|
||||
};
|
||||
|
||||
u64 sum = 0;
|
||||
|
||||
for (auto const& id : ids_str) {
|
||||
u64 const start = id[0];
|
||||
u64 const end = id[1];
|
||||
|
||||
fmt::print("{} -> {}: ", id[0], id[1]);
|
||||
for (u64 i = start; i <= end; ++i) {
|
||||
if (!is_valid_id(i)) {
|
||||
fmt::print("{}, ", i);
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
fmt::print("\n");
|
||||
}
|
||||
|
||||
fmt::print("res: {}\n", sum);
|
||||
}
|
||||
|
||||
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user