This commit is contained in:
Eric Mertens 2022-11-11 21:39:58 -08:00
parent e96354c4ec
commit 2955e6f3d9
2 changed files with 43 additions and 1 deletions

View File

@ -16,7 +16,6 @@ struct Coord {
auto operator<=>(const Coord&) const = default; auto operator<=>(const Coord&) const = default;
}; };
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> image) -> void; auto Draw(std::ostream & out, std::map<Coord, std::int64_t> image) -> void;
/// Move one unit up /// Move one unit up

View File

@ -0,0 +1,43 @@
#ifndef AOCPP_GRID_HPP_
#define AOCPP_GRID_HPP_
#include <iostream>
#include <vector>
#include <utility>
#include <type_traits>
#include <aocpp/Coord.hpp>
namespace aocpp {
struct Grid {
std::vector<std::string> rows;
auto operator[](Coord c) -> char& { return rows[c.y][c.x]; }
auto operator[](Coord c) const -> char { return rows[c.y][c.x]; }
static auto Parse(std::istream & in) -> Grid {
Grid result;
std::string line;
while (std::getline(in, line)) {
result.rows.emplace_back(std::move(line));
}
return {result};
}
template <typename F>
auto each(F f = F{}) const {
auto const h = rows.size();
for (std::size_t y = 0; y < h; y++) {
auto const& row = rows[y];
auto const w = row.size();
for (std::size_t x = 0; x < w; x++) {
f(Coord{static_cast<std::int64_t>(x), static_cast<std::int64_t>(y)}, row[x]);
}
}
}
};
}
#endif