make a common lib for non-intcode problems
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include <intcode/Coord.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
|
||||
namespace intcode {
|
||||
namespace aocpp {
|
||||
|
||||
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void {
|
||||
ValueType minX = 0, maxX = 0, minY = 0, maxY = 0;
|
||||
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> image) -> void {
|
||||
std::int64_t minX = 0, maxX = 0, minY = 0, maxY = 0;
|
||||
for (auto&& [k, _] : image) {
|
||||
auto [x,y] = k;
|
||||
minX = std::min(minX, x);
|
||||
@@ -11,8 +11,8 @@ auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void {
|
||||
minY = std::min(minY, y);
|
||||
maxY = std::max(maxY, y);
|
||||
}
|
||||
for (ValueType y = minY; y <= maxY; y++) {
|
||||
for (ValueType x = minX; x <= maxX; x++) {
|
||||
for (std::int64_t y = minY; y <= maxY; y++) {
|
||||
for (std::int64_t x = minX; x <= maxX; x++) {
|
||||
auto it = image.find({x,y});
|
||||
if (image.end() == it) {
|
||||
out << "░";
|
||||
|
@@ -1,93 +0,0 @@
|
||||
#include <intcode/Interpreter.hpp>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
BadInstruction::BadInstruction(char const* what)
|
||||
: std::runtime_error{what} {}
|
||||
|
||||
auto StepInput(Machine & m, ValueType input) -> void {
|
||||
std::get<Input>(Step(m)).input = input;
|
||||
}
|
||||
|
||||
auto StepOutput(Machine & m) -> ValueType {
|
||||
return std::get<Output>(Step(m)).output;
|
||||
}
|
||||
|
||||
auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
|
||||
for (;;) {
|
||||
auto instruction = m.Next();
|
||||
auto modes = instruction / 10;
|
||||
|
||||
auto arg = [&]() -> ValueType & {
|
||||
auto &v = m.Next();
|
||||
switch ((modes /= 10) % 10) {
|
||||
case 0:
|
||||
return m.At(v);
|
||||
case 1:
|
||||
return v;
|
||||
case 2:
|
||||
return m.Rel(v);
|
||||
default:
|
||||
throw BadInstruction{"invalid addressing mode"};
|
||||
}
|
||||
};
|
||||
|
||||
switch (instruction % 100) {
|
||||
case 1: {
|
||||
arg() = arg() + arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: {
|
||||
arg() = arg() * arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: {
|
||||
return Input{arg()};
|
||||
}
|
||||
|
||||
case 4: {
|
||||
return Output{arg()};
|
||||
}
|
||||
|
||||
case 5: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
if (a) { m.Goto(b); }
|
||||
break;
|
||||
}
|
||||
|
||||
case 6: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
if (!a) { m.Goto(b); }
|
||||
break;
|
||||
}
|
||||
|
||||
case 7: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
arg() = a < b; // order matters
|
||||
break;
|
||||
}
|
||||
|
||||
case 8: {
|
||||
arg() = arg() == arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 9:
|
||||
m.Rebase(arg());
|
||||
break;
|
||||
|
||||
case 99:
|
||||
return Halt{};
|
||||
|
||||
default:
|
||||
throw BadInstruction{"invalid opcode"};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
@@ -1,30 +0,0 @@
|
||||
#include <intcode/Machine.hpp>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
Machine::Machine(std::vector<ValueType> ram)
|
||||
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
auto Machine::At(std::size_t i) -> ValueType & {
|
||||
return i < rom_.size() ? rom_[i] : ram_[i];
|
||||
}
|
||||
|
||||
auto Machine::Rel(std::size_t i) -> ValueType & {
|
||||
return At(base_ + i);
|
||||
}
|
||||
|
||||
auto Machine::Rebase(std::size_t offset) -> void {
|
||||
base_ += offset;
|
||||
}
|
||||
|
||||
auto Machine::Next() -> ValueType & {
|
||||
return At(pc_++);
|
||||
}
|
||||
|
||||
auto Machine::Goto(std::size_t address) -> void {
|
||||
pc_ = address;
|
||||
}
|
||||
|
||||
} // namespace
|
@@ -1,17 +0,0 @@
|
||||
#include <intcode/Parser.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
|
||||
ValueType x;
|
||||
std::string str;
|
||||
std::vector<ValueType> result;
|
||||
while (std::getline(in, str, ',')) {
|
||||
result.push_back(std::stol(str));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
17
lib/src/Startup.cpp
Normal file
17
lib/src/Startup.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace aocpp {
|
||||
|
||||
auto Startup(int argc, char ** argv) -> std::ifstream {
|
||||
if (argc != 2) {
|
||||
std::cerr << "Expected input file argument\n";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return std::ifstream{argv[1]};
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user