Пример #1
0
def runScript(scr):
    vm = IntcodeVM(prog)
    inp = []
    for line in scr.splitlines():
        inp += list(map(ord, line))
        inp.append(10)

    vm.addInput(inp)
    vm.runToBlock()

    s = ''
    ans = 0
    for i in vm.output:
        try:
            s += chr(i)
        except ValueError:
            ans = i
    return ans, s
Пример #2
0
def run(initialBlock):
    vm = IntcodeVM(data)
    grid = defaultdict(lambda: BLACK)
    grid[(0, 0)] = initialBlock

    direction = 'N'
    x = 0
    y = 0
    halted = False
    while not halted:
        vm.addInput([grid[(x, y)]])
        halted = vm.runToBlock()
        grid[(x, y)] = vm.output[-2]
        direction = TURNS[direction][vm.output[-1]]
        dx, dy = DIRECTIONS[direction]
        x += dx
        y += dy

    return grid
Пример #3
0
def readGrid(x, y):
    vm = IntcodeVM(prog)
    vm.addInput([x, y])
    vm.runToBlock()
    return vm.output[-1]
Пример #4
0
from common import *

from intcode import IntcodeVM

prog = filemap(int, "day9.txt", ',')

vm = IntcodeVM(prog)
vm.addInput([1])
vm.runToBlock()
print(vm.output)

vm = IntcodeVM(prog)
vm.addInput([2])
vm.runToBlock()
print(vm.output)

Пример #5
0
from common import *
from intcode import IntcodeVM
from itertools import permutations

prog = filemap(int, "day7.txt", ',')

m = 0
for perm in permutations(range(5)):
    out = 0
    for i in perm:
        vm = IntcodeVM(prog)
        vm.addInput([i, out])
        vm.runToBlock()
        out = vm.output[0]
    m = max(out, m)

print(m)

m = 0
for perm in permutations(range(5, 10)):
    vms = []
    for i in perm:
        vm = IntcodeVM(prog)
        vm.addInput([i])
        vms.append(vm)

    amp = 0
    out = 0
    while True:
        vm = vms[amp]
        vm.addInput([out])
Пример #6
0
from common import *
from intcode import IntcodeVM

prog = filemap(int, "day5.txt", ',')

vm = IntcodeVM(prog)
vm.addInput([1])
vm.runToBlock()
print(vm.output)

vm = IntcodeVM(prog)
vm.addInput([5])
vm.runToBlock()
print(vm.output)
Пример #7
0
from common import *
from intcode import IntcodeVM

prog = filemap(int, "day25.txt", ',')

vm = IntcodeVM(prog)

vm.runToBlock()
while True:
    s = ''
    for i in vm.output:
        s += chr(i)
    print(s)
    vm.clearOutput()

    inp = input('> ')
    for i in inp:
        vm.addInput([ord(i)])
    vm.addInput([10])
    vm.runToBlock()

# ans: 4206594
# astrolabe, ornament, food ration, weather machine