import re from tracemalloc import start import numpy as np f = open('input.txt', 'r') content = f.read() lines = content.splitlines() my_grid = np.array(lines) print(my_grid[2:5]) universe_list = [] locations = [] for line_count, line in enumerate(lines): # finds = line.find_all("#") # for find in finds: # locations.append([line_count, find]) universe_list.append(list(line)) universe = np.array(universe_list) print(np.isin(universe[2:5, 3:30], '#').any()) print(universe[2:5, 3:30]) locations = np.where(universe=='#') original_locations = np.copy(locations) print(locations) #chech columns for i in range(len(universe[0])): if not np.isin(universe[:, i], '#').any(): print(np.where(original_locations[1]>i)[0]) locations[1][[np.where(original_locations[1]>i)[0]]] += (1000000-1) #chech rows for i in range(len(universe)): if not np.isin(universe[i], '#').any(): print(np.where(original_locations[0]>i)[0]) locations[0][[np.where(original_locations[0]>i)[0]]] += (1000000-1) # print(locations) print(locations) sum = 0 for count_i in range(len(locations[0])-1): for count_j in range(count_i+1, len(locations[0])): temp_sum = abs(locations[0][count_j] - locations[0][count_i]) temp_sum += abs(locations[1][count_j] - locations[1][count_i]) sum += temp_sum print(f"galaxy {count_i} to {count_j}", temp_sum) print(sum) f.close()