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
/release

View File

@ -4,7 +4,7 @@
#include <map>
#include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
#include <intcode/Coord.hpp>
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;
});

View File

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

View File

@ -5,7 +5,7 @@
#include <utility>
#include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
#include <intcode/Coord.hpp>
using namespace intcode;
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)

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 {
@ -30,39 +30,61 @@ auto Draw(std::ostream & out, std::map<Coord, ValueType> 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

View File

@ -26,18 +26,12 @@ auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
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<Input, Output, Halt> {
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;
}