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

View File

@ -16,10 +16,10 @@ using Packet = std::pair<ValueType, ValueType>;
using Ethernet = std::deque<std::pair<std::size_t, Packet>>; using Ethernet = std::deque<std::pair<std::size_t, Packet>>;
auto BuildNetwork(Machine m) -> std::vector<Machine> { auto BuildNetwork(Machine m) -> std::vector<Machine> {
auto& i = std::get<Input>(Step(m)).pos;
std::vector<Machine> machines; std::vector<Machine> machines;
for (int i = 0; i < 50; i++) { for (i = 0; i < 50; i++) {
machines.push_back(m); machines.push_back(m);
StepInput(machines.back(), i);
} }
return machines; return machines;
} }
@ -57,8 +57,16 @@ auto main() -> int {
std::optional<Packet> nat; std::optional<Packet> nat;
for(;;) { for(;;) {
if (ethernet.empty()) { if (!ethernet.empty()) {
if (nat) { auto [dest, p] = ethernet.front();
ethernet.pop_front();
if (dest == 255) {
nat = p;
if (!part1) { part1 = p.second; }
} else {
Interact(ethernet, machines.at(dest), p);
}
} else if (nat) {
if (nat->second == part2) { break; } if (nat->second == part2) { break; }
part2 = nat->second; part2 = nat->second;
Interact(ethernet, machines[0], nat); Interact(ethernet, machines[0], nat);
@ -67,18 +75,6 @@ auto main() -> int {
Interact(ethernet, m, {}); Interact(ethernet, m, {});
} }
} }
} else {
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"};
}
}
} }
std::cout << "Part 1: " << *part1 << std::endl; std::cout << "Part 1: " << *part1 << std::endl;