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) { 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(); Step(machine);
return machine.At(0); return machine.At(0);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -36,9 +36,6 @@ public:
Machine(); Machine();
explicit Machine(std::vector<ValueType> ram); explicit Machine(std::vector<ValueType> ram);
/// Advance machine until next side effect
auto Step() -> std::variant<Input, Output, Halt>;
/// Access memory at absolute address /// Access memory at absolute address
/// @param address /// @param address
/// @return reference to memory at given address /// @return reference to memory at given address
@ -48,8 +45,14 @@ public:
/// @param offset from base pointer /// @param offset from base pointer
/// @return reference to memory at given offset /// @return reference to memory at given offset
auto Rel(std::size_t offset) -> ValueType &; 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 { struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what); 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) Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {} : rom_{ram}, ram_{}, pc_{0}, base_{0} {}
namespace { auto Machine::At(std::size_t i) -> ValueType & {
auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p) return i < rom_.size() ? rom_[i] : ram_[i];
-> ValueType & { }
auto &v = m.At(k);
switch (instruction / p % 10) { 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: case 0:
return m.At(v); return m.At(v);
case 1: case 1:
@ -25,68 +49,65 @@ auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
default: default:
throw BadInstruction{"invalid addressing mode"}; 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) { switch (instruction % 100) {
case 1: case 1: {
c() = a() + b(); auto a = arg();
pc_ += 4; auto b = arg();
auto& c = arg();
c = a + b;
break; break;
}
case 2: case 2: {
c() = a() * b(); auto a = arg();
pc_ += 4; auto b = arg();
auto& c = arg();
c = a * b;
break; break;
}
case 3: { case 3: {
auto &pos = a(); return Input{arg()};
pc_ += 2;
return Input{pos};
} }
case 4: { case 4: {
auto val = a(); return Output{arg()};
pc_ += 2;
return Output{val};
} }
case 5: case 5: {
pc_ = a() ? b() : pc_ + 3; auto a = arg();
auto b = arg();
if (a) { m.Goto(b); }
break; break;
}
case 6: case 6: {
pc_ = a() ? pc_ + 3 : b(); auto a = arg();
auto b = arg();
if (!a) { m.Goto(b); }
break; break;
}
case 7: case 7: {
c() = a() < b(); auto a = arg();
pc_ += 4; auto b = arg();
auto& c = arg();
c = a < b;
break; break;
}
case 8: case 8: {
c() = a() == b(); auto a = arg();
pc_ += 4; auto b = arg();
auto& c = arg();
c = a == b;
break; break;
}
case 9: case 9:
base_ += a(); m.Rebase(arg());
pc_ += 2;
break; break;
case 99: case 99: