def amplifier(program, settings): i = 0 for setting in settings: amp = Intcode(program.copy()) outputs = amp.run(data=[i, setting]) i = outputs return outputs
def search(program): stack = [('', program)] grid = dict() oxygen = None while stack: sequence, p = stack.pop(0) c = coordinate(sequence) #draw(grid, X=c) # Update map grid[c] = '.' # Run sequence for d in 'NSWE': ds = sequence + d dc = coordinate(ds) if dc in grid: continue vm = Intcode(p) vm.write(CMD[d]) status = vm.run() if status == 0: grid[dc] = '#' elif status == 1: # Take new step stack.append((ds, vm.program)) elif status == 2: oxygen = ds stack.append((ds, vm.program)) else: print("bad status:", status) return grid, oxygen
def robot(): vm = Intcode(load(11)) grid = dict() di = 0 p = (0, 0) grid[p] = 1 while not vm.is_terminated(): camera = grid.get(p, 0) vm.write(camera) color = vm.run() turn_right = vm.run() if color is not None: grid[p] = color di = (di + (1 if turn_right == 1 else -1)) % len(DIRECTIONS) dx, dy = DIRECTIONS[di] x, y = p p = x + dx, y + dy print(len(grid)) render(grid)
# Jump if any space (unless !A AND B AND !C) and double landing spaces ok # !(A AND AND B C OR) AND D AND (H OR E) #ABCDEFGHI #.#.##.#.#### program = """ NOT T T AND A T AND B T AND C T NOT T J AND D J NOT A T AND A T OR H T OR E T AND T J RUN """ vm.write_ascii(program.lstrip()) while not vm.is_terminated(): c = vm.run() if c is not None: if c > 255: print(c) break print(str(chr(c)), end='')