return f"<{x}>" parser = argparse.ArgumentParser() parser.add_argument("-r", "--raw", help="do not use ascii I/O", action="store_true") parser.add_argument("-i", "--inputs", help="provide input in advance", nargs="+") parser.add_argument("-l", "--log", help="log the session") parser.add_argument( "-t", "--tape", help="tape overrides", nargs="+", default=[], type=parse_overrides ) parser.add_argument("program", help="program to run") args = parser.parse_args() vm = Vm(open(args.program).read()) for pos, val in args.tape: vm.set_tape(pos, val) log = open(args.log, "w") if args.log else None while True: halted = vm.run() output = vm.drain_output() if args.raw: # In raw mode we might get no output, no point printing nothing. if output: print(" ".join(map(str, output))) else: print("".join(map(try_chr, output)), end="") if halted: break
from intcode import Vm from aoc import * program = data(13).read() print(Vm(program).complete()[2::3].count(2)) game = Vm(program) game.set_tape(0, 2) score = 0 last_x = {} game.run() while game.has_output(): for x, y, k in chunk(game.drain_output(), 3): if x == -1 and y == 0: score = k else: last_x[k] = x game.input((last_x[3] < last_x[4]) - (last_x[3] > last_x[4])) game.run() print(score)
def simulate(n, v): computer = Vm(program) computer.set_tape(1, n) computer.set_tape(2, v) computer.complete() return computer.get_tape(0)