|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding, re, math, copy |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | + |
| 6 | +test_data = {} |
| 7 | + |
| 8 | +test = 1 |
| 9 | +test_data[test] = { |
| 10 | + "input": """<x=-1, y=0, z=2> |
| 11 | +<x=2, y=-10, z=-7> |
| 12 | +<x=4, y=-8, z=8> |
| 13 | +<x=3, y=5, z=-1>""", |
| 14 | + "expected": ["179 after 10 steps", "2772"], |
| 15 | +} |
| 16 | + |
| 17 | +test = "real" |
| 18 | +input_file = os.path.join( |
| 19 | + os.path.dirname(__file__), |
| 20 | + "Inputs", |
| 21 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 22 | +) |
| 23 | +test_data[test] = { |
| 24 | + "input": open(input_file, "r+").read().strip(), |
| 25 | + "expected": ["12773", "306798770391636"], |
| 26 | +} |
| 27 | + |
| 28 | +# -------------------------------- Control program execution ------------------------- # |
| 29 | + |
| 30 | +case_to_test = "real" |
| 31 | +part_to_test = 2 |
| 32 | + |
| 33 | +# -------------------------------- Initialize some variables ------------------------- # |
| 34 | + |
| 35 | +puzzle_input = test_data[case_to_test]["input"] |
| 36 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 37 | +puzzle_actual_result = "Unknown" |
| 38 | + |
| 39 | + |
| 40 | +# -------------------------------- Actual code execution ----------------------------- # |
| 41 | + |
| 42 | +stars = [] |
| 43 | +for string in puzzle_input.split("\n"): |
| 44 | + x, y, z = map(int, re.findall("[-0-9]{1,}", string)) |
| 45 | + stars.append([x, y, z, 0, 0, 0]) |
| 46 | + |
| 47 | +if part_to_test == 1: |
| 48 | + for step in range(1000): |
| 49 | + for star_id in range(len(stars)): |
| 50 | + for coord in range(3): |
| 51 | + stars[star_id][3 + coord] += sum( |
| 52 | + [1 for other in stars if stars[star_id][coord] < other[coord]] |
| 53 | + ) |
| 54 | + stars[star_id][3 + coord] += sum( |
| 55 | + [-1 for other in stars if stars[star_id][coord] > other[coord]] |
| 56 | + ) |
| 57 | + |
| 58 | + for star_id in range(len(stars)): |
| 59 | + for coord in range(3): |
| 60 | + stars[star_id][coord] += stars[star_id][3 + coord] |
| 61 | + |
| 62 | + energy = sum( |
| 63 | + [ |
| 64 | + (abs(x) + abs(y) + abs(z)) * (abs(dx) + abs(dy) + abs(dz)) |
| 65 | + for (x, y, z, dx, dy, dz) in stars |
| 66 | + ] |
| 67 | + ) |
| 68 | + puzzle_actual_result = energy |
| 69 | + |
| 70 | +else: |
| 71 | + |
| 72 | + # 1st trick: For this part, do the computation on each axis independently (since they're independent) |
| 73 | + # 2nd trick: the function state => next state is invertible, so any repetition will go through the initial state (we can't have 3>0>1>0>1>0>1, it has to be something like 3>0>1>3>0>1) |
| 74 | + repeats = [] |
| 75 | + for coord in range(3): |
| 76 | + step = -1 |
| 77 | + repeat = 0 |
| 78 | + stars_pos_vel = [ |
| 79 | + [stars[star_id][coord], stars[star_id][coord + 3]] |
| 80 | + for star_id in range(len(stars)) |
| 81 | + ] |
| 82 | + init_stars_pos_vel = [ |
| 83 | + [stars[star_id][coord], stars[star_id][coord + 3]] |
| 84 | + for star_id in range(len(stars)) |
| 85 | + ] |
| 86 | + |
| 87 | + while repeat == 0: # and step < 20: |
| 88 | + step += 1 |
| 89 | + for star_id in range(len(stars)): |
| 90 | + stars_pos_vel[star_id][1] += sum( |
| 91 | + [ |
| 92 | + 1 |
| 93 | + for other in stars_pos_vel |
| 94 | + if stars_pos_vel[star_id][0] < other[0] |
| 95 | + ] |
| 96 | + ) |
| 97 | + stars_pos_vel[star_id][1] -= sum( |
| 98 | + [ |
| 99 | + 1 |
| 100 | + for other in stars_pos_vel |
| 101 | + if stars_pos_vel[star_id][0] > other[0] |
| 102 | + ] |
| 103 | + ) |
| 104 | + |
| 105 | + for star_id in range(len(stars)): |
| 106 | + stars_pos_vel[star_id][0] += stars_pos_vel[star_id][1] |
| 107 | + |
| 108 | + if stars_pos_vel == init_stars_pos_vel: |
| 109 | + repeat = step + 1 |
| 110 | + |
| 111 | + repeats.append(repeat) |
| 112 | + |
| 113 | + lcm = repeats[0] |
| 114 | + for val in repeats: |
| 115 | + lcm = lcm * val // math.gcd(lcm, val) |
| 116 | + |
| 117 | + puzzle_actual_result = lcm |
| 118 | + |
| 119 | +# -------------------------------- Outputs / results --------------------------------- # |
| 120 | + |
| 121 | +print("Expected result : " + str(puzzle_expected_result)) |
| 122 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments