2017-12
This commit is contained in:
57
lib/include/aocpp/Hex.hpp
Normal file
57
lib/include/aocpp/Hex.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#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
|
Reference in New Issue
Block a user