aocpp/lib/src/Coord.cpp

91 lines
1.6 KiB
C++
Raw Normal View History

2022-11-04 21:27:08 -07:00
#include <intcode/Coord.hpp>
2022-11-04 19:59:35 -07:00
namespace intcode {
auto Draw(std::ostream & out, std::map<Coord, ValueType> 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 {
2022-11-04 21:27:08 -07:00
c.y -= 1;
2022-11-04 19:59:35 -07:00
return c;
}
auto Down(Coord c) -> Coord {
2022-11-04 21:27:08 -07:00
c.y += 1;
2022-11-04 19:59:35 -07:00
return c;
}
auto Left(Coord c) -> Coord {
2022-11-04 21:27:08 -07:00
c.x -= 1;
2022-11-04 19:59:35 -07:00
return c;
}
auto Right(Coord c) -> Coord {
2022-11-04 21:27:08 -07:00
c.x += 1;
2022-11-04 19:59:35 -07:00
return c;
}
2022-11-04 21:27:08 -07:00
auto Invert(Coord c) -> Coord {
return {c.y, c.x};
}
2022-11-04 19:59:35 -07:00
auto CW(Coord c) -> Coord {
2022-11-04 21:27:08 -07:00
c = Invert(c);
c.x = -c.x;
2022-11-04 19:59:35 -07:00
return c;
}
auto CCW(Coord c) -> Coord {
2022-11-04 21:27:08 -07:00
c = Invert(c);
c.y = -c.y;
2022-11-04 19:59:35 -07:00
return c;
}
2022-11-04 21:27:08 -07:00
auto operator+(Coord a, Coord b) -> Coord {
return {a.x + b.x, a.y + b.y};
}
auto operator-(Coord a, Coord b) -> Coord {
return {a.x - b.x, a.y - b.y};
}
auto operator+=(Coord & a, Coord b) -> Coord & {
a = a + b;
return a;
}
auto operator-=(Coord & a, Coord b) -> Coord & {
a = a - b;
return a;
}
auto operator<<(std::ostream & out, Coord c) -> std::ostream & {
return out << "(" << c.x << "," << c.y << ")";
2022-11-04 19:59:35 -07:00
}
} // namespace