|
| 1 | +def main(): |
| 2 | + with open("day11_input.txt", "r") as input_file: |
| 3 | + space_image = input_file.readlines() |
| 4 | + |
| 5 | + # Specifies if the current block (row or column) was expanded |
| 6 | + expanded = False |
| 7 | + for y, line in enumerate(space_image): |
| 8 | + if expanded: |
| 9 | + expanded = False |
| 10 | + continue |
| 11 | + if '#' not in line: |
| 12 | + space_image.insert(y, line) |
| 13 | + expanded = True |
| 14 | + |
| 15 | + for x in range(len(space_image[0])): |
| 16 | + is_empty = True |
| 17 | + if expanded: |
| 18 | + expanded = False |
| 19 | + continue |
| 20 | + for y, line in enumerate(space_image): |
| 21 | + if line[x] == '#': |
| 22 | + is_empty = False |
| 23 | + break |
| 24 | + if is_empty: |
| 25 | + for y, line in enumerate(space_image): |
| 26 | + space_image[y] = line[:x] + '.' + line[x:] |
| 27 | + expanded = True |
| 28 | + |
| 29 | + galaxies = [] |
| 30 | + for y, line in enumerate(space_image): |
| 31 | + for x, char in enumerate(line): |
| 32 | + if char == '#': |
| 33 | + galaxies.append({"x": x, "y": y}) |
| 34 | + |
| 35 | + for row in space_image: |
| 36 | + print(row, end='') |
| 37 | + |
| 38 | + print("") |
| 39 | + print("Galaxies:") |
| 40 | + number_of_pairs = 0 |
| 41 | + sum_of_paths_lengths = 0 |
| 42 | + for idx, my_galaxy in enumerate(galaxies): |
| 43 | + print(idx, my_galaxy) |
| 44 | + for another_galaxy in galaxies[idx + 1:]: |
| 45 | + number_of_pairs += 1 |
| 46 | + sum_of_paths_lengths += compute_length_of_shortest_path_between(my_galaxy, another_galaxy) |
| 47 | + print(f'shortest path between galaxy {my_galaxy} and {another_galaxy}', |
| 48 | + compute_length_of_shortest_path_between(my_galaxy, another_galaxy)) |
| 49 | + print("galaxy:", my_galaxy) |
| 50 | + print("Another Galaxy:", another_galaxy) |
| 51 | + print("number of pairs:", number_of_pairs) |
| 52 | + print("sums of paths lengths:", sum_of_paths_lengths) |
| 53 | + |
| 54 | + |
| 55 | +def compute_length_of_shortest_path_between(galaxy_a, galaxy_b): |
| 56 | + steps = 0 |
| 57 | + current_pos = galaxy_a.copy() |
| 58 | + while current_pos != galaxy_b: |
| 59 | + x_difference = galaxy_b["x"] - current_pos["x"] |
| 60 | + x_difference_negative = x_difference < 0 |
| 61 | + y_difference = galaxy_b["y"] - current_pos["y"] |
| 62 | + y_difference_negative = y_difference < 0 |
| 63 | + |
| 64 | + if x_difference_negative: |
| 65 | + x_difference *= -1 |
| 66 | + if y_difference_negative: |
| 67 | + y_difference *= -1 |
| 68 | + |
| 69 | + if y_difference > x_difference: |
| 70 | + if y_difference_negative: |
| 71 | + current_pos["y"] -= 1 |
| 72 | + else: |
| 73 | + current_pos["y"] += 1 |
| 74 | + else: # If y_difference == x_difference it doesn't matter in which direction we move |
| 75 | + if x_difference_negative: |
| 76 | + current_pos["x"] -= 1 |
| 77 | + else: |
| 78 | + current_pos["x"] += 1 |
| 79 | + steps += 1 |
| 80 | + return steps |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments