From ac21d58606c2c4d7e4e319bb9487b3b7307f104f Mon Sep 17 00:00:00 2001 From: Dece Date: Mon, 23 Dec 2019 13:07:43 +0100 Subject: [PATCH] Day 23 part 2 --- 2019/day23.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/2019/day23.py b/2019/day23.py index 1a4a3d6..08825e7 100644 --- a/2019/day23.py +++ b/2019/day23.py @@ -1,5 +1,6 @@ from queue import Empty, SimpleQueue from threading import Thread +import time from intcode import Intcode @@ -7,19 +8,41 @@ from intcode import Intcode def main(): codes = Intcode.parse_file("day23.txt") network = Network(50, codes) + + # Part 1 for nic in network.nics: thread = Thread(target=lambda: nic.run(), name=f"NIC {nic.address}") thread.start() + + # Part 2 + last_nat_y = None + while True: + time.sleep(0.1) + for nic in network.nics: + if not nic.inq.empty: + break + else: + if not network.last_nat_packet: + continue + x, y = network.last_nat_packet + if y == last_nat_y: + exit(f"Sending another {y} from NAT.") + last_nat_y = y + print(f"Network is idle, sending NAT packet to 0.") + network.nics[0].inq.put(x) + network.nics[0].inq.put(y) class Network: def __init__(self, size, codes): self.nics = [NIC(address, self, codes) for address in range(size)] + self.last_nat_packet = None def route(self, sender, dest, x, y): if dest == 255: - print(f"Packet to 255: {x, y}.") + print(f"NAT: {x, y}.") + self.last_nat_packet = (x, y) return print(f"Send {x, y} from {sender} to {dest}") self.nics[dest].inq.put(x)