advent-of-code/include/aoc.hpp

100 lines
1.9 KiB
C++
Raw Normal View History

2024-12-16 01:17:56 -08:00
#pragma once
#include <array>
#include <functional>
2024-12-16 01:17:56 -08:00
typedef long long ll;
typedef unsigned long long ull;
template <typename T> struct Coord {
T x, y;
Coord() : Coord(0, 0) {}
Coord(const T x, const T y) {
this->x = x;
this->y = y;
}
static inline const std::array<Coord<T>, 4> units() {
return {
Coord<T>(0, 1),
Coord<T>(1, 0),
Coord<T>(0, -1),
Coord<T>(-1, 0),
};
}
2024-12-16 01:17:56 -08:00
Coord<T> operator-() { return {-this->x, -this->y}; }
Coord<T> &operator+=(const Coord<T> &other) {
x += other.x;
y += other.y;
return *this;
}
Coord<T> &operator-=(const Coord<T> &other) {
x -= other.x;
y -= other.y;
return *this;
}
Coord<T> &operator*=(const T other) {
x *= other;
y *= other;
return *this;
}
Coord<T> &operator/=(const T other) {
x /= other;
y /= other;
return *this;
}
};
template <typename T>
Coord<T> operator+(const Coord<T> &lhs, const Coord<T> &rhs) {
Coord<T> result = lhs;
result += rhs;
return result;
}
template <typename T>
Coord<T> operator-(const Coord<T> &lhs, const Coord<T> &rhs) {
Coord<T> result = lhs;
result -= rhs;
return result;
}
template <typename T> Coord<T> operator*(const Coord<T> &lhs, const T rhs) {
Coord<T> result = lhs;
result *= rhs;
return result;
}
template <typename T> Coord<T> operator/(const Coord<T> &lhs, const T rhs) {
Coord<T> result = lhs;
result /= rhs;
return result;
}
template <typename T>
inline bool operator==(const Coord<T> &lhs, const Coord<T> &rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
}
template <> struct std::hash<Coord<int>> {
std::size_t operator()(const Coord<int> &c) const noexcept {
return (c.x << (sizeof(std::size_t) / 2)) - c.x + c.y;
}
};
template <> struct std::hash<Coord<ull>> {
std::size_t operator()(const Coord<ull> &c) const noexcept {
return (c.x << (sizeof(std::size_t) / 2)) - c.x + c.y;
}
};
typedef Coord<int> icoord;
typedef Coord<ull> ullcoord;