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()