refactor utils

This commit is contained in:
2025-12-04 22:12:32 -05:00
parent 9282b3d531
commit 4b1da89690
5 changed files with 54 additions and 64 deletions

View File

@@ -27,8 +27,8 @@ enum class Point : char {
Empty = '.',
};
template<typename T>
std::array<char, 8> get_surrounding(const std::vector<T> &lines, size_t x, size_t y) noexcept {
template<utils::StringLike T>
std::array<char, 8> get_surrounding(const std::vector<T> &lines, const size_t x, const size_t y) noexcept {
std::array<char, 8> points = {};
size_t i = 0;
for (int dy = -1; dy < 2; ++dy) {
@@ -48,16 +48,15 @@ std::array<char, 8> get_surrounding(const std::vector<T> &lines, size_t x, size_
uint32_t solve_first(const std::string_view input) noexcept {
const auto lines = std::string_view{input}
| std::views::split('\n')
| std::views::transform(to_string_view)
| std::views::filter([](const auto s) { return !s.empty(); })
| std::views::transform(utils::to_string_view)
| std::views::filter(utils::non_empty)
| std::ranges::to<std::vector>();
uint32_t accessible = 0;
for (size_t y = 0; y < lines.size(); ++y) {
for (size_t x = 0; x < lines[0].size(); ++x) {
const auto c = lines[y][x];
if (c != '@') continue;
if (lines[y][x] != '@') continue;
size_t num_paper = 0;
for (const char p: get_surrounding(lines, x, y)) {
if (p == '@') {
@@ -77,7 +76,7 @@ uint32_t solve_first(const std::string_view input) noexcept {
return accessible;
}
template<typename T>
template<utils::StringLike T>
std::vector<std::tuple<size_t, size_t> > remove_paper(const std::vector<T> &lines) {
std::vector<std::tuple<size_t, size_t> > removable = {};
@@ -110,7 +109,7 @@ uint32_t solve_second(const std::string_view input) {
| std::views::transform([](const auto r) {
return std::string(r.data(), r.size());
})
| std::views::filter([](const auto &s) { return !s.empty(); })
| std::views::filter(utils::non_empty)
| std::ranges::to<std::vector>();
bool removed = false;
uint32_t total_removed = 0;
@@ -136,7 +135,7 @@ TEST_CASE("solves first sample") {
}
TEST_CASE("solves first puzzle") {
CHECK(solve_first(read_file("day4input.txt")) == 1626);
CHECK(solve_first(utils::read_file("day4input.txt")) == 1626);
}
@@ -145,7 +144,7 @@ TEST_CASE("solves second sample") {
}
TEST_CASE("solves second puzzle") {
CHECK(solve_second(read_file("day4input.txt")) == 9173);
CHECK(solve_second(utils::read_file("day4input.txt")) == 9173);
}
TEST_SUITE_END();