|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding, re |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | +from math import ceil |
| 6 | + |
| 7 | +test_data = {} |
| 8 | + |
| 9 | +test = 1 |
| 10 | +test_data[test] = { |
| 11 | + "input": """10 ORE => 10 A |
| 12 | +1 ORE => 1 B |
| 13 | +7 A, 1 B => 1 C |
| 14 | +7 A, 1 C => 1 D |
| 15 | +7 A, 1 D => 1 E |
| 16 | +7 A, 1 E => 1 FUEL |
| 17 | +6 HTRFP, 1 FVXV, 4 JKLNF, 1 TXFCS, 2 PXBP => 4 JRBFT""", |
| 18 | + "expected": ["31", "Unknown"], |
| 19 | +} |
| 20 | + |
| 21 | +test += 1 |
| 22 | +test_data[test] = { |
| 23 | + "input": """157 ORE => 5 NZVS |
| 24 | +165 ORE => 6 DCFZ |
| 25 | +44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL |
| 26 | +12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ |
| 27 | +179 ORE => 7 PSHF |
| 28 | +177 ORE => 5 HKGWZ |
| 29 | +7 DCFZ, 7 PSHF => 2 XJWVT |
| 30 | +165 ORE => 2 GPVTF |
| 31 | +3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT""", |
| 32 | + "expected": ["13312", "82892753"], |
| 33 | +} |
| 34 | + |
| 35 | +test = "real" |
| 36 | +input_file = os.path.join( |
| 37 | + os.path.dirname(__file__), |
| 38 | + "Inputs", |
| 39 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 40 | +) |
| 41 | +test_data[test] = { |
| 42 | + "input": open(input_file, "r+").read().strip(), |
| 43 | + "expected": ["1037742", "1572358"], |
| 44 | +} |
| 45 | + |
| 46 | +# -------------------------------- Control program execution ------------------------- # |
| 47 | + |
| 48 | +case_to_test = "real" |
| 49 | +part_to_test = 2 |
| 50 | + |
| 51 | +# -------------------------------- Initialize some variables ------------------------- # |
| 52 | + |
| 53 | +puzzle_input = test_data[case_to_test]["input"] |
| 54 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 55 | +puzzle_actual_result = "Unknown" |
| 56 | + |
| 57 | + |
| 58 | +# -------------------------------- Actual code execution ----------------------------- # |
| 59 | + |
| 60 | + |
| 61 | +def execute_reaction(stock, reaction, required): |
| 62 | + global ore_required |
| 63 | + target = reaction[1] |
| 64 | + nb_reactions = ceil((required[target] - stock.get(target, 0)) / reaction[0]) |
| 65 | + |
| 66 | + # Impact on target material |
| 67 | + stock[target] = stock.get(target, 0) + nb_reactions * reaction[0] - required[target] |
| 68 | + del required[target] |
| 69 | + |
| 70 | + # Impact on other materials |
| 71 | + for i in range(len(reaction[2]) // 2): |
| 72 | + nb_required, mat = reaction[2][i * 2 : i * 2 + 2] |
| 73 | + nb_required = int(nb_required) * nb_reactions |
| 74 | + if mat == "ORE" and part_to_test == 1: |
| 75 | + ore_required += nb_required |
| 76 | + elif stock.get(mat, 0) >= nb_required: |
| 77 | + stock[mat] -= nb_required |
| 78 | + else: |
| 79 | + missing = nb_required - stock.get(mat, 0) |
| 80 | + stock[mat] = 0 |
| 81 | + required[mat] = required.get(mat, 0) + missing |
| 82 | + |
| 83 | + |
| 84 | +reactions = {} |
| 85 | +for string in puzzle_input.split("\n"): |
| 86 | + if string == "": |
| 87 | + continue |
| 88 | + |
| 89 | + source, target = string.split(" => ") |
| 90 | + nb, target = target.split(" ") |
| 91 | + nb = int(nb) |
| 92 | + |
| 93 | + sources = source.replace(",", "").split(" ") |
| 94 | + |
| 95 | + reactions[target] = (nb, target, sources) |
| 96 | + |
| 97 | + |
| 98 | +if part_to_test == 1: |
| 99 | + required = {"FUEL": 1} |
| 100 | + ore_required = 0 |
| 101 | + stock = {} |
| 102 | + while len(required) > 0: |
| 103 | + material = list(required.keys())[0] |
| 104 | + execute_reaction(stock, reactions[material], required) |
| 105 | + |
| 106 | + puzzle_actual_result = ore_required |
| 107 | + |
| 108 | + |
| 109 | +else: |
| 110 | + below, above = 1000000000000 // 1037742, 1000000000000 |
| 111 | + |
| 112 | + while below != above - 1: |
| 113 | + required = {"FUEL": (below + above) // 2} |
| 114 | + stock = {"ORE": 1000000000000} |
| 115 | + while len(required) > 0 and "ORE" not in required: |
| 116 | + material = list(required.keys())[0] |
| 117 | + execute_reaction(stock, reactions[material], required) |
| 118 | + |
| 119 | + if stock["ORE"] == 0 or "ORE" in required: |
| 120 | + above = (below + above) // 2 |
| 121 | + else: |
| 122 | + below = (below + above) // 2 |
| 123 | + |
| 124 | + puzzle_actual_result = below |
| 125 | + |
| 126 | + |
| 127 | +# -------------------------------- Outputs / results --------------------------------- # |
| 128 | + |
| 129 | +print("Expected result : " + str(puzzle_expected_result)) |
| 130 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments