|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. |
| 8 | + Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.""", |
| 9 | + "race_duration": 1000, |
| 10 | + "expected": ['1120', '689 according to the website, 688 according to my math'], |
| 11 | + } |
| 12 | +test = 'real' |
| 13 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 14 | +test_data[test] = {"input": open(input_file, "r+").read(), |
| 15 | + "race_duration": 2503, |
| 16 | + "expected": ['2696', '1084'], |
| 17 | + } |
| 18 | + |
| 19 | +# -------------------------------- Control program execution -------------------------------- # |
| 20 | + |
| 21 | +case_to_test = 'real' |
| 22 | +part_to_test = 2 |
| 23 | +verbose_level = 1 |
| 24 | + |
| 25 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 26 | + |
| 27 | +puzzle_input = test_data[case_to_test]['input'] |
| 28 | +race_duration = int(test_data[case_to_test]['race_duration']) |
| 29 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 30 | +puzzle_actual_result = 'Unknown' |
| 31 | + |
| 32 | + |
| 33 | +# -------------------------------- Actual code execution -------------------------------- # |
| 34 | + |
| 35 | +if part_to_test == 1: |
| 36 | + reindeers_distance = {} |
| 37 | + for string in puzzle_input.split('\n'): |
| 38 | + if string == '': |
| 39 | + continue |
| 40 | + |
| 41 | + reindeer, _, _, speed, _, _, fly_duration, _, _, _, _, _, _, sleep_duration, _ = string.split() |
| 42 | + fly_duration = int(fly_duration) |
| 43 | + sleep_duration = int(sleep_duration) |
| 44 | + speed = int(speed) |
| 45 | + |
| 46 | + full_cycles = race_duration // (fly_duration + sleep_duration) |
| 47 | + remaining_duration = race_duration % (fly_duration + sleep_duration) |
| 48 | + |
| 49 | + reindeers_distance[reindeer] = full_cycles * fly_duration * speed |
| 50 | + |
| 51 | + |
| 52 | + if remaining_duration >= fly_duration: |
| 53 | + reindeers_distance[reindeer] += fly_duration * speed |
| 54 | + else: |
| 55 | + reindeers_distance[reindeer] += remaining_duration * speed |
| 56 | + |
| 57 | + max_distance = max(reindeers_distance.values()) |
| 58 | + puzzle_actual_result = max_distance |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +else: |
| 64 | + reindeers_data = {} |
| 65 | + reindeers_points = {} |
| 66 | + reindeers_distance = {} |
| 67 | + for string in puzzle_input.split('\n'): |
| 68 | + if string == '': |
| 69 | + continue |
| 70 | + |
| 71 | + reindeer, _, _, speed, _, _, fly_duration, _, _, _, _, _, _, sleep_duration, _ = string.split() |
| 72 | + fly_duration = int(fly_duration) |
| 73 | + sleep_duration = int(sleep_duration) |
| 74 | + speed = int(speed) |
| 75 | + reindeers_data.setdefault(reindeer, dict()) |
| 76 | + reindeers_data[reindeer]['fly_duration'] = int(fly_duration) |
| 77 | + reindeers_data[reindeer]['sleep_duration'] = int(sleep_duration) |
| 78 | + reindeers_data[reindeer]['speed'] = int(speed) |
| 79 | + |
| 80 | + reindeers_distance = {k: 0 for k in reindeers_data.keys()} |
| 81 | + reindeers_points = {k: 0 for k in reindeers_data.keys()} |
| 82 | + for i in range(1, race_duration+1): |
| 83 | + for reindeer in reindeers_data.keys(): |
| 84 | + if (i-1) % (reindeers_data[reindeer]['fly_duration'] + reindeers_data[reindeer]['sleep_duration']) < reindeers_data[reindeer]['fly_duration']: |
| 85 | + reindeers_distance[reindeer] += reindeers_data[reindeer]['speed'] |
| 86 | + |
| 87 | + max_distance = max(reindeers_distance.values()) |
| 88 | + winning_reindeer = [x for x in reindeers_data.keys() if reindeers_distance[x] == max_distance][0] |
| 89 | + reindeers_points[winning_reindeer] += 1 |
| 90 | + # print (i, reindeers_distance, 'Lead is', [x for x in reindeers_distance if reindeers_distance[x] == max_distance][0], reindeers_points) |
| 91 | + |
| 92 | + max_points = max(reindeers_points.values()) |
| 93 | + puzzle_actual_result = max_points |
| 94 | + print (reindeers_points) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +# -------------------------------- Outputs / results -------------------------------- # |
| 99 | + |
| 100 | +if verbose_level >= 3: |
| 101 | + print ('Input : ' + puzzle_input) |
| 102 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 103 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments