aoc24: day16 create graph
This commit is contained in:
@@ -91,7 +91,15 @@ inline auto read_text(std::string const& path) -> std::expected<std::string, aoc
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
auto to_string_view(auto const& str) { return std::string_view(std::begin(str), std::end(str)); }
|
||||
inline constexpr auto split = std::views::split;
|
||||
|
||||
constexpr auto map_to_sv = std::views::transform([](auto const& str) {
|
||||
return std::string_view(std::begin(str), std::end(str));
|
||||
});
|
||||
|
||||
constexpr auto filter_non_empty = std::views::filter([](auto const& str) {
|
||||
return !str.empty();
|
||||
});
|
||||
}
|
||||
|
||||
#endif // !AOC_AOC_HPP
|
||||
|
||||
@@ -13,8 +13,18 @@ namespace rbr {
|
||||
enum class op {
|
||||
add,
|
||||
mul,
|
||||
cat,
|
||||
};
|
||||
|
||||
auto op_to_str(rbr::op value) -> char const* {
|
||||
switch(value) {
|
||||
case op::add: return "+";
|
||||
case op::mul: return "*";
|
||||
case op::cat: return "||";
|
||||
default: return "none";
|
||||
}
|
||||
}
|
||||
|
||||
class expr {
|
||||
public:
|
||||
expr(u64 eres, std::vector<u64> const& operands)
|
||||
@@ -23,8 +33,8 @@ public:
|
||||
}
|
||||
|
||||
auto find_res() -> bool {
|
||||
auto const max = std::pow(2, m_operands.size());
|
||||
for (u32 t = 0; t < max; ++t) {
|
||||
auto const max = u64(std::pow(3, m_operands.size()));
|
||||
for (u64 t = 0; t < max; ++t) {
|
||||
m_ops = t;
|
||||
m_res = 0;
|
||||
compute();
|
||||
@@ -33,25 +43,36 @@ public:
|
||||
return m_res == m_eres;
|
||||
}
|
||||
auto compute() -> void {
|
||||
for (usize i = 0; i < m_operands.size(); ++i) {
|
||||
if (i == 0) {
|
||||
m_res = m_operands[0];
|
||||
std::vector<u64> ops{};
|
||||
for (usize i = 0; i < m_operands.size() - 1; ++i) {
|
||||
auto const mode = op((m_ops >> (i * 2)) & 0x03);
|
||||
if (mode == op::mul) {
|
||||
ops.push_back(std::stoull(fmt::format("{}{}", m_operands[i + 0], m_operands[i + 1])));
|
||||
++i;
|
||||
m_ops = m_ops | ((m_ops & ~u64(mode)) >> 2);
|
||||
continue;
|
||||
}
|
||||
ops.push_back(m_operands[i]);
|
||||
}
|
||||
|
||||
for (usize i = 0; i < ops.size(); ++i) {
|
||||
if (i == 0) {
|
||||
m_res = ops[0];
|
||||
continue;
|
||||
}
|
||||
auto const a = m_res;
|
||||
auto const b = m_operands[i];
|
||||
auto const am = op((m_ops >> (i - 1)) & 0x01);
|
||||
auto const b = ops[i];
|
||||
auto const am = op((m_ops >> (i * 2)) & 0x03);
|
||||
if (am == op::add)
|
||||
m_res = a + b;
|
||||
else
|
||||
else if (am == op::mul)
|
||||
m_res = a * b;
|
||||
}
|
||||
}
|
||||
auto res() const -> u64 { return m_eres; }
|
||||
auto eres() const -> u64 { return m_eres; }
|
||||
auto operands() const -> std::vector<u64> const& { return m_operands; }
|
||||
auto ops() const -> u32 const& { return m_ops; }
|
||||
auto ops() const -> u64 const& { return m_ops; }
|
||||
auto valid() const -> bool { return m_res == m_eres; };
|
||||
|
||||
auto str() const -> std::string {
|
||||
@@ -64,7 +85,13 @@ public:
|
||||
auto const& operand = m_operands[i + 0];
|
||||
str += std::to_string(operand);
|
||||
if (i == m_operands.size() - 1) break;
|
||||
str += ((m_ops >> i) & 0x01) == 0x01 ? "+" : "*";
|
||||
auto const mode = op((m_ops >> (i * 2)) & 0x03);
|
||||
if (mode == op::add)
|
||||
str += " + ";
|
||||
else if (mode == op::mul)
|
||||
str += " * ";
|
||||
else
|
||||
str += " || ";
|
||||
}
|
||||
str += " }";
|
||||
return str;
|
||||
@@ -74,7 +101,7 @@ private:
|
||||
u64 m_eres;
|
||||
std::vector<u64> m_operands;
|
||||
u64 m_res{0x00};
|
||||
u32 m_ops{0x00};
|
||||
u64 m_ops{0x00};
|
||||
};
|
||||
|
||||
auto str_to_op(std::string const& str) -> expr {
|
||||
@@ -98,30 +125,38 @@ auto str_to_op(std::string const& str) -> expr {
|
||||
}
|
||||
|
||||
auto aoc24::day07([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
// auto res = aoc::read_text("./dat/24/ex/07.txt");
|
||||
auto res = aoc::read_text("./dat/24/re/07.txt");
|
||||
auto res = aoc::read_text("./dat/24/ex/07.txt");
|
||||
// auto res = aoc::read_text("./dat/24/re/07.txt");
|
||||
if (!res) return std::unexpected(res.error());
|
||||
auto const txt = *res;
|
||||
|
||||
auto exprs = txt | std::views::split("\n"sv)
|
||||
| std::views::transform([](auto const& str){
|
||||
return std::string(std::begin(str), std::end(str));
|
||||
})
|
||||
| std::views::filter([](auto const& str) { return !str.empty(); })
|
||||
| std::views::transform(rbr::str_to_op);
|
||||
u64 test = 0b1100'1100;
|
||||
fmt::print("{:b}\n", test);
|
||||
|
||||
auto const valids = exprs | std::views::filter([](auto const expr) { return expr.valid(); })
|
||||
| std::views::transform([](auto const& e) { return f64(e.eres()); })
|
||||
| std::ranges::to<std::vector>();
|
||||
test = (test & ~0b1100u) >> 2;
|
||||
fmt::print("{:b}\n", test);
|
||||
|
||||
auto const sum = std::accumulate(std::begin(valids), std::end(valids), 0.0);
|
||||
// fmt::print("{}\n", rbr::op_to_str(rbr::op(test >> 2)));
|
||||
|
||||
fmt::print("{}\n", sum);
|
||||
// auto exprs = txt | std::views::split("\n"sv)
|
||||
// | std::views::transform([](auto const& str){
|
||||
// return std::string(std::begin(str), std::end(str));
|
||||
// })
|
||||
// | std::views::filter([](auto const& str) { return !str.empty(); })
|
||||
// | std::views::transform(rbr::str_to_op);
|
||||
|
||||
// auto const valids = exprs | std::views::filter([](auto const expr) { return expr.valid(); })
|
||||
// | std::views::transform([](auto const& e) { return f64(e.eres()); })
|
||||
// | std::ranges::to<std::vector>();
|
||||
//
|
||||
// auto const sum = std::accumulate(std::begin(valids), std::end(valids), 0.0);
|
||||
|
||||
// fmt::print("{}\n", sum);
|
||||
|
||||
// auto const validsex = exprs | std::views::filter([](auto const expr) { return !expr.valid(); })
|
||||
// | std::ranges::to<std::vector>();
|
||||
//
|
||||
// for (auto const& exp : validsex) {
|
||||
// for (auto const& exp : exprs) {
|
||||
// fmt::print("{}\n", exp.str());
|
||||
// }
|
||||
return {};
|
||||
|
||||
111
sol/24/day16.cpp
111
sol/24/day16.cpp
@@ -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 {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user