This commit is contained in:
Eric Mertens 2022-11-03 14:43:47 -07:00
parent 40f58503dc
commit 898ae0521b
6 changed files with 83 additions and 74 deletions

View File

@ -7,23 +7,23 @@ using namespace intcode;
namespace { namespace {
auto compute(Machine machine, value_type x, value_type y) { auto Compute(Machine machine, ValueType x, ValueType y) {
machine.at(1) = x; machine.At(1) = x;
machine.at(2) = y; machine.At(2) = y;
machine.step(); machine.Step();
return machine.at(0); return machine.At(0);
} }
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{parse_stream(std::cin)}; auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << compute(machine, 12, 2) << std::endl; std::cout << "Part 1: " << Compute(machine, 12, 2) << std::endl;
for (std::int64_t i = 0; i < 100; i++) { for (std::int64_t i = 0; i < 100; i++) {
for (std::int64_t j = 0; j < 100; j++) { for (std::int64_t j = 0; j < 100; j++) {
if (19690720 == compute(machine, i, j)) { if (19690720 == Compute(machine, i, j)) {
std::cout << "Part 2: " << 100 * i + j << std::endl; std::cout << "Part 2: " << 100 * i + j << std::endl;
} }
} }

View File

@ -6,8 +6,8 @@ using namespace intcode;
namespace { namespace {
auto compute(Machine machine, value_type d) -> value_type { auto Compute(Machine machine, ValueType d) -> ValueType {
value_type last_output = -1; ValueType last_output = -1;
while (std::visit(overloaded{ while (std::visit(overloaded{
[](Halt) { return false; }, [](Halt) { return false; },
[d, &machine](Input arg) { [d, &machine](Input arg) {
@ -18,14 +18,14 @@ auto compute(Machine machine, value_type d) -> value_type {
last_output = arg.val; last_output = arg.val;
return true; return true;
}}, }},
machine.step())); machine.Step()));
return last_output; return last_output;
} }
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{parse_stream(std::cin)}; auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << compute(machine, 1) << std::endl; std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << compute(std::move(machine), 5) << std::endl; std::cout << "Part 2: " << Compute(std::move(machine), 5) << std::endl;
} }

View File

@ -10,18 +10,18 @@ using namespace intcode;
namespace { namespace {
template <class R> auto set_params(Machine machine, R const &params) { template <class R> auto Controller(Machine machine, R const &params) {
std::vector<Machine> amps; std::vector<Machine> amps;
for (auto p : params) { for (auto p : params) {
auto m = machine; auto m = machine;
std::get<Input>(m.step()).pos = p; std::get<Input>(m.Step()).pos = p;
amps.push_back(std::move(m)); amps.push_back(std::move(m));
} }
return amps; return amps;
} }
template <class R> template <class R>
auto feed(R &&amps, value_type start) -> std::optional<value_type> { auto Feed(R &&amps, ValueType start) -> std::optional<ValueType> {
for (auto &m : amps) { for (auto &m : amps) {
auto i = m.step(); auto i = m.step();
if (auto p = std::get_if<Input>(&i)) { if (auto p = std::get_if<Input>(&i)) {
@ -34,23 +34,23 @@ auto feed(R &&amps, value_type start) -> std::optional<value_type> {
return {start}; return {start};
} }
auto compute1(Machine machine, std::vector<value_type> const &params) auto compute1(Machine machine, std::vector<ValueType> const &params)
-> value_type { -> ValueType {
return *feed(set_params(std::move(machine), params), 0); return *Feed(Controller(std::move(machine), params), 0);
} }
auto compute2(Machine machine, std::vector<value_type> const &params) auto compute2(Machine machine, std::vector<ValueType> const &params)
-> value_type { -> ValueType {
auto amps = set_params(std::move(machine), params); auto amps = Controller(std::move(machine), params);
value_type last = 0; ValueType last = 0;
while (auto next = feed(amps, last)) { while (auto next = Feed(amps, last)) {
last = *next; last = *next;
} }
return last; return last;
} }
template <std::invocable<Machine, std::vector<value_type> const &> F> template <std::invocable<Machine, std::vector<ValueType> const &> F>
auto optimize(Machine machine, std::vector<value_type> params, F f) { auto optimize(Machine machine, std::vector<ValueType> params, F f) {
value_type best = 0; ValueType best = 0;
do { do {
best = std::max(best, f(machine, params)); best = std::max(best, f(machine, params));
} while (std::next_permutation(params.begin(), params.end())); } while (std::next_permutation(params.begin(), params.end()));
@ -60,7 +60,7 @@ auto optimize(Machine machine, std::vector<value_type> params, F f) {
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{parse_stream(std::cin)}; auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << optimize(machine, {0, 1, 2, 3, 4}, compute1) << std::endl; std::cout << "Part 1: " << optimize(machine, {0, 1, 2, 3, 4}, compute1) << std::endl;
std::cout << "Part 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl; std::cout << "Part 2: " << optimize(std::move(machine), {5, 6, 7, 8, 9}, compute2) << std::endl;
} }

View File

@ -6,8 +6,8 @@ using namespace intcode;
namespace { namespace {
auto compute(Machine machine, value_type d) -> value_type { auto Compute(Machine machine, ValueType d) -> ValueType {
value_type output = -1; ValueType output = -1;
while (std::visit(overloaded{ while (std::visit(overloaded{
[](Halt) { return false; }, [](Halt) { return false; },
[d](Input arg) { [d](Input arg) {
@ -18,14 +18,14 @@ auto compute(Machine machine, value_type d) -> value_type {
output = arg.val; output = arg.val;
return false; return false;
}}, }},
machine.step())); machine.Step()));
return output; return output;
} }
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{parse_stream(std::cin)}; auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << compute(machine, 1) << std::endl; std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << compute(std::move(machine), 2) << std::endl; std::cout << "Part 2: " << Compute(std::move(machine), 2) << std::endl;
} }

View File

@ -5,6 +5,7 @@
#include <cstdint> #include <cstdint>
#include <istream> #include <istream>
#include <unordered_map> #include <unordered_map>
#include <stdexcept>
#include <variant> #include <variant>
#include <vector> #include <vector>
@ -13,43 +14,47 @@ template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
namespace intcode { namespace intcode {
using value_type = std::int64_t; using ValueType = std::int64_t;
struct Input { struct Input {
value_type &pos; ValueType &pos;
}; };
struct Output { struct Output {
value_type val; ValueType val;
}; };
struct Halt {}; struct Halt {};
class Machine { class Machine {
std::vector<value_type> rom_; std::vector<ValueType> rom_;
std::unordered_map<std::size_t, value_type> ram_; std::unordered_map<std::size_t, ValueType> ram_;
std::size_t pc_; std::size_t pc_;
std::size_t base_; std::size_t base_;
auto ref(value_type instruction, value_type p, std::size_t offset)
-> value_type &;
public: public:
Machine(); Machine();
explicit Machine(std::vector<value_type> ram); explicit Machine(std::vector<ValueType> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> value_type &;
/// Advance machine until next side effect
auto Step() -> std::variant<Input, Output, Halt>;
/// Access memory at absolute address
/// @param address
/// @return reference to memory at given address
auto At(std::size_t address) -> ValueType &;
/// Access memory at address relative to base pointer
/// @param offset from base pointer
/// @return reference to memory at given offset
auto Rel(std::size_t offset) -> ValueType &;
}; };
struct BadInstruction : public std::exception { struct BadInstruction : public std::runtime_error {
std::size_t pc; explicit BadInstruction(char const* what);
value_type instruction;
explicit BadInstruction(std::size_t pc, value_type instruction);
const char *what() const noexcept override;
}; };
auto parse_stream(std::istream &in) -> std::vector<value_type>; auto ParseStream(std::istream &in) -> std::vector<ValueType>;
} }

View File

@ -3,41 +3,45 @@
namespace intcode { namespace intcode {
BadInstruction::BadInstruction(std::size_t pc, value_type instruction) BadInstruction::BadInstruction(char const* what)
: pc{pc}, instruction{instruction} {} : std::runtime_error{what} {}
char const *BadInstruction::what() const noexcept { return "bad instruction"; }
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {} Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<value_type> ram) Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {} : rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto Machine::ref(value_type instruction, value_type p, std::size_t offset) namespace {
-> value_type & { auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
auto &i = at(pc_ + offset); -> ValueType & {
auto &v = m.At(k);
switch (instruction / p % 10) { switch (instruction / p % 10) {
case 0: case 0:
return at(i); return m.At(v);
case 1: case 1:
return i; return v;
case 2: case 2:
return at(base_ + i); return m.At(m.Rel(v));
default: default:
throw BadInstruction{pc_, instruction}; throw BadInstruction{"invalid addressing mode"};
}
} }
} }
auto Machine::at(std::size_t i) -> value_type & { auto Machine::At(std::size_t i) -> ValueType & {
return i < rom_.size() ? rom_[i] : ram_[i]; return i < rom_.size() ? rom_[i] : ram_[i];
} }
auto Machine::step() -> std::variant<Input, Output, Halt> { auto Machine::Rel(std::size_t i) -> ValueType & {
return At(base_ + i);
}
auto Machine::Step() -> std::variant<Input, Output, Halt> {
for (;;) { for (;;) {
auto instruction = at(pc_); auto instruction = At(pc_);
auto a = [=]() -> auto & { return ref(instruction, 100, 1); }; auto a = [=]() -> auto & { return Ref(*this, instruction, pc_+1, 100); };
auto b = [=]() -> auto & { return ref(instruction, 1000, 2); }; auto b = [=]() -> auto & { return Ref(*this, instruction, pc_+2, 1000); };
auto c = [=]() -> auto & { return ref(instruction, 10000, 3); }; auto c = [=]() -> auto & { return Ref(*this, instruction, pc_+3, 10000); };
switch (instruction % 100) { switch (instruction % 100) {
case 1: case 1:
@ -89,15 +93,15 @@ auto Machine::step() -> std::variant<Input, Output, Halt> {
return Halt{}; return Halt{};
default: default:
throw BadInstruction{pc_, instruction}; throw BadInstruction{"invalid opcode"};
} }
} }
} }
auto parse_stream(std::istream &in) -> std::vector<value_type> { auto ParseStream(std::istream &in) -> std::vector<ValueType> {
value_type x; ValueType x;
std::string str; std::string str;
std::vector<value_type> result; std::vector<ValueType> result;
while (std::getline(in, str, ',')) { while (std::getline(in, str, ',')) {
result.push_back(std::stoi(str)); result.push_back(std::stoi(str));
} }