def get_amplifier_chain_result(program_text, phase_sequence, initial_input=0): """ >>> get_amplifier_chain_result(\ '3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0', [4, 3, 2, 1, 0]) 43210 >>> get_amplifier_chain_result(\ '3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99'\ ',0,0', [0, 1, 2, 3, 4]) 54321 >>> get_amplifier_chain_result(\ '3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33'\ ',31,31,1,32,31,31,4,31,99,0,0,0', [1, 0, 4, 3, 2]) 65210 """ keep_looping = True inputs = [[phase] for phase in phase_sequence] errors = [None for _ in phase_sequence] value = initial_input while keep_looping: keep_looping = False for index, (error, _input) in enumerate(zip(errors, inputs)): _input.append(value) try: _, (value, ) = get_program_result_and_output_extended( program_text, _input, error=error) errors[index] = None except InsufficientInputError as e: errors[index] = e value = e.output_stream[-1] keep_looping = True return value
def solve(self, _input, debug=False): """ >>> Challenge().default_solve() 880360 """ image = get_image_from_program_text(_input) movement_commands_text = get_movement_commands_text_from_image(image) routine_commands_text, chunks = \ find_routine_and_functions_for_movement_commands_new( movement_commands_text) try: _, output = get_program_result_and_output_extended( _input, list( map(ord, ("{}\n" "{}" "n\n".format( routine_commands_text, "".join(map("{}\n".format, chunks)), )))), substitutions={0: 2}) except InsufficientInputError as e: if debug: print("".join(map(chr, e.output_stream))) raise *message, result = output if debug: print("".join(map(chr, message))) return result
def solve(self, _input, debug=False): """ >>> Challenge().default_solve() 247 """ _, output_stream = get_program_result_and_output_extended(_input, []) game = fill_game(output_stream) return sum(1 for tile_id in game.values() if tile_id == TILE_BLOCK)
def solve(self, _input, debug=False): """ >>> Challenge().default_solve() 8928 """ _, output = get_program_result_and_output_extended(_input, []) image = parse_image(output) scaffolds, _, _ = get_scaffolds_start_position_and_direction(image) intersections = get_intersections(scaffolds) return get_intersections_calibration(intersections)
def run_spring_robot(script, program_text, running=False): input_stream = script.as_input_stream(running=running) _, output = get_program_result_and_output_extended(program_text, input_stream) if not output: raise Exception("No output") if output[-1] <= 255: print("".join(map(chr, output))) return None solution = output[-1] return solution
def play_game(program_text=None, interactive=False): if program_text is None: program_text = Challenge().input error = None input_stream = [] game = {} while True: try: _, output = get_program_result_and_output_extended( program_text, input_stream, substitutions={0: 2}, error=error) fill_game(output, game) # print("Finished") break except InsufficientInputError as e: error = e new_output = error.output_stream fill_game(new_output, game) tiles_positions = get_game_tile_positions(game) paddle_position, _ = tiles_positions[TILE_PADDLE] ball_position, _ = tiles_positions[TILE_BALL] suggestion = sign(ball_position - paddle_position) if interactive: print(show_game(game)) user_input = None while user_input is None \ or user_input.strip() not in ['-1', '0', '1', '']: try: options_with_suggestion = '/'.join( f'[{option}]' if option == suggestion else f'{option}' for option in [-1, 0, 1]) user_input = input( f"Enter (paddle={paddle_position}, " f"ball={ball_position}) {options_with_suggestion}:") except EOFError: print("Exiting") return None if user_input == '': paddle_input = suggestion else: paddle_input = int(user_input) else: paddle_input = suggestion input_stream.append(paddle_input) return game
def paint_panels(program_text, initial_paint=None): position = (0, 0) direction = DIRECTION_UP paint_map = defaultdict(lambda: 0) if initial_paint: paint_map.update(initial_paint) error = None input_stream = [] while True: paint = paint_map[position] input_stream.append(paint) # print( # f"In {position}, facing {direction}, paint is {paint}, input is " # f"{input_stream}, counter is " # f"{error.input_stream_counter if error else 0}") try: _, output = get_program_result_and_output_extended(program_text, input_stream, error=error) # print("Finished") break except InsufficientInputError as e: error = e new_output = error.output_stream # print(new_output) if len(new_output) != 2: raise Exception(f"Expected 2 new outputs, but got {new_output}") new_paint, command = new_output if new_paint not in [0, 1]: raise Exception( f"Expected paint to be 0 or 1, but was '{new_paint}'") paint_map[position] = new_paint if command == COMMAND_TURN_LEFT: rotation = ROTATION_CCW elif command == COMMAND_TURN_RIGHT: rotation = ROTATION_CW else: raise Exception(f"Unknown rotation command '{command}'") direction = rotate(direction, rotation) position = move(position, direction) # paint = paint_map[position] # print(f"In {position}, facing {direction}, paint is {paint}") return paint_map
def solve(self, _input, debug=False): """ >>> Challenge().default_solve() 2738720997 >>> get_program_result_and_output_extended(\ '109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99', [])[1] [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99] >>> get_program_result_and_output_extended(\ '1102,34915192,34915192,7,4,7,99,0', [])[1] [1219070632396864] >>> get_program_result_and_output_extended(\ '104,1125899906842624,99', [])[1] [1125899906842624] """ _, output_stream = get_program_result_and_output_extended(_input, [1]) *wrong_op_codes, boost_keycode = output_stream if wrong_op_codes: raise Exception("Some op codes were wrong") return boost_keycode
def run_interactive_program(program_text, endless=False): error = None input_stream = [] while True: try: _, output = get_program_result_and_output_extended(program_text, input_stream, error=error) if endless: raise Exception("Endless interactive program exited") yield True, output break except InsufficientInputError as e: error = e next_input_or_inputs = yield False, error.output_stream if isinstance(next_input_or_inputs, int): next_input = next_input_or_inputs next_inputs = [next_input] else: next_inputs = next_input_or_inputs input_stream.extend(next_inputs)
def get_image_from_program_text(program_text): _, output = get_program_result_and_output_extended(program_text, []) image = parse_image(output) return image
def get_scan_point(program_text, x, y): _, output = get_program_result_and_output_extended(program_text, [x, y]) point, = output return point