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>
|
template <class R>
|
||||||
auto Feed(R &&s, ValueType start) -> std::optional<ValueType> {
|
auto Feed(R &&s, 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 {};
|
||||||
|
|
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>>;
|
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,26 +57,22 @@ auto main() -> int {
|
||||||
std::optional<Packet> nat;
|
std::optional<Packet> nat;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if (ethernet.empty()) {
|
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 {
|
|
||||||
auto [dest, p] = ethernet.front();
|
auto [dest, p] = ethernet.front();
|
||||||
ethernet.pop_front();
|
ethernet.pop_front();
|
||||||
if (dest == 255) {
|
if (dest == 255) {
|
||||||
nat = p;
|
nat = p;
|
||||||
if (!part1) { part1 = p.second; }
|
if (!part1) { part1 = p.second; }
|
||||||
} else if (dest < 50) {
|
|
||||||
Interact(ethernet, machines[dest], p);
|
|
||||||
} else {
|
} 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