Example #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
Example #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
Example #3
0
from common import *
from intcode import IntcodeVM

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

vm = IntcodeVM(prog)
vm.runToBlock()

s = ''
for i in vm.output:
    s += chr(i)

print(s)
dg = s.strip().split('\n')
grid = defaultdict(lambda: False)
SCAFFOLD = set('#^><v')
HEIGHT = len(dg)
WIDTH = len(dg[0])

robotPos = None
robotDirection = None

DIRS = {'^': 'N', 'v': 'S', '>': 'W', '<': 'E'}

for y in range(HEIGHT):
    for x in range(WIDTH):
        if dg[y][x] in SCAFFOLD:
            grid[(x, y)] = True
        if dg[y][x] in '^v<>':
            robotPos = (x, y)
            robotDirection = DIRS[dg[y][x]]
Example #4
0
def readGrid(x, y):
    vm = IntcodeVM(prog)
    vm.addInput([x, y])
    vm.runToBlock()
    return vm.output[-1]
Example #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])