aoc24: day16 create graph

This commit is contained in:
2024-12-16 21:17:17 +01:00
parent 9c840a7c8f
commit 5c7da45fe1
3 changed files with 152 additions and 54 deletions

View File

@@ -12,13 +12,14 @@ using namespace aoc::types;
using namespace std::string_view_literals;
namespace rme {
struct node;
struct edge;
struct edge {
u64 n;
u32 a;
u32 b;
u64 a{0x00};
u64 b{0x00};
auto str() const -> std::string {
if (b == 0) return "nil";
return fmt::format("{},{}", (b >> 32), b & 0xFFFFFFFF);
}
};
struct node {
@@ -27,19 +28,45 @@ struct node {
edge s;
edge e;
edge w;
auto str() const -> std::string {
std::string str{"node {"};
str += " id: " + fmt::format("{},{}", (id >> 32) & 0xFFFFFFFF, id & 0xFFFFFFFF) + ",";
if (n.b != 0)
str += " n -> " + n.str() + ",";
if (s.b != 0)
str += " s -> " + s.str() + ",";
if (e.b != 0)
str += " e -> " + e.str() + ",";
if (w.b != 0)
str += " w -> " + w.str();
str += " }";
return str;
}
};
class graph {
public:
graph(std::string const& maze_str) : m_start(0), m_exit(0) {
auto const lines = maze_str | std::views::split("\n"sv)
| std::views::transform([](auto const& str) { return std::string_view(std::begin(str), std::end(str)); })
| std::views::filter([](auto const& str){ return !str.empty(); })
| std::ranges::to<std::vector>();
build(maze_str);
for (auto const& node : m_nodes) {
fmt::print("{}\n", node.str());
}
}
private:
auto build(std::string const& maze) -> void {
auto const lines = maze | aoc::split("\n"sv)
| aoc::map_to_sv
| aoc::filter_non_empty
| std::ranges::to<std::vector>();
m_rows = lines.size();
for (usize i = 0; i < lines.size(); ++i) {
auto const& str = lines[i];
for (usize j = 0; j < str.size(); ++j) {
m_cols = str.size();
for (usize j = 0; j < m_cols; ++j) {
auto const c = str[j];
node n{};
auto const id = u64((u64(i + 1) << 32) | (j + 1));
@@ -47,23 +74,44 @@ public:
if (c == 'S') m_start = id;
if (c == 'E') m_exit = id;
n.id = id;
auto const n_e = lines[i - 1][j];
auto const s_e = lines[i + 1][j];
auto const e_e = lines[i][j - 1];
auto const w_e = lines[i][j + 1];
if (n_e != '#') {
n.n = {
.a = n.id,
.b = u64((u64(i - 2) << 32) | (j + 1))
};
m_edges.emplace_back(n.n);
}
if (s_e != '#') {
n.s = {
.a = n.id,
.b = u64((u64(i + 2) << 32) | (j + 1))
};
m_edges.emplace_back(n.s);
}
if (e_e != '#') {
n.e = {
.a = n.id,
.b = u64((u64(i + 1) << 32) | (j - 2))
};
m_edges.emplace_back(n.e);
}
if (w_e != '#') {
n.w = {
.a = n.id,
.b = u64((u64(i + 1) << 32) | (j + 2))
};
m_edges.emplace_back(n.w);
}
m_nodes.emplace_back(std::move(n));
}
}
// for (usize i = 0; i < maze_str.size(); ++i) {
// auto const c = maze_str[i];
// node n{};
//
// if (c == '\n') {
// ++row;
// col = 1;
// }
// auto const id = u64((u64(row) << 32) | col++);
// if (c == '#' || c == '\n') continue;
// if (c == 'S') m_start = id;
// if (c == 'E') m_exit = id;
//
// n.id = id;
// }
}
private:
@@ -71,14 +119,21 @@ private:
u64 m_exit;
std::vector<node> m_nodes{};
std::vector<edge> m_edges{};
u64 m_rows{};
u64 m_cols{};
};
}
auto aoc24::day16([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
// auto res = aoc::read_text("./dat/24/ex/16.txt");
auto res = aoc::read_text("./dat/24/re/16.txt");
auto res = aoc::read_text("./dat/24/ex/16.txt");
// auto res = aoc::read_text("./dat/24/re/16.txt");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
rme::graph g{txt};
fmt::print("----------------------------------------\n");
fmt::print("{}", txt);
return {};
}