aoc24: add day16

This commit is contained in:
2024-12-16 16:43:34 +01:00
parent 1108b8c65b
commit 9c840a7c8f
15 changed files with 976 additions and 8 deletions

View File

@@ -9,11 +9,13 @@ set(SOURCES
# "day04.cpp"
"day05.cpp"
"day06.cpp"
"day07.cpp"
# "day07.cpp"
"day08.cpp"
"day09.cpp"
"day10.cpp"
# "day11.cpp"
"day14.cpp"
"day16.cpp"
)
add_library(aoc24 STATIC ${HEADERS} ${SOURCES})
target_include_directories(aoc24

View File

@@ -24,11 +24,13 @@ auto entry([[maybe_unused]]std::span<char const*> const& args) -> std::expected<
// case 4: return day04(args);
case 5: return day05(args);
case 6: return day06(args);
case 7: return day07(args);
// case 7: return day07(args);
case 8: return day08(args);
case 9: return day09(args);
case 10: return day10(args);
// case 11: return day11(args);
case 14: return day14(args);
case 16: return day16(args);
default:
return aoc::make_error(fmt::format("day {}", day), std::errc::not_supported);
}

View File

@@ -20,7 +20,7 @@ public:
m_stones = str | std::views::filter([](auto const& c) { return c != '\n'; })
| std::views::split(" "sv)
| std::views::transform([](auto const& c_str) {
return std::stoll(std::string(std::begin(c_str), std::end(c_str)));
return i64(std::stoll(std::string(std::begin(c_str), std::end(c_str))));
}) | std::ranges::to<std::vector>();
}
@@ -44,15 +44,16 @@ public:
m_stones = stones;
}
auto blink(usize order, std::span<i64*> const& stones) -> void {
auto blink(usize order, std::span<i64> const& stones) -> void {
std::vector<i64> compute{};
for (usize i = 0; i < stones.size(); ++i) {
auto const stone = stones[i];
auto const str = std::to_string(*stone);
auto const str = std::to_string(stone);
if (stone == 0) compute.emplace_back(1);
else if (str.length() % 2 == 0) {
fmt::print("{}\n", str);
auto const a = std::stoll(str.substr(0, str.length() / 2));
auto const b = std::stoll(str.substr(str.length() / 2));
compute.emplace_back(a);
@@ -115,7 +116,7 @@ auto aoc24::day11([[maybe_unused]]std::span<char const*> const& args) -> std::ex
auto const max_threads = std::thread::hardware_concurrency();
for (i32 i = 0; i < 75; ++i) {
auto const& stones = magic.data();
auto stones = magic.data();
auto chunk_size = stones.size() / max_threads;
if (chunk_size == 0) chunk_size = stones.size();
@@ -125,8 +126,8 @@ auto aoc24::day11([[maybe_unused]]std::span<char const*> const& args) -> std::ex
auto end_index = std::min((k + 1) * chunk_size, stones.size());
if (start_index >= stones.size()) break;
// Fix: Proper range span construction
std::span<i64*> split{stones.data(), static_cast<std::size_t>(2)};
std::span<i64> sp{stones};
std::span<i64> split = sp.subspan(start_index, end_index);
threads.emplace_back([&]{ magic.blink(k, split); });
}
for (usize k = 0; k < threads.size(); ++k) {

27
sol/24/day12.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <utility>
#include <vector>
#include <algorithm>
#include <numeric>
#include <thread>
#include <functional>
#include <mutex>
#include "aoc.hpp"
#include "aoc/utils.hpp"
#include "fmt/format.h"
using namespace aoc::types;
using namespace std::string_view_literals;
namespace gmf {}
auto aoc24::day11([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
auto res = aoc::read_text("./dat/24/re/12.txt");
// auto res = aoc::read_text("./dat/24/ex/12a.txt");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
fmt::print("{}\n", txt);
return {};
}

21
sol/24/day14.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <vector>
#include <ranges>
#include <numeric>
#include "aoc.hpp"
#include "aoc/utils.hpp"
#include "fmt/format.h"
using namespace aoc::types;
using namespace std::string_view_literals;
namespace ebh {
}
auto aoc24::day14([[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");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
return {};
}

84
sol/24/day16.cpp Normal file
View File

@@ -0,0 +1,84 @@
#include <vector>
#include <ranges>
#include <numeric>
#include <memory>
#include "aoc.hpp"
#include "aoc/utils.hpp"
#include "fmt/format.h"
#include "stb_image_write.h"
using namespace aoc::types;
using namespace std::string_view_literals;
namespace rme {
struct node;
struct edge;
struct edge {
u64 n;
u32 a;
u32 b;
};
struct node {
u64 id;
edge n;
edge s;
edge e;
edge w;
};
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>();
for (usize i = 0; i < lines.size(); ++i) {
auto const& str = lines[i];
for (usize j = 0; j < str.size(); ++j) {
auto const c = str[j];
node n{};
auto const id = u64((u64(i + 1) << 32) | (j + 1));
if (c == '#' || c == '\n') continue;
if (c == 'S') m_start = id;
if (c == 'E') m_exit = id;
n.id = id;
}
}
// 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:
u64 m_start;
u64 m_exit;
std::vector<node> m_nodes{};
std::vector<edge> m_edges{};
};
}
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");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
return {};
}