// Solutions to the Advent Of Code 2024. // Copyright (C) 2024 Stefan Müller // // This program is free software: you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free Software // Foundation, either version 3 of the License, or (at your option) any later // version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along with // this program. If not, see . #include const Point2 Point2::left{ -1, 0 }; const Point2 Point2::right{ 1, 0 }; const Point2 Point2::up{ 0, -1 }; const Point2 Point2::down{ 0, 1 }; const Point2 Point2::upLeft{ -1, -1 }; const Point2 Point2::upRight{ 1, -1 }; const Point2 Point2::downLeft{ -1, 1 }; const Point2 Point2::downRight{ 1, 1 }; const std::array Point2::directions = { Point2::down, Point2::downRight, Point2::right, Point2::upRight, Point2::up, Point2::upLeft, Point2::left, Point2::downLeft }; const std::array Point2::cardinalDirections = { Point2::down, Point2::right, Point2::up, Point2::left }; Point2 Point2::getCardinalDirection(const char directionChar) { switch (directionChar) { case 'v' : return Point2::down; case '>' : return Point2::right; case '^' : return Point2::up; case '<' : return Point2::left; default : return { 0, 0 }; } } Point2::Point2() : Point2{ 0, 0 } { } Point2::Point2(const int x, const int y) : x{ x }, y{ y } { } bool Point2::operator==(const Point2& rhs) const { return x == rhs.x && y == rhs.y; } bool Point2::operator!=(const Point2& rhs) const { return !(*this == rhs); } bool Point2::operator<(const Point2& rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y); } Point2 Point2::operator+(const Point2& rhs) const { return Point2(x + rhs.x, y + rhs.y); } Point2 Point2::operator-(const Point2& rhs) const { return Point2(x - rhs.x, y - rhs.y); } Point2 Point2::operator*(const int rhs) const { return Point2(x * rhs, y * rhs); } Point2 Point2::operator-() const { return Point2(-x, -y); } Point2& Point2::operator+=(const Point2& rhs) { *this = *this + rhs; return *this; } Point2& Point2::operator-=(const Point2& rhs) { *this = *this - rhs; return *this; } Point2& Point2::operator*=(const int rhs) { *this = *this * rhs; return *this; } int& Point2::operator[](size_t coordinateIndex) { return coordinateIndex == 0 ? x : y; } std::ostream& operator<<(std::ostream& os, const Point2& rhs) { os << "(" << rhs.x << ", " << rhs.y << ")"; return os; }