Пример #1
0
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)
Пример #2
0
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

    data = input() if args.inputs is None else args.inputs.pop(0)
    if log:
        log.write(data + "\n")
    if args.raw:
        vm.input(int(data))