program.restart() program.memory[0] = 2 def get_move(): if ball[0] < paddle[0]: return -1 if ball[0] > paddle[0]: return 1 return 0 while program.state != STATE_HALTED: move = get_move() program.run([move]) i = 0 while i < len(program.output): x, y, score = program.output[i:i + 3] i += 3 paddle = (paddle[0] + move, paddle[1]) ball = (x, y) return score if __name__ == '__main__': memory = read_comma_separated_list("game.txt", int) tiles, program = initialize_board(memory) counter = Counter(tiles.values()) print(counter[2]) print(play_game_using_ai(tiles, program))
def test_correct_diagnostic_code_after_input_2(): memory = read_comma_separated_list("int_program2.txt", int) program = IntProgram(memory, always_move_pointer=False) program.run([5]) assert program.output[-1] == 15724522
def parse_doors(output): return re.findall("(?<=- )(?:north|south|east|west)", output) move_one_step_in_direction = { "east": lambda i, j: (i, j+1), "west": lambda i, j: (i, j-1), "north": lambda i, j: (i-1, j), "south": lambda i, j: (i+1, j) } if __name__ == '__main__': memory = read_comma_separated_list("cryostasis.txt", int) print(len(memory)) program = AsciiProgram(memory) ship_map = defaultdict(lambda: ".") pos = (0, 0) ship_map[pos] = "x" inp = None while 1: program.run(inp) out = program.parsed_output if inp in move_one_step_in_direction and "You can't go that way" not in out: ship_map[pos] = "x" pos = move_one_step_in_direction[inp](*pos)
def test_find_max_thruster_output3(settings_range_min, settings_range_max, expected_output): memory = read_comma_separated_list("thrusters.txt", int) assert find_max_thruster_output(memory, settings_range_min, settings_range_max) == expected_output
def test_correct_diagnostic_code_after_input(): memory = read_comma_separated_list("int_program2.txt", int) program = IntProgram(memory, always_move_pointer=True) program.run([1]) assert not any(program.output[:-1]) # All 0 assert program.output[-1] == 7286649
def equals(self: IntProgram): _run_3_param_op(self, lambda x, y: int(x == y)) @register_operation(9) def adjust_relative_base(self: IntProgram): self.relative_base += self.get_param(0) self.pointer += 2 @register_operation(99) def halt(self: IntProgram): self.state = STATE_HALTED def _run_3_param_op(self: IntProgram, op): self.move_pointer( self.set_param(2, op(self.get_param(0), self.get_param(1))), 4) def _jump(self: IntProgram, jump_if): self.pointer = self.get_param(1) if bool( self.get_param(0)) == jump_if else self.pointer + 3 if __name__ == '__main__': memory = read_comma_separated_list("int_program3.txt", int) program = IntProgram(memory) program.run([2]) print(program.output)
("U", 0): "L", ("U", 1) : "R", ("D", 0): "R", ("D", 1): "L", ("R", 0): "U", ("R", 1): "D", ("L", 0): "D", ("L", 1): "U", } move_one_step_in_direction = { "R": lambda i, j: (i, j+1), "L": lambda i, j: (i, j-1), "U": lambda i, j: (i-1, j), "D": lambda i, j: (i+1, j) } if __name__ == '__main__': memory = read_comma_separated_list("painting.txt", int) program = IntProgram(memory) i, j = 0, 0 direction = "U" painting = defaultdict(int) painting[(i, j)] = 1 while program.state != STATE_HALTED: program.run([painting[(i, j)]]) painting[(i, j)] = program.output[0] move_left = program.output[1] direction = new_direction[(direction, program.output[1])] i, j = move_one_step_in_direction[direction](i, j)
sub_paths = [] for i in range(1, 4): sub_paths.append(match.group(i)[:-1]) for p, c in zip(sub_paths, ("A", "B", "C")): path_str = path_str.replace(p, c) return path_str, sub_paths def path_to_command(path): return list(map(ord, path)) + [10] if __name__ == '__main__': memory = read_comma_separated_list("rescue.txt", int) program = IntProgram(memory) program.run() scaffold_map = get_scaffold_map(memory) scaffold_map_lines = list(clean_lines_iter(scaffold_map)) print(get_sum_of_intersection_coord_products(scaffold_map_lines)) path = get_robot_path(scaffold_map_lines) main_path, sub_paths = get_sub_paths(path) memory[0] = 2 program = IntProgram(memory) program.run(path_to_command(main_path))
x = clean_lines_iter(x) x = list(map(lambda row: list(map(lambda c: int(c != "."), row)), x)) for i, row in enumerate(x): start_x = row.index(1) try: width = row[start_x:].index(0) except ValueError: width = 50 - start_x if width >= 10 and i >= 9 and x[i - 9][start_x + 9] == 1: break print(start_x * 10000 + i - 9) memory = read_comma_separated_list("tractor_beam.txt", int) picture = defaultdict(int) def get_val_at_xy(x, y): if (y, x) not in picture: program = IntProgram(memory) program.run([x, y]) picture[(y, x)] = program.output[0] return picture[(y, x)] for y in range(50): for x in range(50): picture[(y, x)] = get_val_at_xy(x, y) print(sum(picture.values())) plot_arr(picture)
def test_game(): memory = read_comma_separated_list("game.txt", int) tiles, program = initialize_board(memory) counter = Counter(tiles.values()) assert counter[2] == 298 assert play_game_using_ai(tiles, program) == 13956
for amplifier in amplifiers: amplifier.run([prev_out]) prev_out = amplifier.output[-1] return amplifiers[-1].output[-1] def find_max_thruster_output(program, range_min, range_max): max_out = -1 for setting in itertools.permutations(list(range(range_min, range_max))): out = compute_thruster_output(program, setting) if out > max_out: max_out = out return max_out def _list_to_gen(_list): return (i for i in _list) if __name__ == '__main__': program = read_comma_separated_list("thrusters.txt", int) print(find_max_thruster_output(program, 0, 5)) print(find_max_thruster_output(program, 5, 10))
NOT C T AND D T OR T J WALK""" commands2 = """NOT C T AND D T OR E J OR H J AND T J NOT A T OR T J OR B T OR E T NOT T T OR T J RUN""" if __name__ == '__main__': memory = read_comma_separated_list("springdroid.txt", int) program = IntProgram(memory) program.run() print_out(program.output) commands_arr = [] for line in commands2.splitlines(): commands_arr.extend(str_to_command(line)) program.run(commands_arr) print_out(program.output)
if i in all_messages and len(all_messages[i]) > 0: message = all_messages[i].pop(0) else: message = [-1] computer.run(message) return prev_nat_message, False def network_is_idle(all_messages): for key, val in all_messages.items(): if key == 255: continue if val: return False return True if __name__ == '__main__': memory = read_comma_separated_list("network.txt", int) network = create_network(50, memory) all_messages = defaultdict(list) prev_nat_message = None while 1: prev_nat_message, was_duplicate = run_iteration( network, all_messages, prev_nat_message) if was_duplicate: break parse_output_from_all_computers(network, all_messages) print(prev_nat_message)
return world, shortest_path def get_longest_path(world, start_pos): queue = [(start_pos, 0)] visited = set() max_depth = -1 while queue: pos, depth = queue.pop(0) if depth > max_depth: max_depth = depth visited.add(pos) for move in move_one_step_in_direction: new_pos = move_one_step_in_direction[move](*pos) if (world[new_pos] in (EMPTY, TARGET)) and (new_pos not in visited): queue.append((new_pos, depth + 1)) return max_depth if __name__ == '__main__': memory = read_comma_separated_list("oxygen_system.txt", int) program = IntProgram(memory) world, shortest_path = create_map_of_area(memory, False) start_pos = go_to(0, 0, shortest_path) plot_arr(world) print(len(shortest_path)) print(get_longest_path(world, start_pos))