diff --git a/2019/day21-p1.springscript b/2019/day21-p1.springscript new file mode 100644 index 0000000..94a869b --- /dev/null +++ b/2019/day21-p1.springscript @@ -0,0 +1,6 @@ +OR D J +NOT C T +AND T J +NOT A T +OR T J +WALK diff --git a/2019/day21.py b/2019/day21.py new file mode 100644 index 0000000..7ff6692 --- /dev/null +++ b/2019/day21.py @@ -0,0 +1,20 @@ +import sys + +from intcode import AIC + + +def main(): + codes = AIC.parse_file("day21.txt") + ss = SSIC(codes, script=sys.stdin.read()) + ss.run() + + +class SSIC(AIC): + + def __init__(self, *args, script="", **kwargs): + super().__init__(*args, **kwargs) + self.input_text = script + + +if __name__ == "__main__": + main() diff --git a/2019/intcode.py b/2019/intcode.py index 445d3ba..83597ec 100644 --- a/2019/intcode.py +++ b/2019/intcode.py @@ -162,3 +162,25 @@ class Intcode(object): def output_data(self, data): print(">", data) + + +class AIC(Intcode): + """ASCII-enabled Intcode interpreter.""" + LN = ord("\n") + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.input_text = "" + + def input_data(self): + data, self.input_text = self.input_text[0], self.input_text[1:] + return ord(data) + + def output_data(self, data): + if data > 256: + self.handle_int_output(data) + else: + print(chr(data), end="") + + def handle_int_output(self, i): + print(f"Int output: {i}.")