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