aocpp/lib/include/aocpp/Coord.hpp
2023-01-23 14:46:01 -06:00

81 lines
1.7 KiB
C++

#ifndef AOCPP_COORD_HPP_
#define AOCPP_COORD_HPP_
#include <functional>
#include <map>
#include <ostream>
#include <tuple>
namespace aocpp {
/// Cartesian coordinate
///
/// Coordinate where y grows down and x grows right
struct Coord {
std::int64_t x, y;
auto operator<=>(const Coord&) const = default;
};
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> image) -> void;
/// Move one unit up
auto Up(Coord) -> Coord;
/// Move one unit down
auto Down(Coord) -> Coord;
/// Move one unit left
auto Left(Coord) -> Coord;
/// Move one unit right
auto Right(Coord) -> Coord;
/// Mirror coordinates about y=x line
auto Invert(Coord) -> Coord;
/// Rotate clockwise
auto CW(Coord) -> Coord;
/// Rotate counter-clockwise
auto CCW(Coord) -> Coord;
auto Turn180(Coord) -> Coord;
/// @brief Computes the "1 norm" of a coordinate as a vector
/// @param vector
/// @return Sum of magnitudes of components
auto Norm1(Coord) -> std::int64_t;
/// @brief Computes the "infinity norm" of a coordinate as a vector
/// @param vector
/// @return Magnitude of the largest component
auto NormInf(Coord) -> std::int64_t;
/// Add two coordinates pairwise
auto operator+(Coord, Coord) -> Coord;
/// Subtract two coordinates pairwise
auto operator-(Coord, Coord) -> Coord;
/// Compound assignment addition of coordinates
auto operator+=(Coord &, Coord) -> Coord &;
/// Compound assignment subtraction of coordinates
auto operator-=(Coord &, Coord) -> Coord &;
/// Write a coordinate to a string "(x,y)"
auto operator<<(std::ostream &, Coord) -> std::ostream &;
} // namespace
template<>
struct std::hash<aocpp::Coord>
{
auto operator()(aocpp::Coord const& c) const noexcept -> std::size_t {
std::hash<std::int64_t> h;
return h(c.x) ^ h(c.y);
}
};
#endif