160 lines
4.0 KiB
C++
160 lines
4.0 KiB
C++
#include <fstream>
|
|
#include <expected>
|
|
#include <string>
|
|
#include <ranges>
|
|
#include <numeric>
|
|
#include <vector>
|
|
#include <span>
|
|
|
|
#include "aoc/aoc.hpp"
|
|
#include "fmt/format.h"
|
|
#include "ctre.hpp"
|
|
|
|
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())
|
|
throw std::runtime_error(fmt::format("Failed to open file {}", path));
|
|
|
|
matnxn text{};
|
|
vecn cols{};
|
|
while (!strm.eof()) {
|
|
auto const c = static_cast<std::uint8_t>(strm.peek());
|
|
if (c == '\n') {
|
|
text.emplace_back(std::move(cols));
|
|
} else {
|
|
cols.push_back(c);
|
|
}
|
|
strm.get();
|
|
}
|
|
|
|
return text;
|
|
}
|
|
}
|
|
|
|
auto aoc::entry([[maybe_unused]]std::span<char const*> const& args) -> void {
|
|
auto source = cms::read_text_matnxn("./dat/24/ex/04.txt");
|
|
|
|
cms::vecn hpat{'X', 'M', 'A', 'S'};
|
|
|
|
// 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");
|
|
// }
|
|
}
|
|
|