aoc: 2024 bk
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include <ranges>
|
||||
#include <numeric>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
@@ -18,7 +20,7 @@ struct edge {
|
||||
|
||||
auto str() const -> std::string {
|
||||
if (b == 0) return "nil";
|
||||
return fmt::format("{},{}", (b >> 32), b & 0xFFFFFFFF);
|
||||
return fmt::format("{}.{}", (b >> 32), b & 0xFFFFFFFF);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -31,20 +33,21 @@ struct node {
|
||||
|
||||
auto str() const -> std::string {
|
||||
std::string str{"node {"};
|
||||
str += " id: " + fmt::format("{},{}", (id >> 32) & 0xFFFFFFFF, id & 0xFFFFFFFF) + ",";
|
||||
str += " id: " + fmt::format("{}.{}", (id >> 32) & 0xFFFFFFFF, id & 0xFFFFFFFF);
|
||||
if (n.b != 0)
|
||||
str += " n -> " + n.str() + ",";
|
||||
str += ", n → " + n.str();
|
||||
if (s.b != 0)
|
||||
str += " s -> " + s.str() + ",";
|
||||
str += ", s → " + s.str();
|
||||
if (e.b != 0)
|
||||
str += " e -> " + e.str() + ",";
|
||||
str += ", e → " + e.str();
|
||||
if (w.b != 0)
|
||||
str += " w -> " + w.str();
|
||||
str += ", w → " + w.str();
|
||||
str += " }";
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class graph {
|
||||
public:
|
||||
graph(std::string const& maze_str) : m_start(0), m_exit(0) {
|
||||
@@ -55,6 +58,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
auto vertices() const -> std::vector<node> const& { return m_nodes; }
|
||||
|
||||
private:
|
||||
auto build(std::string const& maze) -> void {
|
||||
auto const lines = maze | aoc::split("\n"sv)
|
||||
@@ -83,7 +88,7 @@ private:
|
||||
if (n_e != '#') {
|
||||
n.n = {
|
||||
.a = n.id,
|
||||
.b = u64((u64(i - 2) << 32) | (j + 1))
|
||||
.b = u64((u64(i) << 32) | (j + 1))
|
||||
};
|
||||
m_edges.emplace_back(n.n);
|
||||
}
|
||||
@@ -97,7 +102,7 @@ private:
|
||||
if (e_e != '#') {
|
||||
n.e = {
|
||||
.a = n.id,
|
||||
.b = u64((u64(i + 1) << 32) | (j - 2))
|
||||
.b = u64((u64(i + 1) << 32) | (j))
|
||||
};
|
||||
m_edges.emplace_back(n.e);
|
||||
}
|
||||
@@ -122,6 +127,30 @@ private:
|
||||
u64 m_rows{};
|
||||
u64 m_cols{};
|
||||
};
|
||||
|
||||
// auto dfs() -> void {}
|
||||
// auto bfs() -> void {}
|
||||
|
||||
auto dijkstra(graph const& g, u64 source) -> void {
|
||||
std::map<u64, i64> dist{};
|
||||
std::map<u64, i64> prev{};
|
||||
for (auto const& v : g.vertices()) {
|
||||
dist.insert({v.id, aoc::lim<i64>::max()});
|
||||
prev.insert({v.id, -1});
|
||||
}
|
||||
dist[source] = 0;
|
||||
std::queue<node> q{};
|
||||
q.push(g.vertices()[0]);
|
||||
|
||||
while (!q.empty()) {
|
||||
node u = q.pop();
|
||||
|
||||
if (u.n.b != 0) {
|
||||
i64 alt = dist[u.id] == aoc::lim<i64>::max() ? 1 : dist[u.id] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
auto aoc24::day16([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
|
||||
Reference in New Issue
Block a user