aocpp/lib/include/aocpp/Hex.hpp
2022-11-25 10:55:28 -08:00

57 lines
1.0 KiB
C++

#ifndef AOCPP_HEX_HPP_
#define AOCPP_HEX_HPP_
#include <numeric>
#include <cstdint>
namespace aocpp {
/*
\ n /
nw +--+ ne
/ \
-+ +-
\ /
sw +--+ se
/ s \
*/
struct Hex {
std::int64_t q, r, s;
};
constexpr Hex HEX_N { 0, -1, 1 };
constexpr Hex HEX_NE { 1, -1, 0 };
constexpr Hex HEX_SE { 1, 0, -1 };
constexpr Hex HEX_S { 0, 1, -1 };
constexpr Hex HEX_SW { -1, 1, 0 };
constexpr Hex HEX_NW { -1, 0, 1 };
auto operator+(Hex const& a, Hex const& b) -> Hex {
return {a.q + b.q, a.r + b.r, a.s + b.s };
}
auto operator+=(Hex & a, Hex const& b) -> Hex & {
a.q += b.q; a.r += b.r; a.s += b.s;
return a;
}
auto operator-(Hex const& a, Hex const& b) -> Hex {
return {a.q - b.q, a.r - b.r, a.s - b.s };
}
auto operator-=(Hex & a, Hex const& b) -> Hex & {
a.q -= b.q; a.r -= b.r; a.s -= b.s;
return a;
}
auto operator-(Hex const& a) -> Hex {
return {-a.q, -a.r, -a.s };
}
auto hex_distance(Hex hex) -> std::int64_t {
return (std::abs(hex.q) + std::abs(hex.r) + std::abs(hex.s)) / 2;
}
} // namespace
#endif