fuss with stepping

This commit is contained in:
Eric Mertens 2022-11-05 21:48:52 -07:00
parent d6727ab770
commit e23ce1f101
2 changed files with 15 additions and 19 deletions

View File

@ -22,9 +22,9 @@ 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 = Step(m);
if (auto p = std::get_if<Input>(&i)) {
p->pos = start;
auto e = Step(m);
if (auto i = std::get_if<Input>(&e)) {
i->pos = start;
start = StepOutput(m);
} else {
return {};

View File

@ -16,10 +16,10 @@ using Packet = std::pair<ValueType, ValueType>;
using Ethernet = std::deque<std::pair<std::size_t, Packet>>;
auto BuildNetwork(Machine m) -> std::vector<Machine> {
auto& i = std::get<Input>(Step(m)).pos;
std::vector<Machine> machines;
for (int i = 0; i < 50; i++) {
for (i = 0; i < 50; i++) {
machines.push_back(m);
StepInput(machines.back(), i);
}
return machines;
}
@ -57,26 +57,22 @@ auto main() -> int {
std::optional<Packet> nat;
for(;;) {
if (ethernet.empty()) {
if (nat) {
if (nat->second == part2) { break; }
part2 = nat->second;
Interact(ethernet, machines[0], nat);
} else {
for (auto && m : machines) {
Interact(ethernet, m, {});
}
}
} else {
if (!ethernet.empty()) {
auto [dest, p] = ethernet.front();
ethernet.pop_front();
if (dest == 255) {
nat = p;
if (!part1) { part1 = p.second; }
} else if (dest < 50) {
Interact(ethernet, machines[dest], p);
} else {
throw std::runtime_error{"bad destination"};
Interact(ethernet, machines.at(dest), p);
}
} else if (nat) {
if (nat->second == part2) { break; }
part2 = nat->second;
Interact(ethernet, machines[0], nat);
} else {
for (auto && m : machines) {
Interact(ethernet, m, {});
}
}
}