OpcodeComputer now accepts Iterators and Iterables

master
Arne Wischer 6 years ago
parent 86572611c9
commit d78d1c6f98
  1. 9
      days/comp.py

@ -1,4 +1,4 @@
from typing import List, Dict, Tuple, Iterator
from typing import Dict, Tuple, Iterator, Iterable, Union
class OpcodeComputer():
@ -55,7 +55,7 @@ class OpcodeComputer():
pass
return (int(i[-2:]), a, b, c)
def process_op(self, inp: List[int] = [])\
def process_op(self, inp: Union[Iterator[int], Iterable[int]] = [])\
-> Iterator[int]:
"""Run program code
@ -75,6 +75,8 @@ class OpcodeComputer():
pc: int = 0
mem = self.memory
cmd: int = mem[pc]
input: Iterator[int] = iter(inp)
popcount = 0
while cmd != 99:
cmd, a, b, c = self.parse_param(pc, mem, base)
@ -85,7 +87,8 @@ class OpcodeComputer():
mem[c] = read(a) * read(b)
pc += 4
elif cmd == 3:
mem[a] = int(inp.pop(0))
mem[a] = int(next(input))
popcount += 1
pc += 2
elif cmd == 4:
yield read(a)

Loading…
Cancel
Save