diff options
Diffstat (limited to 'solutions/py')
| -rw-r--r-- | solutions/py/d09.py | 8 | ||||
| -rw-r--r-- | solutions/py/d13.py | 4 | ||||
| -rw-r--r-- | solutions/py/intcode.py | 31 |
3 files changed, 27 insertions, 16 deletions
diff --git a/solutions/py/d09.py b/solutions/py/d09.py index 7495d3a..ada6763 100644 --- a/solutions/py/d09.py +++ b/solutions/py/d09.py @@ -5,9 +5,11 @@ def do(input, code): c = intcode.Computer(program) c.input = code output = [] - while c.memory[c.pointer] != 99: + while True: c.step() #print(c.relative_base, c.pointer, c.memory) + if c.SIG_HALT: + break if c.output is not None: output.append(c.output) c.output = None @@ -20,10 +22,6 @@ def pt2(input): return do(input[0], 2) if __name__ == "__main__": - import cProfile - input = open("../input/09", "r").readlines() - cProfile.run("pt1(input)") - cProfile.run("pt2(input)") print(pt1(input)) print(pt2(input)) diff --git a/solutions/py/d13.py b/solutions/py/d13.py index 88b0fa2..3dbc5fb 100644 --- a/solutions/py/d13.py +++ b/solutions/py/d13.py @@ -44,7 +44,7 @@ def pt2(input): ball_x = paddle_x = 0 while c.memory[c.pointer] != 99: - if c.wants_input: + if c.SIG_INPUT: points, ball_x, paddle_x = draw(screen, points) if paddle_x < ball_x: c.input = 1 @@ -104,7 +104,7 @@ def visualize(input): ball_x = paddle_x = 0 while c.memory[c.pointer] != 99: - if c.wants_input: + if c.SIG_INPUT: time.sleep(0.01) frame_n += 1 frame, points, ball_x, paddle_x = draw(screen, points) diff --git a/solutions/py/intcode.py b/solutions/py/intcode.py index 9be6dad..e9ef90c 100644 --- a/solutions/py/intcode.py +++ b/solutions/py/intcode.py @@ -14,6 +14,8 @@ HAL = 99 class Computer(object): def __init__(self, program): self.memory = program.copy() + self.memory_size = len(self.memory) + self.extra_memory = {} self.instruction_cache = {} self.pointer = 0 self.phase_read = False # for day 7 @@ -21,10 +23,12 @@ class Computer(object): self.input = None self.output = None - self.wants_input = False + + self.SIG_INPUT = False + self.SIG_OUTPUT = False + self.SIG_HALT = False def parse_op(self, op): - #TODO if op in self.instruction_cache: return self.instruction_cache[op] code = op % 100 @@ -38,13 +42,14 @@ class Computer(object): self.output = None def write(self, addr, val): - while addr >= len(self.memory): - self.memory.append(0) - self.memory[addr] = val + if addr >= self.memory_size: + self.extra_memory[addr] = val + else: + self.memory[addr] = val def get(self, addr): - while addr >= len(self.memory): - self.memory.append(0) + if addr >= self.memory_size: + return self.extra_memory.get(addr, 0) return self.memory[addr] def get_param(self, inst, num): @@ -64,8 +69,12 @@ class Computer(object): self.write(self.relative_base + self.get(self.pointer + num), val) def step(self): + if self.SIG_OUTPUT and self.output is None: + self.SIG_OUTPUT = False + inst = self.parse_op(self.memory[self.pointer]) if inst[0] == HAL: + self.SIG_HALT = True return elif inst[0] == ADD: self.write_param(inst, 3, \ @@ -77,14 +86,18 @@ class Computer(object): self.pointer += 4 elif inst[0] == IN: if self.input is None: - self.wants_input = True + self.SIG_INPUT = True return self.write_param(inst, 1, self.input) - self.wants_input = False self.input = None + self.SIG_INPUT = False self.pointer += 2 elif inst[0] == OUT: + if self.output is not None: + self.SIG_OUTPUT = True + return self.output = self.get_param(inst, 1) + self.SIG_OUTPUT = False self.pointer += 2 elif inst[0] == JNZ: if self.get_param(inst, 1) != 0: |
