|
|
|
|
@ -1,7 +1,23 @@ |
|
|
|
|
from typing import * |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpcodeComputer(): |
|
|
|
|
def parse_param(self, pc, mem): |
|
|
|
|
i = str(mem[pc]) |
|
|
|
|
a, b, c = (0, 0, 0) |
|
|
|
|
|
|
|
|
|
def parse_param(self, pc: int, mem: List[int]) \ |
|
|
|
|
-> Tuple[int, int, int, int]: |
|
|
|
|
"""Parse Intcode and return parameters as specified by parameter mode |
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
pc (int): current instruction pointer |
|
|
|
|
mem (List[int]): program code |
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
Tuple[int, int, int, int]: current opcode, parameter a, b and c |
|
|
|
|
""" |
|
|
|
|
i: str = str(mem[pc]) |
|
|
|
|
a: int = 0 |
|
|
|
|
b: int = 0 |
|
|
|
|
c: int = 0 |
|
|
|
|
try: |
|
|
|
|
a = mem[pc+1] if i[-3:-4:-1] == '1' else mem[mem[pc+1]] |
|
|
|
|
b = mem[pc+2] if i[-4:-5:-1] == '1' else mem[mem[pc+2]] |
|
|
|
|
@ -10,9 +26,17 @@ class OpcodeComputer(): |
|
|
|
|
pass |
|
|
|
|
return (int(i[-2:]), a, b, c) |
|
|
|
|
|
|
|
|
|
def process_op(self, mem): |
|
|
|
|
pc = 0 |
|
|
|
|
cmd = mem[pc] |
|
|
|
|
def process_op(self, mem: List[int]) -> int: |
|
|
|
|
"""Run program code |
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
mem (List[int]): Memory to run OpcodeComputer on |
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
int: Content in memory at position 0 |
|
|
|
|
""" |
|
|
|
|
pc: int = 0 |
|
|
|
|
cmd: int = mem[pc] |
|
|
|
|
while cmd != 99: |
|
|
|
|
cmd, a, b, c = self.parse_param(pc, mem) |
|
|
|
|
if cmd == 1: |
|
|
|
|
@ -39,3 +63,11 @@ class OpcodeComputer(): |
|
|
|
|
pc += 4 |
|
|
|
|
cmd = mem[pc] |
|
|
|
|
return mem[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def input() -> str: |
|
|
|
|
return input() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print(text: str): |
|
|
|
|
print(text) |
|
|
|
|
|