def run(size, intcode): NAT = None nodes = [Runner(Buffer([i], -1)) for i in range(size)] network = [node.iterator(intcode) for node in nodes] output_buffers = [[] for _ in range(size)] for index in cycle(range(size)): iterator = network[index] value = next(iterator) if value is not None: output_buffers[index].append(value) if len(output_buffers[index]) == 3: address, X, Y = output_buffers[index] output_buffers[index] = [] if address < size: nodes[address].input.list += [X, Y] nodes[address].input.idle = False else: NAT = (X, Y) yield address, X, Y, False if all(node.input.idle for node in nodes) and NAT is not None: address = 0 X, Y = NAT yield address, X, Y, True nodes[address].input.list += [X, Y] nodes[address].input.idle = False
def runSequence(sequence, intcode, endCondition, startSignal=0): runners = [Runner(iter([phase])) for phase in sequence] runnerIterators = [runner.output_iterator(intcode) for runner in runners] # pipe last signal to current runner's input and add its next output signal to signals def step(signals, index): runners[index].feed(signals[-1]) signal = next(runnerIterators[index], None) return signals + [signal], (index + 1) % len(runners) return reduceUntil(endCondition, unpack(step), ([startSignal], 0))[0]
def translate_to_instructions(compression): names = 'ABC' lines = [] # main routine lines.append([names[index] for index in compression[0]]) # subroutines subroutine_to_line = lambda pairs: [ str(item) for pair in pairs for item in pair ] for pairs in compression[1]: lines.append(subroutine_to_line(pairs)) # disable visual feed lines.append(['n']) return Runner.ASCII_input([','.join(line) for line in lines])
def run(code, intcode): runner = Runner(Runner.ASCII_input(code)) runner.run(intcode) return runner.output
def solve(input, intcode): runner = Runner(iter(input)) runner.run(intcode) return intcode, runner
def run(input, intcode): runner = Runner(iter(input)) runner.run(intcode) return runner.output[-1]
def play(AI, game, intcode): playthrough = execute(Runner(AI(game)), [2] + intcode[1:]) for x, y, tile in playthrough: if x == -1 and y == 0 and count_blocks(game) == 0: return tile game[(x, y)] = tile
def build_game(intcode): result = defaultdict(lambda: EMPTY) for x, y, tile in execute(Runner(), intcode): result[(x, y)] = tile return result
from utils import cmap, compose, invoker, at, unpack from functools import partial from itertools import product from aoc.intcode import Runner N = 100 TARGET = 19690720 parse = compose(list, cmap(int), invoker('split', ',')) runner = Runner() run = lambda intcode: runner.run(intcode) alarm = lambda noun, verb: lambda intcode: intcode[:1] + [noun, verb ] + intcode[3:] solve = lambda noun, verb: compose(at(0), run, alarm(noun, verb)) findNounVerb = lambda target, intcode: next( (noun, verb) for noun, verb in product(range(N), range(N)) if solve(noun, verb)(intcode) == target) one = compose(solve(12, 2), parse) two = compose(unpack(lambda a, b: a * 100 + b), partial(findNounVerb, TARGET), parse)
def draw(intcode): runner = Runner() runner.run(intcode) return runner.output
def get_dust(intcode): grid = get_grid(intcode) runner = Runner(get_instructions(grid)) runner.run([2] + intcode[1:]) return runner.output[-1]
def solve(intcode, grid): robot = Robot(Runner()) robot.paint_all(intcode, grid) return grid, robot