This commit is contained in:
Eric Mertens 2022-11-04 08:29:02 -07:00
parent e88863ed37
commit fd39209ff0
9 changed files with 136 additions and 92 deletions

View File

@ -10,7 +10,7 @@ namespace {
auto Compute(Machine machine, ValueType x, ValueType y) {
machine.At(1) = x;
machine.At(2) = y;
machine.Step();
Step(machine);
return machine.At(0);
}

View File

@ -18,7 +18,7 @@ auto Compute(Machine machine, ValueType d) -> ValueType {
last_output = arg.val;
return true;
}},
machine.Step()));
Step(machine)));
return last_output;
}

View File

@ -14,7 +14,7 @@ template <class R> auto Controller(Machine machine, R const &params) {
std::vector<Machine> amps;
for (auto p : params) {
auto m = machine;
std::get<Input>(m.Step()).pos = p;
std::get<Input>(Step(m)).pos = p;
amps.push_back(std::move(m));
}
return amps;
@ -23,10 +23,10 @@ template <class R> auto Controller(Machine machine, R const &params) {
template <class R>
auto Feed(R &&amps, ValueType start) -> std::optional<ValueType> {
for (auto &m : amps) {
auto i = m.Step();
auto i = Step(m);
if (auto p = std::get_if<Input>(&i)) {
p->pos = start;
start = std::get<Output>(m.Step()).val;
start = std::get<Output>(Step(m)).val;
} else {
return {};
}

View File

@ -18,7 +18,7 @@ auto Compute(Machine machine, ValueType d) -> ValueType {
output = arg.val;
return false;
}},
machine.Step()));
Step(machine)));
return output;
}

View File

@ -19,15 +19,15 @@ auto Compute(Machine machine, ValueType start)
ValueType dy {-1};
for (;;) {
auto effect = machine.Step();
auto effect = Step(machine);
if (std::holds_alternative<Halt>(effect)) {
return paint;
}
std::get<Input>(effect).pos = paint[here];
paint[here] = std::get<Output>(machine.Step()).val;
auto dir = std::get<Output>(machine.Step()).val;
paint[here] = std::get<Output>(Step(machine)).val;
auto dir = std::get<Output>(Step(machine)).val;
std::swap(dx, dy);
if (dir) {

View File

@ -15,13 +15,13 @@ auto Compute1(Machine machine)
std::set<Coord> screen;
for (;;) {
auto effect = machine.Step();
auto effect = Step(machine);
if (std::holds_alternative<Halt>(effect)) {
return screen.size();
}
auto x = std::get<Output>(effect).val;
auto y = std::get<Output>(machine.Step()).val;
auto v = std::get<Output>(machine.Step()).val;
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).val;
if (2 == v) { screen.insert({x,y}); } else { screen.erase({x,y}); }
}
@ -34,7 +34,7 @@ auto Compute2(Machine machine) {
machine.At(0) = 2;
for(;;) {
auto effect = machine.Step();
auto effect = Step(machine);
if (std::holds_alternative<Halt>(effect)) {
return score;
} else if (auto i = std::get_if<Input>(&effect)) {
@ -42,8 +42,8 @@ auto Compute2(Machine machine) {
: paddleX > ballX ? -1 : 0;
} else {
auto x = std::get<Output>(effect).val;
auto y = std::get<Output>(machine.Step()).val;
auto v = std::get<Output>(machine.Step()).val;
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).val;
if (-1 == x && 0 == y) {
score = v;

View File

@ -4,6 +4,7 @@
#include <tuple>
#include <map>
#include <deque>
#include <vector>
#include <intcode.hpp>
using namespace intcode;
@ -13,46 +14,65 @@ namespace {
using Coord = std::pair<ValueType, ValueType>;
auto Compute(Machine machine) -> std::pair<int,int> {
int part1 = 0;
int part2 = 0;
Coord here {0,0};
std::map<Coord, ValueType> world;
std::map<Coord, ValueType> world {{{0,0},1}};
std::deque<std::tuple<int, Coord, Machine>> todo {{0, {0,0}, std::move(machine)}};
using State = std::tuple<Coord, Machine>;
auto action = [&world, &todo](Machine m, int steps, ValueType cmd, Coord coord) {
std::vector<State> layer {{{0,0}, std::move(machine)}};
std::vector<State> next_layer;
auto action = [&](Machine m, ValueType cmd, Coord coord) {
if (!world.contains(coord)) {
std::get<Input>(m.Step()).pos = cmd;
auto o = std::get<Output>(m.Step()).val;
std::get<Input>(Step(m)).pos = cmd;
auto o = std::get<Output>(Step(m)).val;
world[coord] = o;
if (o) {
todo.emplace_back(steps + 1, coord, std::move(m));
next_layer.emplace_back(coord, std::move(m));
}
}
};
while (!todo.empty()) {
auto [steps, here, machine] = todo.front();
auto [x,y] = here;
todo.pop_front();
if (2 == world[here]) {
part1 = steps;
todo.clear();
int part1 = 0;
while (!layer.empty()) {
for (auto && s : layer) {
auto& [loc, m] = s;
if (2 == world[loc]) {
world.clear();
world[here] = 2;
steps = 0;
part2 = 0;
world[loc] = 2;
auto start = std::move(m);
layer.clear();
layer.push_back({loc, std::move(start)});
goto loop2;
}
auto [x,y] = loc;
action(m, 1, {x,y-1});
action(m, 2, {x,y+1});
action(m, 3, {x-1,y});
action(std::move(m), 4, {x+1,y});
}
layer = std::move(next_layer);
next_layer.clear();
part1++;
}
part2 = std::max(part2, steps);
action(machine, steps, 1, {x,y-1});
action(machine, steps, 2, {x,y+1});
action(machine, steps, 3, {x-1,y});
action(std::move(machine), steps, 4, {x+1,y});
loop2:
int part2 = 0;
while (!layer.empty()) {
for (auto && s : layer) {
auto& [loc, m] = s;
auto [x,y] = loc;
action(m, 1, {x,y-1});
action(m, 2, {x,y+1});
action(m, 3, {x-1,y});
action(std::move(m), 4, {x+1,y});
}
return {part1,part2};
layer = std::move(next_layer);
next_layer.clear();
part2++;
}
return {part1, part2-1};
}
} // namespace

View File

@ -36,9 +36,6 @@ public:
Machine();
explicit Machine(std::vector<ValueType> ram);
/// 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
@ -48,8 +45,14 @@ public:
/// @param offset from base pointer
/// @return reference to memory at given offset
auto Rel(std::size_t offset) -> ValueType &;
auto Next() -> ValueType &;
auto Goto(std::size_t address) -> void;
auto Rebase(std::size_t offset) -> void;
};
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};

View File

@ -11,11 +11,35 @@ Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
namespace {
auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
-> ValueType & {
auto &v = m.At(k);
switch (instruction / p % 10) {
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;
}
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:
@ -25,68 +49,65 @@ auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
default:
throw BadInstruction{"invalid addressing mode"};
}
}
}
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::Step() -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = At(pc_);
auto a = [=]() -> auto & { return Ref(*this, instruction, pc_+1, 100); };
auto b = [=]() -> auto & { return Ref(*this, instruction, pc_+2, 1000); };
auto c = [=]() -> auto & { return Ref(*this, instruction, pc_+3, 10000); };
};
switch (instruction % 100) {
case 1:
c() = a() + b();
pc_ += 4;
case 1: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a + b;
break;
}
case 2:
c() = a() * b();
pc_ += 4;
case 2: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a * b;
break;
}
case 3: {
auto &pos = a();
pc_ += 2;
return Input{pos};
return Input{arg()};
}
case 4: {
auto val = a();
pc_ += 2;
return Output{val};
return Output{arg()};
}
case 5:
pc_ = a() ? b() : pc_ + 3;
case 5: {
auto a = arg();
auto b = arg();
if (a) { m.Goto(b); }
break;
}
case 6:
pc_ = a() ? pc_ + 3 : b();
case 6: {
auto a = arg();
auto b = arg();
if (!a) { m.Goto(b); }
break;
}
case 7:
c() = a() < b();
pc_ += 4;
case 7: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a < b;
break;
}
case 8:
c() = a() == b();
pc_ += 4;
case 8: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a == b;
break;
}
case 9:
base_ += a();
pc_ += 2;
m.Rebase(arg());
break;
case 99: