summaryrefslogtreecommitdiffstats
path: root/solutions
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2019-12-13 19:25:32 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2019-12-13 19:25:32 +0100
commit186070aedc2fc45ac8d6cb5762c0de9af1855ba1 (patch)
treec1b6ea8f158aec99395a54e4882385f967da8939 /solutions
parentbb432b66d00deb7d4a259389fe45fba7bac202cc (diff)
downloadaoc-186070aedc2fc45ac8d6cb5762c0de9af1855ba1.tar.gz
Minor speed-ups
Diffstat (limited to 'solutions')
-rw-r--r--solutions/py/d13.py7
-rw-r--r--solutions/py/intcode.py17
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: