jazz up Coord

This commit is contained in:
Eric Mertens 2022-11-04 21:27:08 -07:00
parent 48808dab97
commit 02a6d7ae31
9 changed files with 114 additions and 57 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/build /build
/release

View File

@ -4,7 +4,7 @@
#include <map> #include <map>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp> #include <intcode/Coord.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {
@ -25,7 +25,7 @@ auto Compute(Machine machine, ValueType start)
paint[here] = o; paint[here] = o;
} else { } else {
dir = (o ? CW : CCW)(dir); dir = (o ? CW : CCW)(dir);
here = Add(here, dir); here += dir;
} }
next_color = !next_color; next_color = !next_color;
}); });

View File

@ -4,7 +4,7 @@
#include <set> #include <set>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp> #include <intcode/Coord.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {

View File

@ -5,7 +5,7 @@
#include <utility> #include <utility>
#include <intcode/intcode.hpp> #include <intcode/intcode.hpp>
#include <intcode/Grid.hpp> #include <intcode/Coord.hpp>
using namespace intcode; using namespace intcode;
namespace { namespace {

View File

@ -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) target_include_directories(intcode PUBLIC include)

View File

@ -0,0 +1,71 @@
#ifndef INTCODE_COORD_HPP_
#define INTCODE_COORD_HPP_
#include <functional>
#include <map>
#include <ostream>
#include <tuple>
#include <intcode/Machine.hpp>
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<Coord, ValueType> 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<intcode::Coord>
{
auto operator()(intcode::Coord const& c) const noexcept -> std::size_t {
std::hash<std::int64_t> h;
return h(c.x) ^ h(c.y);
}
};
#endif

View File

@ -1,27 +0,0 @@
#ifndef INTCODE_GRID_HPP_
#define INTCODE_GRID_HPP_
#include <ostream>
#include <map>
#include <tuple>
#include <intcode/Machine.hpp>
namespace intcode {
using Coord = std::pair<ValueType, ValueType>;
auto Draw(std::ostream & out, std::map<Coord, ValueType> 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

View File

@ -1,4 +1,4 @@
#include <intcode/Grid.hpp> #include <intcode/Coord.hpp>
namespace intcode { namespace intcode {
@ -30,39 +30,61 @@ auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void {
} }
auto Up(Coord c) -> Coord { auto Up(Coord c) -> Coord {
c.second -= 1; c.y -= 1;
return c; return c;
} }
auto Down(Coord c) -> Coord { auto Down(Coord c) -> Coord {
c.second += 1; c.y += 1;
return c; return c;
} }
auto Left(Coord c) -> Coord { auto Left(Coord c) -> Coord {
c.first -= 1; c.x -= 1;
return c; return c;
} }
auto Right(Coord c) -> Coord { auto Right(Coord c) -> Coord {
c.first += 1; c.x += 1;
return c; return c;
} }
auto Invert(Coord c) -> Coord {
return {c.y, c.x};
}
auto CW(Coord c) -> Coord { auto CW(Coord c) -> Coord {
std::swap(c.first, c.second); c = Invert(c);
c.first = -c.first; c.x = -c.x;
return c; return c;
} }
auto CCW(Coord c) -> Coord { auto CCW(Coord c) -> Coord {
std::swap(c.first, c.second); c = Invert(c);
c.second = -c.second; c.y = -c.y;
return c; return c;
} }
auto Add(Coord x, Coord y) -> Coord { auto operator+(Coord a, Coord b) -> Coord {
return {x.first + y.first, x.second + y.second}; 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 } // namespace

View File

@ -26,18 +26,12 @@ auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
switch (instruction % 100) { switch (instruction % 100) {
case 1: { case 1: {
auto a = arg(); arg() = arg() + arg();
auto b = arg();
auto& c = arg();
c = a + b;
break; break;
} }
case 2: { case 2: {
auto a = arg(); arg() = arg() * arg();
auto b = arg();
auto& c = arg();
c = a * b;
break; break;
} }
@ -66,16 +60,12 @@ auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
case 7: { case 7: {
auto a = arg(); auto a = arg();
auto b = arg(); auto b = arg();
auto& c = arg(); arg() = a < b; // order matters
c = a < b;
break; break;
} }
case 8: { case 8: {
auto a = arg(); arg() = arg() == arg();
auto b = arg();
auto& c = arg();
c = a == b;
break; break;
} }