From 8b0cb8d9d706a99b2e01d8b34aeb8eb66be1ec4d Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Tue, 14 Mar 2023 12:44:43 +0100 Subject: [PATCH] Added pipes to intcode --- intcode.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/intcode.py b/intcode.py index e59299c..b5ad256 100644 --- a/intcode.py +++ b/intcode.py @@ -9,6 +9,9 @@ class WaitForInput(Exception): class Halted(Exception): pass +class PipeError(Exception): + pass + class defaultlist(list): """Default list class. Allows writing and reading out of bounds.""" def __init__(self, lst, val_factory): @@ -207,6 +210,21 @@ class Interpreter(object): pass return self.IO.flush() + def pipe_from(self, other): + class Output(Exception): + pass + def raise_output(o): + raise Output(o) + other.comp.out_f = raise_output + def get_input(): + try: + other.run() + raise PipeError + except Output as o: + return int(str(o)) + self.comp.in_f = get_input + self.IO.write = other.IO.write + def copy(self): memory = self.memory.copy()