aocpp/day23.cpp

90 lines
2.1 KiB
C++
Raw Normal View History

2022-11-05 11:19:32 -07:00
#include <cstddef>
#include <deque>
#include <iostream>
#include <stdexcept>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
2022-11-06 10:07:31 -08:00
#include <fstream>
2022-11-05 11:19:32 -07:00
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {
2022-11-06 10:53:26 -08:00
struct Payload { ValueType x, y; };
struct Packet { ValueType destination; Payload payload; };
using Ethernet = std::deque<Packet>;
2022-11-05 11:19:32 -07:00
auto BuildNetwork(Machine m) -> std::vector<Machine> {
std::vector<Machine> machines;
2022-11-06 10:53:26 -08:00
auto& i = std::get<Input>(Step(m)).pos;
2022-11-05 21:48:52 -07:00
for (i = 0; i < 50; i++) {
2022-11-05 11:19:32 -07:00
machines.push_back(m);
}
return machines;
}
2022-11-06 10:53:26 -08:00
auto Interact(Ethernet & ethernet, Machine & m, std::optional<Payload> p) -> void {
for(;;) {
auto e = Step(m);
switch (e.index()) {
default: throw std::runtime_error{"unexpected halt"};
case 0: {
auto& i = std::get<0>(e).pos;
if (p) {
i = p->x;
StepInput(m, p->y);
p = {};
break;
}
i = -1; // no packet
return;
}
case 1: {
auto d = std::get<1>(e).val;
auto x = StepOutput(m);
auto y = StepOutput(m);
ethernet.push_back({d, {x,y}});
}
2022-11-05 15:06:54 -07:00
}
2022-11-06 10:53:26 -08:00
}
2022-11-05 11:19:32 -07:00
}
} // namespace
2022-11-05 15:06:54 -07:00
auto main() -> int {
2022-11-06 10:11:33 -08:00
std::ifstream fin { "/Users/emertens/Source/advent/inputs/2019/23.txt" };
auto machines = BuildNetwork(Machine{ParseStream(fin)});
2022-11-05 11:19:32 -07:00
auto ethernet = Ethernet{};
std::optional<ValueType> part1;
std::optional<ValueType> part2;
2022-11-06 10:53:26 -08:00
std::optional<Payload> nat;
2022-11-05 11:19:32 -07:00
for(;;) {
2022-11-05 21:48:52 -07:00
if (!ethernet.empty()) {
2022-11-06 10:53:26 -08:00
auto packet = ethernet.front();
2022-11-05 11:19:32 -07:00
ethernet.pop_front();
2022-11-06 10:53:26 -08:00
if (packet.destination == 255) {
nat = packet.payload;
if (!part1) { part1 = packet.payload.y; }
2022-11-05 11:19:32 -07:00
} else {
2022-11-06 10:53:26 -08:00
Interact(ethernet, machines.at(packet.destination), packet.payload);
2022-11-05 21:48:52 -07:00
}
} else if (nat) {
2022-11-06 10:53:26 -08:00
if (part2 == nat->y) { break; }
part2 = nat->y;
2022-11-05 21:48:52 -07:00
Interact(ethernet, machines[0], nat);
} else {
2022-11-06 10:53:26 -08:00
for (auto & m : machines) {
2022-11-05 21:48:52 -07:00
Interact(ethernet, m, {});
2022-11-05 11:19:32 -07:00
}
}
}
std::cout << "Part 1: " << *part1 << std::endl;
std::cout << "Part 2: " << *part2 << std::endl;
}