85 lines
2.6 KiB
C++
85 lines
2.6 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;
|
|
|
|
// starts at 50
|
|
[[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);
|
|
|
|
// 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) {
|
|
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)};
|
|
})
|
|
| std::ranges::to<std::vector>();
|
|
}) | std::ranges::to<std::vector>();
|
|
|
|
std::vector<std::vector<f64>> nums{};
|
|
std::vector<std::string> ops{};
|
|
|
|
for (usize i = 0; i < vec.size(); ++i) {
|
|
for (usize j = 0; j < vec[i].size(); ++j) {
|
|
auto const& value = vec[i][j];
|
|
if (nums.size() <= j) nums.push_back({});
|
|
if (value == "*" || value == "+") {
|
|
ops.push_back(value);
|
|
continue;
|
|
}
|
|
nums[j].push_back(std::stod(vec[i][j]));
|
|
}
|
|
}
|
|
|
|
// fmt::print("{} {}\n", nums.size(), ops.size());
|
|
|
|
f64 sum = 0.0;
|
|
for (usize i = 0; i < nums.size(); ++i) {
|
|
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];
|
|
}
|
|
// fmt::print("sum: {}\n", row_sum);
|
|
sum += row_sum;
|
|
}
|
|
|
|
fmt::print("res: {}\n", sum);
|
|
}
|
|
|
|
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;
|
|
}
|