aoc24: add day11

This commit is contained in:
portersky committed 2024-12-11 08:08:46 +01:00
1 parent 521b3dc806
commit 7ae660566e
10 files changed
+412 -21

No files matched your search

+1
View File
@@ -0,0 +1 @@
2333133121414131402
+1
View File
@@ -0,0 +1 @@
0 1 10 99 999
+1
View File
@@ -0,0 +1 @@
125 17
+1
View File
@@ -0,0 +1 @@
1711667363201489516692347485192217635365349434435044488931569084485013753845513061771430407252283779331998184220387458285563566476829778442295236696801137714536486845442950189414868095302479991233632775942376657934177170283598917795799667891410104182419392339187517969491035465122756859453436583798834343326025273367348415683239757329184167226474709599591861482283189073133251543441593867508581995716916727337789608384426072199958997716228952229776154791437676856820746922531519209764398145592394346443968414651785515137508862611174927231413639909531477759192962857353847260813261644266442265161116767260365419448954994787588697531475753620916156769185462319773894601222722554809486626517207217284878889697801537542454892189257864329118233563701989127336775621154695851988472663616556118167148956988889342784653390165121316343128016585588371641637637299778288444205359792934541084262539406550841063507134166918616535106816479491712795959694467322527835569083273076436674864445543037743159199340379515952941247459439959809857182730144966414445538691249595361235931672554539497752794822157463742415398094717116154969395076957583902497193159778653598752395619345236782011146878681544781649658160546582597381606175123933757298155728366243985057992535533763583078706820699544238666686524171422586965149535912715721173635537467979489020117485841914425752117090397344247114398647433095546749231084716056985813534423644317207674913346975435116230218892192419239496239217683928561944187018852591458468324653471236438523784425701391342016106442102863734132303158551589317063396191202550996031231178611194259554614812115622907457473657441322112267588579734680596155107438315487104543175310358524618787386597245682169184983443924474545756687088531147207711353771914689445992763885945394162659751533586439682536248578279047186163423526133883824828162655881931302852211658904869717561864679616674259568999418139522477056126870596537523417373555796839919010189971236526461088553911618370964867975433274790972055644377674880629772513836668795567754491115975057932840407447595319172257928682171057235074936416899950336897676579799015429350969457157872224868828169944711987466221990636385467413907016164692587943396332657162914782482734197385148020185176403649255910111956878843641342561719496621911064268252136279939415947959448722656938608070965859324428793669177365799148837357248798815878359370617315177110865716903087106631654679525524738157378974688882681663426845291985428721762895486921378036943139917524476274319265717218422624476996198430458053327740782594166390418828698833538886757412604921646272161171546791593743601860133379398366325261234148408263662658947755106994605212162388972699255929717789311946836924766646393036201986489327175748411020854665976536807947569797724061667393524169458633525285951478541834308241411132529627253190599387567110402610511592981072351453852983971528976956909168502752298773546324292968995758929079499778998636575733839460164310502971578346432919269588825613487390478719138231569383997283783142662212569073515521773678264396277238944430181694966657553223601527825952314477189567683427821986969837465872482477775088116619187636643232606769199843705550456542761560567454952910516931776995984887871328164031946635461093939879664073187333472636965669794282391213976956879259554044607014683515879012599687927843623021681812784275103986223721455417216699734251812658183587401551839571264721817386643951542766782619541071512728392692983981505182398298435684535257349140131815779710649652251748187784809358445020159082119413702533805753153757643548702247335123982214753066177730417292558210546046786528203720881595342132367746282581995426866549663294344863663583373121398199973554237671954286444524154695712516648814834092223428253152728917318281456229298112798067293576152827495922252055129050766968152447668026226151583832644197457299717675241181214538669760471030613283787636609494215710503637784631905361614610242237521549317196196419517179973554253985887763706133158151793845323783401926584246237835739616853444118692731889497072534433244842898572487954279547363773787615577993601211483696739914675043703327156284187255935997886054757864874852584315958786255051568740948038256416297280421710605911654841746478408074184636183712179294555464812827366147415726966580249217709650735666956736603573724346129677835672761948651056686481532135962591816418902980546014299845895663177124981131498162193491163691351178561223763374677799914383744665596766224792847691827813261618479327274547943758229598637665192951397912575115115077397756648110693492718617266159981326438888113362697081242037981285844245448861387631575776294473954064162699959634986971734349517777568665116175975046101173794471684546989136519892523776503792386350265957125572139779674741939512269062685963742790188655281266596358311149666180558890247186577176152164373081214692341952568383265449643916175420448110586962319547407888666870912387787467283288889915441847425675367570758372242160474814259881483384816034945Line truncated
+1
View File
@@ -0,0 +1 @@
112 1110 163902 0 7656027 83039 9 74
+1
View File
@@ -13,6 +13,7 @@ set(SOURCES
"day08.cpp"
"day09.cpp"
"day10.cpp"
"day11.cpp"
)
add_library(aoc24 STATIC ${HEADERS} ${SOURCES})
target_include_directories(aoc24
+2 -1
View File
@@ -26,8 +26,9 @@ auto entry([[maybe_unused]]std::span<char const*> const& args) -> std::expected<
case 6: return day06(args);
case 7: return day07(args);
case 8: return day08(args);
// case 9: return day09(args);
case 9: return day09(args);
case 10: return day10(args);
case 11: return day11(args);
default:
return aoc::make_error(fmt::format("day {}", day), std::errc::not_supported);
}
+237 -1
View File
@@ -1,7 +1,243 @@
#include <utility>
#include <vector>
#include <algorithm>
#include <numeric>
#include "aoc.hpp"
#include "aoc/utils.hpp"
#include "fmt/format.h"
using namespace aoc::types;
namespace dsk {
enum class format : u8 {
file,
free,
};
class block {
public:
block(u32 id, dsk::format format) : m_id(id), m_format(format) {}
auto id() const -> u32 { return m_id; }
auto format() const -> dsk::format { return m_format; }
auto str() const -> std::string {
using namespace std::string_literals;
std::string str{"{"};
auto const id = m_id == aoc::lim<u32>::max() ? "na" : std::to_string(m_id);
str += " id: "s + id + ",";
str += " type: "s + std::to_string(u32(m_format)) + ",";
str += " }";
return str;
}
private:
u32 m_id;
dsk::format m_format;
};
class page_block {
public:
page_block(u32 id, dsk::format format, u32 size)
: m_format(format), m_size(size) {
for (u32 i = 0; i < m_size; ++i) {
m_blocks.emplace_back(id, format);
}
}
auto format() const -> dsk::format { return m_format; }
auto size() const -> u32 { return m_size; }
auto blocks() const -> std::vector<block> const& { return m_blocks; }
auto str() const -> std::string {
using namespace std::string_literals;
std::string str{"page_block {"};
str += " size: "s + std::to_string(u32(m_size)) + ",";
str += " data: [ "s;
for (usize i = 0; i < m_blocks.size(); ++i) {
str += fmt::format("{}: ", i);
str += m_blocks[i].str();
if (i < m_blocks.size() - 1) str += ", ";
else if (i == m_blocks.size() - 1) str += " ";
}
str += "]";
str += " }";
return str;
}
private:
dsk::format m_format;
u32 m_size;
std::vector<block> m_blocks{};
};
class disk_map {
public:
disk_map(std::string const& input_str) {
m_pages = input_str | std::views::filter([](auto const& c) { return c != '\n'; })
| std::views::transform(
[i = 0u, id = 0u](auto const& size_char) mutable {
auto const f = format(i++ % 2);
auto curr_id = id;
if (f == format::file)
++id;
else
curr_id = aoc::lim<u32>::max();
return page_block(curr_id, f, u32(size_char - '0'));
}
) | std::ranges::to<std::vector>();
}
auto pages() const -> std::vector<page_block> {
return m_pages;
}
auto to_blocks(std::vector<page_block> const& pages) const -> std::vector<block> {
std::vector<block> tmp{};
for (usize i = 0; i < pages.size(); ++i) {
auto const& blocks = pages[i].blocks();
for (usize j = 0; j < blocks.size(); ++j) {
tmp.push_back(blocks[j]);
}
}
return tmp;
}
auto defrag_block(std::vector<block> const& blocks) -> std::vector<block> {
if (blocks.size() == 0) return {};
std::vector<block> cpy = blocks;
usize free_index = 0;
usize file_index = cpy.size() - 1;
while (free_index < cpy.size()) {
free_index = next_free(cpy, free_index);
if (free_index >= cpy.size() - 1) break;
file_index = next_file(cpy, file_index);
if (file_index == 0) break;
if (free_index >= file_index) break;
std::swap(cpy[free_index], cpy[file_index]);
}
return cpy;
}
// auto defrag_page(std::vector<page_block> const& pages) -> std::vector<page_block> {
// if (pages.size() == 0) return {};
//
// std::vector<page_block> cpy = pages;
// usize free_index = 0;
// usize file_index = cpy.size() - 1;
// while (free_index < cpy.size()) {
// free_index = next_page_free(cpy, free_index);
// if (free_index >= cpy.size() - 1) break;
// auto const& tmp = cpy[free_index];
//
// file_index = next_page_file(cpy, file_index, tmp.size());
// if (file_index == 0) break;
//
// if (free_index >= file_index) break;
//
// fmt::print("{}, {}\n", file_index, free_index);
// std::swap(cpy[free_index], cpy[file_index]);
// }
//
// return cpy;
// }
auto checksum(std::vector<block> const& blocks) const -> u64 {
u64 sum = 0;
for (usize i = 0; i < blocks.size(); ++i) {
auto const& block = blocks[i];
if (block.format() == format::free) break;
sum += i * block.id();
}
return sum;
}
auto debug_str(std::vector<block> const& blocks) const -> std::string {
using namespace std::string_literals;
std::string str{};
for (usize i = 0; i < blocks.size(); ++i) {
auto const& b = blocks[i];
if (b.format() == format::file) {
str += fmt::format("{}", b.id());
if (b.id() > 9) str += "'";
} else {
str += ".";
}
}
return str;
}
auto str() const -> std::string {
using namespace std::string_literals;
std::string str{"disk_map [\n"};
auto const blocks = to_blocks(m_pages);
for (usize i = 0; i < blocks.size(); ++i) {
str += " "s + blocks[i].str();
if (i < blocks.size() - 1)
str += ",\n";
}
str += "\n]";
return str;
}
private:
auto next_free(std::vector<block> const& blocks, usize start) const -> usize {
while (start < blocks.size()) {
if (blocks[start].format() == format::free)
break;
++start;
}
return start;
}
auto next_file(std::vector<block> const& blocks, usize start) const -> usize {
while (start >= 0) {
if (blocks[start].format() == format::file)
break;
--start;
}
return start;
}
auto next_page_free(std::vector<page_block> const& pages, usize start) const -> usize {
while (start < pages.size()) {
if (pages[start].format() == format::free)
break;
++start;
}
return start;
}
auto next_page_file(std::vector<page_block> const& pages, usize start, u32 size) const -> usize {
while (start >= 0) {
if (pages[start].format() == format::file && pages[start].size() <= size)
break;
--start;
}
return start;
}
private:
std::vector<page_block> m_pages{};
};
}
auto aoc24::day09([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
return aoc::make_error("", std::errc::not_supported);
auto res = aoc::read_text("./dat/24/ex/09.txt");
// auto res = aoc::read_text("./dat/24/re/09.txt");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
dsk::disk_map dsk{txt};
fmt::print("{}\n", dsk.debug_str(dsk.to_blocks(dsk.pages())));
auto const bl = dsk.defrag_block(dsk.to_blocks(dsk.pages()));
fmt::print("{}\n", dsk.debug_str(bl));
fmt::print("{}\n", dsk.checksum(bl));
return {};
}
+26 -19
View File
@@ -14,25 +14,6 @@ namespace lpf {
using edge_t = std::pair<class node, class node>;
using edges_t = std::vector<edge_t>;
class node {
public:
node(i32 id, i32 value, edges_t const& edges)
: m_id(id), m_value(value), m_edges(edges) {}
auto operator==(node const& other) const -> bool {
return m_id == other.m_id && m_value == other.m_value;
}
auto id() const -> i32 { return m_id; }
auto value() const -> i32 { return m_value; }
auto edges() const -> edges_t const& { return m_edges; }
private:
i32 m_id;
i32 m_value;
edges_t m_edges;
};
class trail {
public:
trail(std::string const& str) {
@@ -71,6 +52,32 @@ private:
usize m_cols;
std::vector<u8> m_data;
};
class node {
public:
node(i32 id, i32 value, edges_t const& edges)
: m_id(id), m_value(value), m_edges(edges) {}
auto operator==(node const& other) const -> bool {
return m_id == other.m_id && m_value == other.m_value;
}
auto id() const -> i32 { return m_id; }
auto value() const -> i32 { return m_value; }
auto edges() const -> edges_t const& { return m_edges; }
private:
i32 m_id;
i32 m_value;
edges_t m_edges;
};
class graph {
public:
private:
};
}
auto aoc24::day10([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
+141
View File
@@ -0,0 +1,141 @@
#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 ms {
class magic {
public:
magic(std::string const& str) {
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)));
}) | std::ranges::to<std::vector>();
}
auto blink() -> void {
std::vector<i64> stones{};
for (usize i = 0; i < m_stones.size(); ++i) {
auto const stone = m_stones[i];
auto const str = std::to_string(stone);
if (stone == 0) stones.emplace_back(1);
else if (str.length() % 2 == 0) {
auto const a = std::stoll(str.substr(0, str.length() / 2));
auto const b = std::stoll(str.substr(str.length() / 2));
stones.emplace_back(a);
stones.emplace_back(b);
} else {
stones.emplace_back(stone * 2024);
}
}
m_stones = stones;
}
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);
if (stone == 0) compute.emplace_back(1);
else if (str.length() % 2 == 0) {
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);
compute.emplace_back(b);
} else {
compute.emplace_back(stone * 2024);
}
}
m_mutex.lock();
m_tmp.push_back({order, compute});
m_mutex.unlock();
}
auto size() const -> usize { return m_stones.size(); }
auto join(i32 current) -> void {
std::sort(std::begin(m_tmp), std::end(m_tmp), [](auto const& a, auto const& b) {
return a.first < b.first;
});
m_stones = {};
for (usize i = 0; i < m_tmp.size(); ++i) {
for (usize j = 0; j < m_tmp[i].second.size(); ++j) {
m_stones.push_back(m_tmp[i].second[j]);
}
}
m_tmp = {};
(void)current;
// fmt::print("join... {}, {}\n", current, m_stones.size());
}
auto data() const -> std::vector<i64> const& { return m_stones; }
auto str() const -> std::string {
std::string str{};
for (usize i = 0; i < m_stones.size(); ++i) {
str += std::to_string(m_stones[i]);
if (i < m_stones.size() - 1) str += " ";
}
return str;
}
private:
std::vector<i64> m_stones{};
std::vector<std::pair<usize, std::vector<i64>>> m_tmp{};
std::mutex m_mutex{};
};
}
auto aoc24::day11([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
auto res = aoc::read_text("./dat/24/re/11.txt");
// auto res = aoc::read_text("./dat/24/ex/11b.txt");
if (!res) return std::unexpected(res.error());
auto const txt = *res;
ms::magic magic{txt};
fmt::print("{}\n", magic.str());
auto const max_threads = std::thread::hardware_concurrency();
for (i32 i = 0; i < 75; ++i) {
auto const& stones = magic.data();
auto chunk_size = stones.size() / max_threads;
if (chunk_size == 0) chunk_size = stones.size();
std::vector<std::thread> threads{};
for (usize k = 0; k < max_threads; ++k) {
auto start_index = k * chunk_size;
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)};
threads.emplace_back([&]{ magic.blink(k, split); });
}
for (usize k = 0; k < threads.size(); ++k) {
threads[k].join();
}
magic.join(i);
}
fmt::print("stones: {}\n", magic.size());
return {};
}