aoc24: day04b complete
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
|
||||
19
meson.build
19
meson.build
@@ -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('.')
|
||||
]
|
||||
)
|
||||
268
sol/24/day04.cpp
268
sol/24/day04.cpp
@@ -1,10 +1,10 @@
|
||||
#include <fstream>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
@@ -15,92 +15,6 @@ namespace cms {
|
||||
using vecn = std::vector<std::uint8_t>;
|
||||
using matnxn = std::vector<vecn>;
|
||||
|
||||
// template <typename T>
|
||||
// class mat {
|
||||
// class vec {
|
||||
// public:
|
||||
// vec(std::span<T> const& data) : m_data(data) {}
|
||||
//
|
||||
// auto operator[](std::size_t i) const -> T const& {
|
||||
// return m_data[i];
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// std::span<T> 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<T> 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<T> m_data;
|
||||
// std::size_t m_rows;
|
||||
// std::size_t m_cols;
|
||||
// };
|
||||
|
||||
// Permutations:
|
||||
std::vector<matnxn> 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<std::uint8_t>(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<aoc::u8> 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<aoc::u8> 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<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
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 {};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user