2025 day06 p2

This commit is contained in:
2025-12-14 22:04:04 +01:00
parent 8062f9a537
commit 1079b2536c
2 changed files with 171 additions and 13 deletions

View File

@@ -15,33 +15,31 @@ using namespace std::string_view_literals;
using namespace aoc::types;
// starts at 50
[[maybe_unused]]constexpr auto raw_str = R"(
123 328 51 64
45 64 387 23
[[maybe_unused]]constexpr auto raw_str = R"(123 328 51 64
45 64 387 23
6 98 215 314
* + * +
)";
* + * + )";
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 = aoc::trim(str);
// str = aoc::trim(str);
// fmt::print("INPUT\n");
// fmt::print("⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼\n");
// fmt::print("{}\n", str);
// fmt::print("{}\n", std::string(80, '-'));
auto vec = str | std::views::split("\n"sv) | std::views::transform([](auto&& line) {
auto vec = str | std::views::split("\r\n"sv) | std::views::transform([](auto&& line) {
return line | std::views::split(" "sv)
| std::views::filter([](auto&& tok) { return !std::ranges::empty(tok); })
| std::views::transform([](auto&& tok) {
return std::string{std::begin(tok), std::end(tok)};
return aoc::trim(std::string{std::begin(tok), std::end(tok)});
})
| std::ranges::to<std::vector>();
}) | std::ranges::to<std::vector>();
std::vector<std::vector<f64>> nums{};
std::vector<std::vector<std::string>> nums{};
std::vector<std::string> ops{};
for (usize i = 0; i < vec.size(); ++i) {
@@ -52,7 +50,7 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
ops.push_back(value);
continue;
}
nums[j].push_back(std::stod(vec[i][j]));
nums[j].push_back(vec[i][j]);
}
}
@@ -63,14 +61,62 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
auto const& op = ops[i];
f64 row_sum = op == "*" ? 1.0 : 0.0;
for (usize j = 0; j < nums[i].size(); ++j) {
if (op == "*") row_sum *= nums[i][j];
else if (op == "+") row_sum += nums[i][j];
if (op == "*") row_sum *= std::stod(nums[i][j]);
else if (op == "+") row_sum += std::stod(nums[i][j]);
}
// fmt::print("sum: {}\n", row_sum);
sum += row_sum;
}
fmt::print("res: {}\n", sum);
auto lines = str | std::views::split("\r\n"sv) | std::views::transform([](auto&& line) {
return std::string{std::begin(line), std::end(line)};
}) | std::ranges::to<std::vector>();
// for (usize i = 0; i < lines.size(); ++i) {
// fmt::print("| {} |\n", lines[i]);
// }
for (usize i = 0; i < lines.size(); ++i) {
fmt::print("size: {}\n", lines[i].size());
}
std::vector<std::vector<std::string>> mat{};
auto const w = lines[0].size();
fmt::print("w: {}\n", w);
for (isize j = w - 1; j >= 0; --j) {
std::string tmp{};
if (mat.empty()) mat.push_back({});
for (usize i = 0; i < lines.size() - 1; ++i) {
tmp += lines[i][j];
}
mat.back().push_back(aoc::trim(tmp));
auto const ch = lines[lines.size() - 2][j];
if (ch == '+' || ch == '*') {
mat.push_back({});
continue;
}
}
f64 new_sum = 0.0;
for (usize i = 0; i < mat.size() - 1; ++i) {
auto const& op = ops[mat.size() - 2 - i];
f64 row_sum = op == "*" ? 1.0 : 0.0;
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j].empty()) continue;
fmt::print("{},", aoc::trim(mat[i][j]));
if (op == "*") row_sum *= std::stod(aoc::trim(mat[i][j]));
else if (op == "+") row_sum += std::stod(aoc::trim(mat[i][j]));
}
new_sum += row_sum;
fmt::print("\n");
}
fmt::print("res: {}, {}\n", sum, new_sum);
}
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {