|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """pbga (66) |
| 8 | +xhth (57) |
| 9 | +ebii (61) |
| 10 | +havc (66) |
| 11 | +ktlj (57) |
| 12 | +fwft (72) -> ktlj, cntj, xhth |
| 13 | +qoyq (66) |
| 14 | +padx (45) -> pbga, havc, qoyq |
| 15 | +tknk (41) -> ugml, padx, fwft |
| 16 | +jptl (61) |
| 17 | +ugml (68) -> gyxo, ebii, jptl |
| 18 | +gyxo (61) |
| 19 | +cntj (57)""", |
| 20 | + "expected": ['Unknown', 'Unknown'], |
| 21 | + } |
| 22 | + |
| 23 | +test += 1 |
| 24 | +test_data[test] = {"input": """""", |
| 25 | + "expected": ['Unknown', 'Unknown'], |
| 26 | + } |
| 27 | + |
| 28 | +test = 'real' |
| 29 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 30 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 31 | + "expected": ['vtzay', '910'], |
| 32 | + } |
| 33 | + |
| 34 | +# -------------------------------- Control program execution -------------------------------- # |
| 35 | + |
| 36 | +case_to_test = 'real' |
| 37 | +part_to_test = 2 |
| 38 | +verbose_level = 1 |
| 39 | + |
| 40 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 41 | + |
| 42 | +puzzle_input = test_data[case_to_test]['input'] |
| 43 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 44 | +puzzle_actual_result = 'Unknown' |
| 45 | + |
| 46 | + |
| 47 | +# -------------------------------- Actual code execution -------------------------------- # |
| 48 | + |
| 49 | +all_held = [] |
| 50 | +holders = [] |
| 51 | +individual_weights = {} |
| 52 | +branches = {} |
| 53 | +for string in puzzle_input.split('\n'): |
| 54 | + if string == '': |
| 55 | + continue |
| 56 | + |
| 57 | + individual_weights[string.split(' ')[0]] = int(string.split(' ')[1][1:-1]) |
| 58 | + |
| 59 | + is_holder = string.split('->') |
| 60 | + if len(is_holder) == 1: |
| 61 | + all_held.append(string.split(' ')[0]) |
| 62 | + continue |
| 63 | + |
| 64 | + holder, weight, _, *held = string.split(' ') |
| 65 | + all_held += [x.replace(',', '') for x in held] |
| 66 | + holders.append(holder) |
| 67 | + |
| 68 | + branches[holder] = [x.replace(',', '') for x in held] |
| 69 | + |
| 70 | +for holder in holders: |
| 71 | + if holder not in all_held: |
| 72 | + puzzle_actual_result = holder |
| 73 | + break |
| 74 | + |
| 75 | + |
| 76 | +if part_to_test == 2: |
| 77 | + unknown_weights = holders.copy() |
| 78 | + held_weight = {} |
| 79 | + total_weight = {x:individual_weights[x] for x in individual_weights if x not in holders} |
| 80 | + mismatch = {} |
| 81 | + while len(unknown_weights): |
| 82 | + for holder in unknown_weights: |
| 83 | + if all([x in total_weight for x in branches[holder]]): |
| 84 | + # We know the weights of all leaves, including sub-towers |
| 85 | + |
| 86 | + held_weight[holder] = [total_weight[x] for x in branches[holder]] |
| 87 | + if any([x != held_weight[holder][0] for x in held_weight[holder]]): |
| 88 | + mismatch.update({holder: held_weight[holder]}) |
| 89 | + total_weight[holder] = sum(held_weight[holder]) + individual_weights[holder] |
| 90 | + unknown_weights.remove(holder) |
| 91 | + |
| 92 | + # This is very ugly code |
| 93 | + # First, determine which mismatch disk has the minimum weight (because that's the closest to the problem) |
| 94 | + min_weight = min([y for x in mismatch for y in mismatch[x]]) |
| 95 | + min_holder = [x for x in mismatch if min_weight in mismatch[x]][0] |
| 96 | + |
| 97 | + # Then, determine what are the correct and incorrect weights |
| 98 | + count_weights = {mismatch[min_holder].count(x):x for x in mismatch[min_holder]} |
| 99 | + wrong_weight = count_weights[1] |
| 100 | + correct_weight = count_weights[len(mismatch[min_holder])-1] |
| 101 | + delta = correct_weight - wrong_weight |
| 102 | + |
| 103 | + # Find which tower has the wrong individual weight, then calculate its new weight |
| 104 | + wrong_holder = [x for x in branches[min_holder] if total_weight[x] == wrong_weight][0] |
| 105 | + new_weight = individual_weights[wrong_holder] + delta |
| 106 | + |
| 107 | + puzzle_actual_result = new_weight |
| 108 | + |
| 109 | + |
| 110 | +# -------------------------------- Outputs / results -------------------------------- # |
| 111 | + |
| 112 | +if verbose_level >= 3: |
| 113 | + print ('Input : ' + puzzle_input) |
| 114 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 115 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
0 commit comments