def run_robot(program, start_color=0): direction = 1 painted_panels = defaultdict(lambda: 0) robot = intcode_program(program, verbose=False) pos = [0, 0] painted_panels[tuple(pos)] = start_color while True: try: color, next_direction = robot.send(painted_panels[tuple(pos)]) painted_panels[tuple(pos)] = color if next_direction: direction += 1 else: direction -= 1 if direction < 0: direction = 3 elif direction > 3: direction = 0 if direction == 0: pos[0] -= 1 elif direction == 1: pos[1] += 1 elif direction == 2: pos[0] += 1 elif direction == 3: pos[1] -= 1 except StopIteration: print("Completed. Painted {} panels.".format( len(painted_panels.keys()))) break return painted_panels
def __init__(self, program): self.pos = 0, 0 self.program = intcode_program(program) self.stack = deque([self.pos]) self.target = None self.seen = dict() self.t = 0
def compute_feedback_thruster_input(intcodes, sequence): amps = [intcode_program(intcodes, [phase]) for phase in sequence] value = 0 done = False while not done: for amp in amps: try: value = amp.send(value)[-1] except StopIteration as err: value = err.value done = True return value
from itertools import permutations from intcode import intcode_program if __name__ == "__main__": with open("inputs/inputdaytwo.txt", "r") as f: opcode_org = list(map(int, (f.read().split(",")))) for noun, verb in permutations(range(0, 99), 2): opcode = opcode_org.copy() opcode[1] = noun opcode[2] = verb output = intcode_program(opcode) if output == 19690720: break assert noun == 22 and verb == 54
def compute_thruster_input(intcodes, sequence): value = 0 for phase in sequence: value = intcode_program(intcodes, inputs=[phase, value]) return value
from intcode import intcode_program if __name__ == "__main__": with open("inputs/inputdaynine.txt", "r") as f: intcode = list(map(int, f.read().strip().split(","))) assert intcode_program(intcode, [1]) == 2738720997 assert intcode_program(intcode, [2]) == 50894
from intcode import intcode_program if __name__ == "__main__": with open("inputs/inputdayfive.txt", "r") as f: program = list(map(int, f.read().strip().split(","))) assert intcode_program(program, [1]) == 16225258 assert intcode_program(program, [5]) == 2808771