|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """p=<3,0,0>, v=<2,0,0>, a=<-1,0,0> |
| 8 | +p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>""", |
| 9 | + "expected": ['Unknown', 'Unknown'], |
| 10 | + } |
| 11 | + |
| 12 | +test += 1 |
| 13 | +test_data[test] = {"input": """p=<-6,0,0>, v=<3,0,0>, a=<0,0,0> |
| 14 | +p=<-4,0,0>, v=<2,0,0>, a=<0,0,0> |
| 15 | +p=<-2,0,0>, v=<1,0,0>, a=<0,0,0> |
| 16 | +p=<3,0,0>, v=<-1,0,0>, a=<0,0,0>""", |
| 17 | + "expected": ['Unknown', 'Unknown'], |
| 18 | + } |
| 19 | + |
| 20 | +test = 'real' |
| 21 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 22 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 23 | + "expected": ['Unknown', 'Unknown'], |
| 24 | + } |
| 25 | + |
| 26 | +# -------------------------------- Control program execution -------------------------------- # |
| 27 | + |
| 28 | +case_to_test = 'real' |
| 29 | +part_to_test = 2 |
| 30 | +verbose_level = 1 |
| 31 | + |
| 32 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 33 | + |
| 34 | +puzzle_input = test_data[case_to_test]['input'] |
| 35 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 36 | +puzzle_actual_result = 'Unknown' |
| 37 | + |
| 38 | + |
| 39 | +# -------------------------------- Actual code execution -------------------------------- # |
| 40 | + |
| 41 | +max_accel = 10**6 |
| 42 | + |
| 43 | +if part_to_test == 1: |
| 44 | + part_nr = 0 |
| 45 | + for string in puzzle_input.split('\n'): |
| 46 | + _, _, acceleration = string.split(' ') |
| 47 | + acceleration = list(map(int, acceleration[3:-1].split(','))) |
| 48 | + |
| 49 | + if max_accel > sum(map(abs, acceleration)): |
| 50 | + max_accel = sum(map(abs, acceleration)) |
| 51 | + closest_part = part_nr |
| 52 | + |
| 53 | + part_nr += 1 |
| 54 | + |
| 55 | + puzzle_actual_result = closest_part |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +else: |
| 60 | + particles = {} |
| 61 | + collisions = [] |
| 62 | + part_nr = 0 |
| 63 | + saved_len = 0 |
| 64 | + for string in puzzle_input.split('\n'): |
| 65 | + position, speed, acceleration = string.split(' ') |
| 66 | + position = list(map(int, position[3:-2].split(','))) |
| 67 | + speed = list(map(int, speed[3:-2].split(','))) |
| 68 | + acceleration = list(map(int, acceleration[3:-1].split(','))) |
| 69 | + |
| 70 | + particles[part_nr] = [position, speed, acceleration] |
| 71 | + |
| 72 | + part_nr += 1 |
| 73 | + |
| 74 | + for i in range(10**4): |
| 75 | + collisions = [] |
| 76 | + for part_nr in particles: |
| 77 | + position, speed, acceleration = particles[part_nr] |
| 78 | + speed = [speed[x] + acceleration[x] for x in range (3)] |
| 79 | + position = [position[x] + speed[x] for x in range (3)] |
| 80 | + particles[part_nr] = [position, speed, acceleration] |
| 81 | + collisions.append(position) |
| 82 | + |
| 83 | + coordinates = [','.join(map(str, collision)) for collision in collisions] |
| 84 | + |
| 85 | + list_particles = list(particles.keys()) |
| 86 | + for part_nr in list_particles: |
| 87 | + if collisions.count(particles[part_nr][0]) > 1: |
| 88 | + del particles[part_nr] |
| 89 | + |
| 90 | + if i % 10 == 0 and len(particles) == saved_len: |
| 91 | + break |
| 92 | + elif i % 10 == 0: |
| 93 | + saved_len = len(particles) |
| 94 | + |
| 95 | + print(i, len(particles)) |
| 96 | + |
| 97 | + puzzle_actual_result = len(particles) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +# -------------------------------- Outputs / results -------------------------------- # |
| 103 | + |
| 104 | +if verbose_level >= 3: |
| 105 | + print ('Input : ' + puzzle_input) |
| 106 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 107 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments