def main(): memory = read() intcode = IntCodeComputer(memory) white_tiles = set() painted = set() curr = [0, 0] turn_to_paint = True face = 0 directions = ['U', 'R', 'D', 'L'] while not intcode.completed: intcode.run() while not intcode.output.empty(): value = intcode.output.get() if turn_to_paint: if value == 1: white_tiles.add(tuple(curr)) elif value == 0 and tuple(curr) in white_tiles: white_tiles.remove(tuple(curr)) painted.add(tuple(curr)) else: if value == 1: face = (face + 5) % 4 elif value == 0: face = (face + 3) % 4 update(curr, directions[face]) turn_to_paint = not turn_to_paint intcode.read(int(tuple(curr) in white_tiles)) print(len(painted)) # output
def main(): memory = read() perms = list(itertools.permutations(range(5, 10))) max_signal = -10e9 for perm in perms: amplifers = [] isFirst = True halted = False for phase in perm: amplifier = IntCodeComputer(memory) amplifier.read(phase) amplifers.append(amplifier) output = 0 while True: for amplifier in amplifers: amplifier.read(output) amplifier.run() if amplifier.output.empty(): halted = True break output = amplifier.output.get() if halted: break final_output = output max_signal = max(final_output, max_signal) print(max_signal) # output
def main(): memory = read() memory[0] = 2 intcode = IntCodeComputer(memory) ball_x = -1 paddle_x = -1 score = -1 while not intcode.completed: intcode.run() while not intcode.output.empty(): x = intcode.output.get() y = intcode.output.get() tile_id = intcode.output.get() if x == -1 and y == 0: score = tile_id if tile_id == 3: paddle_x = x elif tile_id == 4: ball_x = x if paddle_x < ball_x: ipt = 1 elif paddle_x > ball_x: ipt = -1 else: ipt = 0 intcode.read(ipt) print(score)
def main(): memory = read() perms = list(itertools.permutations(range(5))) max_signal = -10e9 for perm in perms: output = 0 for idx, phase in enumerate(perm): amplifier = IntCodeComputer(memory) amplifier.read(phase) amplifier.read(output) amplifier.run() output = amplifier.output.get() max_signal = max(output, max_signal) print(max_signal) # output
def main(): memory = read() intcode = IntCodeComputer(memory) white_tiles = set() curr = [0, 0] white_tiles.add(tuple(curr)) turn_to_paint = True face = 0 directions = ['U', 'R', 'D', 'L'] min_x = 10e9 min_y = 10e9 max_x = -10e9 max_y = -10e9 while not intcode.completed: intcode.run() while not intcode.output.empty(): value = intcode.output.get() if turn_to_paint: if value == 1: white_tiles.add(tuple(curr)) elif value == 0 and tuple(curr) in white_tiles: white_tiles.remove(tuple(curr)) min_x = min(min_x, curr[0]) min_y = min(min_y, curr[1]) max_x = max(max_x, curr[0]) max_y = max(max_y, curr[1]) else: if value == 1: face = (face + 5) % 4 elif value == 0: face = (face + 3) % 4 update(curr, directions[face]) turn_to_paint = not turn_to_paint intcode.read(int(tuple(curr) in white_tiles)) for j in range(max_y, min_y - 1, -1): for i in range(min_x, max_x + 1): draw((i, j) in white_tiles) print()