def __init__(self): self.p = (0, 0) self.v = (0, 1) self.colors = defaultdict(int) self.input_buf = [] self.cpu = VM(code) self.cpu.output = None self.cpu.input = self.give_input
class Robot(): DIRS = [(0, 1), (-1, 0), (0, -1), (1, 0)] def __init__(self): self.p = (0, 0) self.v = (0, 1) self.colors = defaultdict(int) self.input_buf = [] self.cpu = VM(code) self.cpu.output = None self.cpu.input = self.give_input def recv_output(self, v): self.input_buf.append(v) def give_input(self): return self.colors[self.p] def step(self): self.cpu.run() color = self.cpu.out_val self.cpu.run() turn = self.cpu.out_val if color is not None: self.colors[self.p] = color dir_ix = self.DIRS.index(self.v) if turn == 0: self.v = self.DIRS[(dir_ix + 1) % 4] elif turn == 1: self.v = self.DIRS[(dir_ix - 1) % 4] v1, v2 = self.v x, y = self.p self.p = (x + v1, y + v2) if self.cpu.halted: raise Exception("DONE")
def run(code, settings): vms = [] inputs = [] outputs = [] for s in settings: vm = VM(code) i = Input([s]) o = Output() vm.input = i.get_input vm.output = o.give_output vm.run() vms.append(vm) inputs.append(i) outputs.append(o) inputs[0].vals = [0] while True: for ix in range(len(vms)): vms[ix].run() o = outputs[ix].val inputs[(ix + 1) % len(inputs)].vals = [o] if all([vm.halted for vm in vms]): break return outputs[-1].val
class Lister(): def __init__(self, vals): self.vals = vals def get(self): if not self.vals: return None c = self.vals[0] self.vals = self.vals[1:] return c code[0] = 2 vm = VM(code) vm.input = lambda: None vm.output = None """ R 4 L 12 L 8 R 4 (A) L 8 R 10 R 10 R 6 (C) R 4 L 12 L 8 R 4 (A) R 4 R 10 L 12 (B) R 4 L 12 L 8 R 4 (A) L 8 R 10 R 10 R 6 (C)
from collections import defaultdict from utils import * from intcode import VM inp = get_input(2019, 13) print("Start of input:") print(inp[:500]) screen = defaultdict(int) code = [int(c) for c in inp.split(",")] vm = VM(code) vm.memory[0] = 2 vm.input = lambda: None vm.output = None score = 0 class OneValue(): def __init__(self, v): self.v = v def get(self): c = self.v self.v = None return c while not vm.halted:
from utils import get_input from intcode import VM inp = get_input(2019, 9) code = [int(c) for c in inp.split(",")] vm = VM(code) vm.run()
from utils import * from intcode import VM inp = get_input(2019, 15) code = [int(c) for c in inp.split(",")] grid = {(0,0): 3} vm = VM(code) vm.output = None pos = (0,0) def up(p): x, y = p return (x, y-1) def down(p): x, y = p return (x, y+1) def left(p): x, y = p return (x-1, y) def right(p): x, y = p return (x+1, y) def reverse(m):