|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, itertools |
| 3 | + |
| 4 | +from operator import mul |
| 5 | +from functools import reduce |
| 6 | + |
| 7 | +test_data = {} |
| 8 | + |
| 9 | +test = 1 |
| 10 | +test_data[test] = {"input": """1 |
| 11 | +2 |
| 12 | +3 |
| 13 | +4 |
| 14 | +5 |
| 15 | +7 |
| 16 | +8 |
| 17 | +9 |
| 18 | +10 |
| 19 | +11""", |
| 20 | + "expected": ['99', 'Unknown'], |
| 21 | + } |
| 22 | + |
| 23 | +test = 'real' |
| 24 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 25 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 26 | + "expected": ['11846773891', 'Unknown'], |
| 27 | + } |
| 28 | + |
| 29 | +# -------------------------------- Control program execution -------------------------------- # |
| 30 | + |
| 31 | +case_to_test = 'real' |
| 32 | +part_to_test = 2 |
| 33 | +verbose_level = 1 |
| 34 | + |
| 35 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 36 | + |
| 37 | +puzzle_input = test_data[case_to_test]['input'] |
| 38 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 39 | +puzzle_actual_result = 'Unknown' |
| 40 | + |
| 41 | + |
| 42 | +# -------------------------------- Actual code execution -------------------------------- # |
| 43 | + |
| 44 | +list_packages = [] |
| 45 | + |
| 46 | +mini_quantum_entanglement = 10 ** 100 |
| 47 | + |
| 48 | +list_packages = [int(x) for x in puzzle_input.split('\n')] |
| 49 | +total_weight = sum(list_packages) |
| 50 | +group_weight = total_weight // 3 if part_to_test == 1 else total_weight // 4 |
| 51 | + |
| 52 | +for group1_size in range (1, len(list_packages) - 2): |
| 53 | + print('Testing with group 1 of size', group1_size) |
| 54 | + for group1 in itertools.combinations(list_packages, group1_size): |
| 55 | + if sum(group1) != group_weight: |
| 56 | + continue |
| 57 | + if reduce(mul, group1, 1) >= mini_quantum_entanglement: |
| 58 | + continue |
| 59 | + |
| 60 | + remaining_packages = [x for x in list_packages if x not in group1] |
| 61 | + |
| 62 | + for group2_size in range (1, len(remaining_packages) - 2): |
| 63 | + print('Testing with group 2 of size', group2_size) |
| 64 | + for group2 in itertools.combinations(remaining_packages, group2_size): |
| 65 | + if sum(group2) == group_weight: |
| 66 | + mini_quantum_entanglement = min(mini_quantum_entanglement, reduce(mul, group1, 1)) |
| 67 | + |
| 68 | + if mini_quantum_entanglement != 10 ** 100: |
| 69 | + break |
| 70 | + |
| 71 | +puzzle_actual_result = mini_quantum_entanglement |
| 72 | + |
| 73 | + |
| 74 | +# -------------------------------- Outputs / results -------------------------------- # |
| 75 | + |
| 76 | +if verbose_level >= 3: |
| 77 | + print ('Input : ' + puzzle_input) |
| 78 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 79 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
0 commit comments