From 186070aedc2fc45ac8d6cb5762c0de9af1855ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 13 Dec 2019 19:25:32 +0100 Subject: Minor speed-ups --- solutions/py/d13.py | 7 ++----- solutions/py/intcode.py | 17 +++++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/solutions/py/d13.py b/solutions/py/d13.py index 78fc423..88b0fa2 100644 --- a/solutions/py/d13.py +++ b/solutions/py/d13.py @@ -105,7 +105,7 @@ def visualize(input): while c.memory[c.pointer] != 99: if c.wants_input: - time.sleep(0.005) + time.sleep(0.01) frame_n += 1 frame, points, ball_x, paddle_x = draw(screen, points) print(frame) @@ -127,9 +127,6 @@ if __name__ == "__main__": import cProfile _input = open("../input/13", "r").readlines() - cProfile.run("pt1(_input)") - cProfile.run("pt2(_input)") print(pt1(_input)) print(pt2(_input)) - if input("Visualize? [Y/n]: ") == "y": - visualize(_input) + #visualize(_input) diff --git a/solutions/py/intcode.py b/solutions/py/intcode.py index 972250e..9be6dad 100644 --- a/solutions/py/intcode.py +++ b/solutions/py/intcode.py @@ -13,13 +13,11 @@ HAL = 99 class Computer(object): def __init__(self, program): - self.memory = {} - for i in range(len(program)): - self.memory[i] = program[i] + self.memory = program.copy() + self.instruction_cache = {} self.pointer = 0 self.phase_read = False # for day 7 self.relative_base = 0 - self.extra_mem = {} self.input = None self.output = None @@ -27,9 +25,12 @@ class Computer(object): def parse_op(self, op): #TODO + if op in self.instruction_cache: + return self.instruction_cache[op] code = op % 100 ops = str(op).zfill(param_amount[code]+2) - return [code] + [int(x) for x in ops[:-2][::-1]] + 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): @@ -37,10 +38,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 def get(self, addr): - return self.memory.get(addr, 0) + while addr >= len(self.memory): + self.memory.append(0) + return self.memory[addr] def get_param(self, inst, num): if inst[num] == 0: -- cgit v1.2.1