2025 day 05 p2

This commit is contained in:
2025-12-14 20:43:34 +01:00
parent ed04586b1d
commit 8062f9a537
2 changed files with 193 additions and 18 deletions

112
2025/day08.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include <span>
#include <algorithm>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <tuple>
#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"(
162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689
)";
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);
auto mat = str | std::views::split("\n"sv) | std::views::transform([](auto line) {
return line | std::views::split(""sv)
| std::views::transform([](auto tok) {
return std::string{std::begin(tok), std::end(tok)};
})
| std::ranges::to<std::vector>();
}) | std::ranges::to<std::vector>();
struct beam {
usize row;
usize col;
};
std::vector<beam> beams{};
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "S") {
beams.push_back({i, j});
break;
}
auto const size = beams.size();
for (usize k = 0; k < size; ++k) {
if (beams[k].row == i - 1 && beams[k].col == j) {
if (mat[i][j] == ".") {
mat[i][j] = "|";
beams[k] = {i, j};
} else if (mat[i][j] == "^") {
beams[k] = {i, j - 1};
beams.push_back({i, j + 1});
mat[i][j - 1] = "|";
mat[i][j + 1] = "|";
}
}
}
}
}
f64 sum = 0.0;
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "^") {
if (mat[i - 1][j] == "|") sum += 1;
}
}
}
fmt::print("res: {}\n", sum);
// for (auto const& row : mat) {
// for (auto const& col : row) {
// fmt::print("{}", col);
// }
// fmt::print("\n");
// }
}
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;
}