From 5a02b90f8591ceac18bb93986a757b9734b35d23 Mon Sep 17 00:00:00 2001 From: mnerv <24420859+mnerv@users.noreply.github.com> Date: Sun, 8 Dec 2024 23:58:15 +0100 Subject: [PATCH] aoc24: day04b complete --- README.md | 6 -- meson.build | 19 ---- sol/24/day04.cpp | 268 +++++++++++++++++++++++++---------------------- 3 files changed, 144 insertions(+), 149 deletions(-) delete mode 100644 meson.build diff --git a/README.md b/README.md index 823f762..4f8a236 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,6 @@ This project has support for different languages. There are different ways to setup this project, using `ninja` or Visual Studio. -To use `miniaudio` to play audio on host machine use the flag `-DUSE_MINIAUDIO=ON`. - -```sh -cmake -S . -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -``` - Use `ninja` to build. diff --git a/meson.build b/meson.build deleted file mode 100644 index 3e24ce5..0000000 --- a/meson.build +++ /dev/null @@ -1,19 +0,0 @@ -project( - 'aoc', - ['cpp', 'c++'] - version: '0.0.0', - default_options: [ - 'c_std=c2x', - 'cpp_std=c++23' - ] -) - -executable( - 'aoc', - [ - 'aoc.cpp' - ], - include_directories: [ - include_directories('.') - ] -) diff --git a/sol/24/day04.cpp b/sol/24/day04.cpp index 5a43a1c..4d088fb 100644 --- a/sol/24/day04.cpp +++ b/sol/24/day04.cpp @@ -1,10 +1,10 @@ #include -#include -#include +#include +#include #include #include -#include -#include +#include +#include #include "aoc.hpp" #include "aoc/utils.hpp" @@ -15,92 +15,6 @@ namespace cms { using vecn = std::vector; using matnxn = std::vector; -// template -// class mat { -// class vec { -// public: -// vec(std::span const& data) : m_data(data) {} -// -// auto operator[](std::size_t i) const -> T const& { -// return m_data[i]; -// } -// -// private: -// std::span m_data; -// }; -// -// public: -// mat() : m_data(), m_rows(0), m_cols(0) {} -// -// auto insert(std::size_t i, std::size_t j, T const& value) -> void { -// if (i >= m_rows && j >= m_cols) { -// std::vector tmp = m_data; -// m_data.resize(i * j); -// } -// } -// -// auto operator[](std::size_t i) const -> mat::vec { -// return vec({m_data[i * m_cols], m_data[i * m_cols + m_cols]}); -// } -// -// private: -// std::vector m_data; -// std::size_t m_rows; -// std::size_t m_cols; -// }; - -// Permutations: -std::vector xmas_patterns{ - { - {'X', 'M', 'A', 'S'}, - {'X', 'M', 'A', 'S'}, - {'X', 'M', 'A', 'S'}, - {'X', 'M', 'A', 'S'}, - }, - { - {'S', 'A', 'M', 'X'}, - {'S', 'A', 'M', 'X'}, - {'S', 'A', 'M', 'X'}, - {'S', 'A', 'M', 'X'}, - }, - { - {'X', 'X', 'X', 'X'}, - {'M', 'M', 'M', 'M'}, - {'A', 'A', 'A', 'A'}, - {'S', 'S', 'S', 'S'}, - }, - { - {'S', 'S', 'S', 'S'}, - {'A', 'A', 'A', 'A'}, - {'M', 'M', 'M', 'M'}, - {'X', 'X', 'X', 'X'}, - }, - { - {'X', 0, 0, 0}, - { 0, 'M', 0, 0}, - { 0, 0, 'A', 0}, - { 0, 0, 0, 'S'}, - }, - { - {'S', 0, 0, 0}, - { 0, 'A', 0, 0}, - { 0, 0, 'M', 0}, - { 0, 0, 0, 'X'}, - }, - { - { 0, 0, 0, 'S'}, - { 0, 0, 'A', 0}, - { 0, 'M', 0, 0}, - {'X', 0, 0, 0}, - }, - { - { 0, 0, 0, 'X'}, - { 0, 0, 'M', 0}, - { 0, 'A', 0, 0}, - {'S', 0, 0, 0}, - }, -}; - auto read_text_matnxn(std::string const& path) -> matnxn { std::ifstream strm{path}; if (!strm.is_open()) @@ -110,11 +24,10 @@ auto read_text_matnxn(std::string const& path) -> matnxn { vecn cols{}; while (!strm.eof()) { auto const c = static_cast(strm.peek()); - if (c == '\n') { + if (c == '\n') text.emplace_back(std::move(cols)); - } else { + else cols.push_back(c); - } strm.get(); } @@ -122,40 +35,147 @@ auto read_text_matnxn(std::string const& path) -> matnxn { } } +auto part_a(cms::matnxn const& mat) -> void { + std::vector pattern{'X', 'M', 'A', 'S'}; + + aoc::i32 hc = 0; + aoc::i32 vc = 0; + aoc::i32 xc = 0; + + for (std::size_t i = 0; i < mat.size(); ++i) { + auto const& cols = mat[i]; + + auto hview = cols | std::views::slide(4) + | std::views::filter([&](auto const& str) { + auto const a = std::ranges::all_of( + std::views::zip(pattern, str), [](auto const& z) { + return std::get<0>(z) == std::get<1>(z); + }); + auto const b = std::ranges::all_of( + std::views::zip(std::views::reverse(pattern), str), + [](auto const& z) { + return std::get<0>(z) == std::get<1>(z); + }); + return a || b; + }); + hc += std::ranges::distance(hview); + + for (std::size_t j = 0; j < cols.size(); ++j) { + // Vertical pattern + bool is_v_match = true; + bool is_rv_match = true; + for (std::size_t y = 0; y < pattern.size(); ++y) { + if (i + y >= mat.size()) { + is_v_match = false; + is_rv_match = false; + break; + } + if (is_v_match) + is_v_match = mat[i + y][j] == pattern[y]; + if (is_rv_match) + is_rv_match = mat[i + y][j] == pattern[pattern.size() - y - 1]; + } + + vc += is_v_match ? 1 : 0; + vc += is_rv_match ? 1 : 0; + + bool is_dr_match = true; + bool is_drr_match = true; + for (std::size_t x = 0; x < pattern.size(); ++x) { + if (i + x >= mat.size() || j + x >= cols.size()) { + is_dr_match = false; + is_drr_match = false; + break; + } + + if (is_dr_match) + is_dr_match = mat[i + x][j + x] == pattern[x]; + if (is_drr_match) + is_drr_match = mat[i + x][j + x] == pattern[pattern.size() - x - 1]; + } + + xc += is_dr_match ? 1 : 0; + xc += is_drr_match ? 1 : 0; + + bool is_dl_match = true; + bool is_dlr_match = true; + for (std::size_t x = 0; x < pattern.size(); ++x) { + if (i + x >= mat.size() || j + pattern.size() - 1 >= cols.size()) { + is_dl_match = false; + is_dlr_match = false; + break; + } + + if (is_dl_match) + is_dl_match = mat[i + x][j + pattern.size() - x - 1] == pattern[x]; + if (is_dlr_match) + is_dlr_match = mat[i + x][j + pattern.size() - x - 1] == pattern[pattern.size() - x - 1]; + } + + xc += is_dl_match ? 1 : 0; + xc += is_dlr_match ? 1 : 0; + } + } + + // fmt::print("{}\n", hc); + // fmt::print("{}\n", vc); + // fmt::print("{}\n", xc); + fmt::print("Part A: {}\n", hc + vc + xc); +} + +auto part_b(cms::matnxn const& mat) -> void { + std::vector pattern{'M', 'A', 'S'}; + + aoc::i32 xc = 0; + + for (std::size_t i = 0; i < mat.size(); ++i) { + auto const& cols = mat[i]; + + for (std::size_t j = 0; j < cols.size(); ++j) { + bool is_dr_match = true; + bool is_drr_match = true; + for (std::size_t x = 0; x < pattern.size(); ++x) { + if (i + x >= mat.size() || j + x >= cols.size()) { + is_dr_match = false; + is_drr_match = false; + break; + } + + if (is_dr_match) + is_dr_match = mat[i + x][j + x] == pattern[x]; + if (is_drr_match) + is_drr_match = mat[i + x][j + x] == pattern[pattern.size() - x - 1]; + } + + bool is_dl_match = true; + bool is_dlr_match = true; + for (std::size_t x = 0; x < pattern.size(); ++x) { + if (i + x >= mat.size() || j + pattern.size() - 1 >= cols.size()) { + is_dl_match = false; + is_dlr_match = false; + break; + } + + if (is_dl_match) + is_dl_match = mat[i + x][j + pattern.size() - x - 1] == pattern[x]; + if (is_dlr_match) + is_dlr_match = mat[i + x][j + pattern.size() - x - 1] == pattern[pattern.size() - x - 1]; + } + + if ((is_dr_match || is_drr_match) && (is_dl_match || is_dlr_match)) xc += 1; + } + } + + fmt::print("Part B: {}\n", xc); +} + auto aoc24::day04([[maybe_unused]]std::span const& args) -> std::expected { - auto source = cms::read_text_matnxn("./dat/24/ex/04.txt"); + // auto mat = cms::read_text_matnxn("./dat/24/ex/04.txt"); + auto mat = cms::read_text_matnxn("./dat/24/re/04.txt"); - cms::vecn hpat{'X', 'M', 'A', 'S'}; + part_a(mat); + part_b(mat); - // for (std::size_t i = 0; i < source.size(); ++i) { - // auto const& cols = source[i]; - // for (std::size_t j = 0; j < cols.size(); ++j) { - // bool is_h_match = true; - // for (std::size_t x = 0; x < hpat.size(); ++i) { - // if (x + i >= cols.size()) { - // is_h_match = false; - // break; - // } - // if (hpat[x] != cols[x + i]) { - // is_h_match = false; - // break; - // } - // } - // - // // for (auto const& pattern : cms::xmas_patterns) { - // // // for (std::size_t y = 0; y < pattern.size(); ++y) { - // // // auto const& pcol = pattern[y]; - // // // for (std::size_t x = 0; x < pcol.size(); ++x) { - // // // if (y + i >= source.size() || x + j >= cols.size()) - // // // break; - // // // - // // // if ( - // // // } - // // // } - // // } - // } - // fmt::print("\n"); - // } return {}; }