def part1(inputs=None): """Output the answer to part 1 - """ cpu = IntCodeProcessor(path='day13input.txt') output = cpu.execute_program() block_count = sum( [1 for instruction in output[2::3] if instruction == BLOCK]) print(f'Part 1 answer: {block_count}')
def part1(): """Output the answer to part 1 - """ cpu = IntCodeProcessor(path='day11input.txt') heading = (0, 1) path = [(0, 0)] paint = {} result = None next_input = None while result is None: try: result = cpu.execute_program(next_input, reset=False) except ExecutionError as err: assert err.reason == ExecutionCode.NEED_INPUT pass output_count = len(cpu.outputs) assert output_count == 2 or output_count == 0 if output_count == 2: new_color = cpu.outputs.pop(0) turn = cpu.outputs.pop(0) heading = step_robot(new_color, turn, path, heading, paint) next_input = paint.get(path[-1], 0) print(f'Part 1 answer: {len(paint)}')
def part2(): """Output the answer to part 2 - What's the final score""" program = IntCodeProcessor.load_program('day13input.txt') program[0] = 2 cpu = IntCodeProcessor(program) result = None next_input = None ball_pos = None paddle_pos = None score = None while result is None: try: result = cpu.execute_program(next_input, reset=False) except ExecutionError as err: assert err.reason == ExecutionCode.NEED_INPUT ball_pos, paddle_pos, score = process_output(cpu.outputs, ball_pos, paddle_pos, score) cpu.outputs = [] next_input = next_input_for(ball_pos, paddle_pos) print(f'Part 2 answer: {score}')
def part2(inputs = None): """Output the answer to part 2 - """ cpu = IntCodeProcessor(path='day15input.txt') path = {(0, 0): True} grid = {(0, 0): OPEN} find_paths(0, 0, None, grid, path, cpu) newly_filled_locs = [loc for loc, status in grid.items() if status == SUCCESS] steps = -1 # start at -1, because we go through the loop 1 extra time to determine that there are no more spaces to fill while len(newly_filled_locs) > 0: newly_filled_locs = fill_adjacent(newly_filled_locs, grid) steps += 1 print(f'Part 2 answer: {steps}')
def part1(inputs = None): """Output the answer to part 1 - Minimum number of movement commands to move to the oxygen system""" cpu = IntCodeProcessor(path='day15input.txt') path = {(0, 0): True} grid = {(0, 0): OPEN} successful_paths = find_paths(0,0,None, grid, path, cpu) # remove the "unmarked" locations from the paths marked_paths = [[loc for loc, marked in path.items() if marked] for path in successful_paths] shortest_path = min(marked_paths, key=len) # subtract 1 because we want the number of movement commands, so we need to # subtract out the initial location num_movement_commands = len(shortest_path) - 1 print(f'Part 1 answer: {num_movement_commands}')
def generate_thruster_signal_part2(program, phase_settings): cpus = [IntCodeProcessor(program) for _ in range(5)] for index, phase in enumerate(phase_settings): try: logging.debug(f'CPU {index} - initialize') cpus[index].execute_program([phase]) except ExecutionError: pass cpu_index = 0 next_input = [0] while True: try: logging.debug(f'CPU {cpu_index} - start') if cpus[cpu_index].execute_program(next_input, False) is not None and cpu_index == 4: break except ExecutionError: pass next_input = cpus[cpu_index].outputs cpus[cpu_index].outputs = [] cpu_index = (cpu_index+1) % 5 return cpus[-1].outputs
from intcode import IntCodeProcessor cpu = IntCodeProcessor(path='day05input.txt') print('Part 1 answer: ', cpu.execute_program(1)[-1]) print('Part 2 answer: ', cpu.execute_program(5)[0])
return (point[1], -point[0]) if right else (-point[1], point[0]) def step_robot(paint_command, move_command, path, heading, paint): location = path[-1] paint[location] = paint_command new_heading = rotate(heading, move_command) path.append((location[0] + new_heading[0], location[1] + new_heading[1])) return new_heading if __name__ == '__main__': #part1() part2() cpu = IntCodeProcessor(path='day11input.txt') heading = (0, 1) path = [(0, 0)] paint = {} result = None next_input = 1 while result is None: try: result = cpu.execute_program(next_input, reset=False) except ExecutionError as err: assert err.reason == ExecutionCode.NEED_INPUT pass output_count = len(cpu.outputs) assert output_count == 2 or output_count == 0
def generate_thruster_signal(program, phase_settings): cpu = IntCodeProcessor(program) signal = 0 for phase in phase_settings: signal = cpu.execute_program([phase, signal])[0] return signal
from itertools import product from intcode import IntCodeProcessor def part1(): """Output the answer for part 1""" print(f'Part 1 answer: {cpu.execute_program_with_inputs(12, 2)}') def find_inputs_in_range_for_output(range_end, output): """ Returns the first aggregated noun and verb (100 * noun + verb) required to produce the given output for nouns and verbs in the range 0 to range_end (not including range_end)""" for noun, verb in product(range(range_end), range(range_end)): result = cpu.execute_program_with_inputs(noun, verb) if result == output: return noun * 100 + verb return None def part2(): """Output the answer for part 1""" print(f'Part 2 answer: {find_inputs_in_range_for_output(100, 19690720)}') cpu = IntCodeProcessor(path = 'day02input.txt') part1() # Expected answer: 662703 part2() # Expected answer: 4019