fuss with stepping
This commit is contained in:
parent
d6727ab770
commit
e23ce1f101
|
@ -22,9 +22,9 @@ template <class R> auto Controller(Machine machine, R const ¶ms) {
|
|||
template <class R>
|
||||
auto Feed(R &&s, 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 {};
|
||||
|
|
28
day23.cpp
28
day23.cpp
|
@ -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, {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user