|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """ne,ne,ne""", |
| 8 | + "expected": ['Unknown', 'Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test = 'real' |
| 12 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 13 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 14 | + "expected": ['877', '1622'], |
| 15 | + } |
| 16 | + |
| 17 | +# -------------------------------- Control program execution -------------------------------- # |
| 18 | + |
| 19 | +case_to_test = 'real' |
| 20 | +part_to_test = 2 |
| 21 | +verbose_level = 1 |
| 22 | + |
| 23 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 24 | + |
| 25 | +puzzle_input = test_data[case_to_test]['input'] |
| 26 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 27 | +puzzle_actual_result = 'Unknown' |
| 28 | + |
| 29 | + |
| 30 | +# -------------------------------- Actual code execution -------------------------------- # |
| 31 | + |
| 32 | +if part_to_test == 1: |
| 33 | + for string in puzzle_input.split('\n'): |
| 34 | + # Simplifies counting |
| 35 | + string = string.split(',') |
| 36 | + nb_se = string.count('se') |
| 37 | + nb_sw = string.count('sw') |
| 38 | + nb_s = string.count('s') |
| 39 | + nb_ne = string.count('ne') |
| 40 | + nb_nw = string.count('nw') |
| 41 | + nb_n = string.count('n') |
| 42 | + |
| 43 | + # This just makes sure all conversions are done twice (as they influence each other) |
| 44 | + for i in range (2): |
| 45 | + |
| 46 | + # Convert se+sw in s |
| 47 | + nb_s += min(nb_se, nb_sw) |
| 48 | + nb_se, nb_sw = nb_se - min(nb_se, nb_sw), nb_sw - min(nb_se, nb_sw) |
| 49 | + |
| 50 | + # Convert ne+nw in n |
| 51 | + nb_n += min(nb_ne, nb_nw) |
| 52 | + nb_ne, nb_nw = nb_ne - min(nb_ne, nb_nw), nb_nw - min(nb_ne, nb_nw) |
| 53 | + |
| 54 | + # Convert sw+n in nw |
| 55 | + nb_nw += min(nb_sw, nb_n) |
| 56 | + nb_sw, nb_n = nb_sw - min(nb_sw, nb_n), nb_n - min(nb_sw, nb_n) |
| 57 | + |
| 58 | + # Convert nw+s in sw |
| 59 | + nb_sw += min(nb_nw, nb_s) |
| 60 | + nb_nw, nb_s = nb_nw - min(nb_nw, nb_s), nb_s - min(nb_nw, nb_s) |
| 61 | + |
| 62 | + # Convert se+n in ne |
| 63 | + nb_ne += min(nb_se, nb_n) |
| 64 | + nb_se, nb_n = nb_se - min(nb_se, nb_n), nb_n - min(nb_se, nb_n) |
| 65 | + |
| 66 | + # Convert ne+s in se |
| 67 | + nb_se += min(nb_ne, nb_s) |
| 68 | + nb_ne, nb_s = nb_ne - min(nb_ne, nb_s), nb_s - min(nb_ne, nb_s) |
| 69 | + |
| 70 | + # Cancel ne and sw |
| 71 | + nb_ne, nb_sw = nb_ne - min(nb_ne, nb_sw), nb_sw - min(nb_ne, nb_sw) |
| 72 | + |
| 73 | + # Cancel nw and se |
| 74 | + nb_nw, nb_se = nb_nw - min(nb_nw, nb_se), nb_se - min(nb_ne, nb_se) |
| 75 | + |
| 76 | + # Cancel n and s |
| 77 | + nb_n, nb_s = nb_n - min(nb_n, nb_s), nb_s - min(nb_n, nb_s) |
| 78 | + |
| 79 | + puzzle_actual_result = sum([nb_se, nb_sw, nb_s, nb_ne, nb_nw, nb_n]) |
| 80 | + |
| 81 | +else: |
| 82 | + max_distance = 0 |
| 83 | + |
| 84 | + all_steps = puzzle_input.split(',') |
| 85 | + |
| 86 | + for i in range (len(all_steps)): |
| 87 | + steps = all_steps[0:i+1] |
| 88 | + |
| 89 | + nb_se = steps.count('se') |
| 90 | + nb_sw = steps.count('sw') |
| 91 | + nb_s = steps.count('s') |
| 92 | + nb_ne = steps.count('ne') |
| 93 | + nb_nw = steps.count('nw') |
| 94 | + nb_n = steps.count('n') |
| 95 | + |
| 96 | + # This just makes sure all conversions are done twice (as they influence each other) |
| 97 | + for i in range (2): |
| 98 | + |
| 99 | + # Convert se+sw in s |
| 100 | + nb_s += min(nb_se, nb_sw) |
| 101 | + nb_se, nb_sw = nb_se - min(nb_se, nb_sw), nb_sw - min(nb_se, nb_sw) |
| 102 | + |
| 103 | + # Convert ne+nw in n |
| 104 | + nb_n += min(nb_ne, nb_nw) |
| 105 | + nb_ne, nb_nw = nb_ne - min(nb_ne, nb_nw), nb_nw - min(nb_ne, nb_nw) |
| 106 | + |
| 107 | + # Convert sw+n in nw |
| 108 | + nb_nw += min(nb_sw, nb_n) |
| 109 | + nb_sw, nb_n = nb_sw - min(nb_sw, nb_n), nb_n - min(nb_sw, nb_n) |
| 110 | + |
| 111 | + # Convert nw+s in sw |
| 112 | + nb_sw += min(nb_nw, nb_s) |
| 113 | + nb_nw, nb_s = nb_nw - min(nb_nw, nb_s), nb_s - min(nb_nw, nb_s) |
| 114 | + |
| 115 | + # Convert se+n in ne |
| 116 | + nb_ne += min(nb_se, nb_n) |
| 117 | + nb_se, nb_n = nb_se - min(nb_se, nb_n), nb_n - min(nb_se, nb_n) |
| 118 | + |
| 119 | + # Convert ne+s in se |
| 120 | + nb_se += min(nb_ne, nb_s) |
| 121 | + nb_ne, nb_s = nb_ne - min(nb_ne, nb_s), nb_s - min(nb_ne, nb_s) |
| 122 | + |
| 123 | + # Cancel ne and sw |
| 124 | + nb_ne, nb_sw = nb_ne - min(nb_ne, nb_sw), nb_sw - min(nb_ne, nb_sw) |
| 125 | + |
| 126 | + # Cancel nw and se |
| 127 | + nb_nw, nb_se = nb_nw - min(nb_nw, nb_se), nb_se - min(nb_ne, nb_se) |
| 128 | + |
| 129 | + # Cancel n and s |
| 130 | + nb_n, nb_s = nb_n - min(nb_n, nb_s), nb_s - min(nb_n, nb_s) |
| 131 | + |
| 132 | + max_distance = max(max_distance, sum([nb_se, nb_sw, nb_s, nb_ne, nb_nw, nb_n])) |
| 133 | + |
| 134 | + puzzle_actual_result = max_distance |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +# -------------------------------- Outputs / results -------------------------------- # |
| 139 | + |
| 140 | +if verbose_level >= 3: |
| 141 | + print ('Input : ' + puzzle_input) |
| 142 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 143 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments