diff --git a/.gitignore b/.gitignore index 796b96d..956c004 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /build +/release \ No newline at end of file diff --git a/day11.cpp b/day11.cpp index 5d7315f..4c43d40 100644 --- a/day11.cpp +++ b/day11.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include using namespace intcode; namespace { @@ -25,7 +25,7 @@ auto Compute(Machine machine, ValueType start) paint[here] = o; } else { dir = (o ? CW : CCW)(dir); - here = Add(here, dir); + here += dir; } next_color = !next_color; }); diff --git a/day13.cpp b/day13.cpp index 02f150e..cb9b659 100644 --- a/day13.cpp +++ b/day13.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day15.cpp b/day15.cpp index 9753cb9..6877204 100644 --- a/day15.cpp +++ b/day15.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 8a44164..dcf7c18 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(intcode src/Grid.cpp src/Parser.cpp src/Interpreter.cpp src/Machine.cpp) +add_library(intcode src/Coord.cpp src/Parser.cpp src/Interpreter.cpp src/Machine.cpp) target_include_directories(intcode PUBLIC include) diff --git a/lib/include/intcode/Coord.hpp b/lib/include/intcode/Coord.hpp new file mode 100644 index 0000000..712b9be --- /dev/null +++ b/lib/include/intcode/Coord.hpp @@ -0,0 +1,71 @@ +#ifndef INTCODE_COORD_HPP_ +#define INTCODE_COORD_HPP_ + +#include +#include +#include +#include + +#include + +namespace intcode { + +/// 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 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; + +/// 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 +{ + auto operator()(intcode::Coord const& c) const noexcept -> std::size_t { + std::hash h; + return h(c.x) ^ h(c.y); + } +}; + +#endif diff --git a/lib/include/intcode/Grid.hpp b/lib/include/intcode/Grid.hpp deleted file mode 100644 index 43ae0a7..0000000 --- a/lib/include/intcode/Grid.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef INTCODE_GRID_HPP_ -#define INTCODE_GRID_HPP_ - -#include -#include -#include - -#include - -namespace intcode { - -using Coord = std::pair; - -auto Draw(std::ostream & out, std::map image) -> void; - -auto Up(Coord) -> Coord; -auto Down(Coord) -> Coord; -auto Left(Coord) -> Coord; -auto Right(Coord) -> Coord; -auto CW(Coord) -> Coord; -auto CCW(Coord) -> Coord; -auto Add(Coord, Coord) -> Coord; - - -} // namespace - -#endif diff --git a/lib/src/Grid.cpp b/lib/src/Coord.cpp similarity index 63% rename from lib/src/Grid.cpp rename to lib/src/Coord.cpp index efd6b3a..10bfc36 100644 --- a/lib/src/Grid.cpp +++ b/lib/src/Coord.cpp @@ -1,4 +1,4 @@ -#include +#include namespace intcode { @@ -30,39 +30,61 @@ auto Draw(std::ostream & out, std::map image) -> void { } auto Up(Coord c) -> Coord { - c.second -= 1; + c.y -= 1; return c; } auto Down(Coord c) -> Coord { - c.second += 1; + c.y += 1; return c; } auto Left(Coord c) -> Coord { - c.first -= 1; + c.x -= 1; return c; } auto Right(Coord c) -> Coord { - c.first += 1; + c.x += 1; return c; } +auto Invert(Coord c) -> Coord { + return {c.y, c.x}; +} + auto CW(Coord c) -> Coord { - std::swap(c.first, c.second); - c.first = -c.first; + c = Invert(c); + c.x = -c.x; return c; } auto CCW(Coord c) -> Coord { - std::swap(c.first, c.second); - c.second = -c.second; + c = Invert(c); + c.y = -c.y; return c; } -auto Add(Coord x, Coord y) -> Coord { - return {x.first + y.first, x.second + y.second}; +auto operator+(Coord a, Coord b) -> Coord { + return {a.x + b.x, a.y + b.y}; +} + +auto operator-(Coord a, Coord b) -> Coord { + return {a.x - b.x, a.y - b.y}; +} + +auto operator+=(Coord & a, Coord b) -> Coord & { + a = a + b; + return a; +} + +auto operator-=(Coord & a, Coord b) -> Coord & { + a = a - b; + return a; +} + +auto operator<<(std::ostream & out, Coord c) -> std::ostream & { + return out << "(" << c.x << "," << c.y << ")"; } } // namespace diff --git a/lib/src/Interpreter.cpp b/lib/src/Interpreter.cpp index 59df270..eafc7a8 100644 --- a/lib/src/Interpreter.cpp +++ b/lib/src/Interpreter.cpp @@ -26,18 +26,12 @@ auto Step(Machine & m) -> std::variant { switch (instruction % 100) { case 1: { - auto a = arg(); - auto b = arg(); - auto& c = arg(); - c = a + b; + arg() = arg() + arg(); break; } case 2: { - auto a = arg(); - auto b = arg(); - auto& c = arg(); - c = a * b; + arg() = arg() * arg(); break; } @@ -66,16 +60,12 @@ auto Step(Machine & m) -> std::variant { case 7: { auto a = arg(); auto b = arg(); - auto& c = arg(); - c = a < b; + arg() = a < b; // order matters break; } case 8: { - auto a = arg(); - auto b = arg(); - auto& c = arg(); - c = a == b; + arg() = arg() == arg(); break; }