diff options
Diffstat (limited to '19/py')
| -rw-r--r-- | 19/py/d21.py | 36 | ||||
| -rw-r--r-- | 19/py/intcode.py | 10 |
2 files changed, 39 insertions, 7 deletions
diff --git a/19/py/d21.py b/19/py/d21.py new file mode 100644 index 0000000..2e39563 --- /dev/null +++ b/19/py/d21.py @@ -0,0 +1,36 @@ +import intcode + +f = open("../input/21", "r").readlines() + +c = intcode.Computer([int(x) for x in f[0].split(",")], ascii=True) +output = [] + +def ascii_draw(a): + s = "" + for c in a: + if c == 10: + s += "\n" + elif c < 128: + s += chr(c) + else: + print("[INVALID ASCII]", c) + return s + +while not c.SIG_HALT: + c.step() + if c.SIG_INPUT: + # flush output + print(ascii_draw(output)) + output = [] + if len(output) > 0: + output = [] + while True: + s = input() + if s.upper() == "END": + break + c.queue_ascii(s.upper()) + if c.SIG_OUTPUT: + output.append(c.output) + c.output = None +print(output) +print(ascii_draw(output)) diff --git a/19/py/intcode.py b/19/py/intcode.py index 123f4f3..8afa12f 100644 --- a/19/py/intcode.py +++ b/19/py/intcode.py @@ -13,10 +13,12 @@ BAS = 9 HAL = 99 class Computer(object): - def __init__(self, program): + def __init__(self, program, ascii=False): self.program = program self.memory_size = len(self.program) self.instruction_cache = {} + if ascii: + self.SIG_ASCII = True self.reset() @@ -32,7 +34,6 @@ class Computer(object): self.output = None self.SIG_INPUT = False - self.SIG_ASCII = False self.SIG_OUTPUT = False self.SIG_HALT = False @@ -43,11 +44,6 @@ class Computer(object): ops = str(op).zfill(param_amount[code]+2) self.instruction_cache[op] = [code] + [int(x) for x in ops[:-2][::-1]] return self.instruction_cache[op] - #return [code] + [(op // 10**(i+2)) % 10**(i+1) for i in range(param_amount[code])] - - def clear_flags(self): - self.input = None - self.output = None def write(self, addr, val): if addr >= self.memory_size: |
