diff --git a/lib/include/intcode/Grid.hpp b/lib/include/intcode/Grid.hpp new file mode 100644 index 0000000..43ae0a7 --- /dev/null +++ b/lib/include/intcode/Grid.hpp @@ -0,0 +1,27 @@ +#ifndef INTCODE_GRID_HPP_ +#define INTCODE_GRID_HPP_ + +#include +#include +#include + +#include + +namespace intcode { + +using Coord = std::pair; + +auto Draw(std::ostream & out, std::map image) -> void; + +auto Up(Coord) -> Coord; +auto Down(Coord) -> Coord; +auto Left(Coord) -> Coord; +auto Right(Coord) -> Coord; +auto CW(Coord) -> Coord; +auto CCW(Coord) -> Coord; +auto Add(Coord, Coord) -> Coord; + + +} // namespace + +#endif diff --git a/lib/src/Grid.cpp b/lib/src/Grid.cpp new file mode 100644 index 0000000..efd6b3a --- /dev/null +++ b/lib/src/Grid.cpp @@ -0,0 +1,68 @@ +#include + +namespace intcode { + +auto Draw(std::ostream & out, std::map image) -> void { + ValueType minX = 0, maxX = 0, minY = 0, maxY = 0; + for (auto&& [k, _] : image) { + auto [x,y] = k; + minX = std::min(minX, x); + maxX = std::max(maxX, x); + minY = std::min(minY, y); + maxY = std::max(maxY, y); + } + for (ValueType y = minY; y <= maxY; y++) { + for (ValueType x = minX; x <= maxX; x++) { + auto it = image.find({x,y}); + if (image.end() == it) { + out << "."; + } else switch (it->second) { + case 0: out << "░"; break; + case 1: out << "▓"; break; + case 2: out << "2"; break; + case 3: out << "3"; break; + case 4: out << "4"; break; + default: out << "!"; break; + } + } + out << "\n"; + } +} + +auto Up(Coord c) -> Coord { + c.second -= 1; + return c; +} + +auto Down(Coord c) -> Coord { + c.second += 1; + return c; +} + +auto Left(Coord c) -> Coord { + c.first -= 1; + return c; +} + +auto Right(Coord c) -> Coord { + c.first += 1; + return c; +} + +auto CW(Coord c) -> Coord { + std::swap(c.first, c.second); + c.first = -c.first; + return c; +} + +auto CCW(Coord c) -> Coord { + std::swap(c.first, c.second); + c.second = -c.second; + return c; +} + +auto Add(Coord x, Coord y) -> Coord { + return {x.first + y.first, x.second + y.second}; +} + +} // namespace