From 2955e6f3d961c3a8aa32568c585aa1979a25b84f Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 11 Nov 2022 21:39:58 -0800 Subject: [PATCH] grids --- lib/include/aocpp/Coord.hpp | 1 - lib/include/aocpp/Grid.hpp | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/include/aocpp/Grid.hpp diff --git a/lib/include/aocpp/Coord.hpp b/lib/include/aocpp/Coord.hpp index 81ddef8..170b768 100644 --- a/lib/include/aocpp/Coord.hpp +++ b/lib/include/aocpp/Coord.hpp @@ -16,7 +16,6 @@ struct Coord { auto operator<=>(const Coord&) const = default; }; - auto Draw(std::ostream & out, std::map image) -> void; /// Move one unit up diff --git a/lib/include/aocpp/Grid.hpp b/lib/include/aocpp/Grid.hpp new file mode 100644 index 0000000..ba4cf4c --- /dev/null +++ b/lib/include/aocpp/Grid.hpp @@ -0,0 +1,43 @@ +#ifndef AOCPP_GRID_HPP_ +#define AOCPP_GRID_HPP_ + +#include +#include +#include +#include + +#include + +namespace aocpp { + +struct Grid { + std::vector 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 + 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(x), static_cast(y)}, row[x]); + } + } + } +}; + +} + +#endif \ No newline at end of file