|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """cpy 2 a |
| 8 | +tgl a |
| 9 | +tgl a |
| 10 | +tgl a |
| 11 | +cpy 1 a |
| 12 | +dec a |
| 13 | +dec a""", |
| 14 | + "expected": ['3', 'Unknown'], |
| 15 | + } |
| 16 | + |
| 17 | +test += 1 |
| 18 | +test_data[test] = {"input": """""", |
| 19 | + "expected": ['Unknown', 'Unknown'], |
| 20 | + } |
| 21 | + |
| 22 | +test = 'real' |
| 23 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 24 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 25 | + "expected": ['10886', '479007446'], |
| 26 | + } |
| 27 | + |
| 28 | +# -------------------------------- Control program execution -------------------------------- # |
| 29 | + |
| 30 | +case_to_test = 'real' |
| 31 | +part_to_test = 1 |
| 32 | +verbose_level = 1 |
| 33 | + |
| 34 | + |
| 35 | +def RepresentsInt(s): |
| 36 | + try: |
| 37 | + int(s) |
| 38 | + return True |
| 39 | + except ValueError: |
| 40 | + return False |
| 41 | + |
| 42 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 43 | + |
| 44 | +puzzle_input = test_data[case_to_test]['input'] |
| 45 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 46 | +puzzle_actual_result = 'Unknown' |
| 47 | + |
| 48 | + |
| 49 | +# -------------------------------- Actual code execution -------------------------------- # |
| 50 | +registers = {'a':0, 'b':0, 'c':0, 'd':0} |
| 51 | + |
| 52 | +instructions = puzzle_input.split('\n') |
| 53 | +i = 0 |
| 54 | +if case_to_test == 'real': |
| 55 | + if part_to_test == 1: |
| 56 | + registers['a'] = 7 |
| 57 | + else: |
| 58 | + registers['a'] = 12 |
| 59 | + |
| 60 | +while True: |
| 61 | + instruction = instructions[i] |
| 62 | + i += 1 |
| 63 | + |
| 64 | +# print (i, instruction, registers) |
| 65 | + |
| 66 | + if instruction[0:3] == 'cpy': |
| 67 | + _, val, target = instruction.split(' ') |
| 68 | + try: |
| 69 | + registers[target] = int(val) |
| 70 | + except ValueError: |
| 71 | + registers[target] = registers[val] |
| 72 | + |
| 73 | + elif instruction[0:3] == 'inc': |
| 74 | + _, target = instruction.split(' ') |
| 75 | + if target in registers: |
| 76 | + registers[target] += 1 |
| 77 | + elif instruction[0:3] == 'dec': |
| 78 | + _, target = instruction.split(' ') |
| 79 | + if target in registers: |
| 80 | + registers[target] -= 1 |
| 81 | + |
| 82 | + elif instruction[0:3] == 'tgl': |
| 83 | + _, target = instruction.split(' ') |
| 84 | + target = registers[target] |
| 85 | + |
| 86 | + target_position = i+target-1 # -1 because we added 1 to i before |
| 87 | + if target_position < len(instructions): |
| 88 | + |
| 89 | + if instructions[target_position][0:3] == 'inc': |
| 90 | + instructions[target_position] = 'dec' + instructions[target_position][3:] |
| 91 | + elif instructions[target_position][0:3] == 'dec': |
| 92 | + instructions[target_position] = 'inc' + instructions[target_position][3:] |
| 93 | + elif instructions[target_position][0:3] == 'jnz': |
| 94 | + instructions[target_position] = 'cpy' + instructions[target_position][3:] |
| 95 | + elif instructions[target_position][0:3] == 'cpy': |
| 96 | + instructions[target_position] = 'jnz' + instructions[target_position][3:] |
| 97 | + elif instructions[target_position][0:3] == 'tgl': |
| 98 | + instructions[target_position] = 'inc' + instructions[target_position][3:] |
| 99 | + |
| 100 | + elif instruction[0:3] == 'jnz': |
| 101 | + _, target, jump = instruction.split(' ') |
| 102 | + if target == '0': |
| 103 | + pass |
| 104 | + else: |
| 105 | + if RepresentsInt(target) and RepresentsInt(jump): |
| 106 | + i = i + int(jump) - 1 # -1 to compensate for what we added before |
| 107 | + elif RepresentsInt(target): |
| 108 | + i = i + registers[jump] - 1 # -1 to compensate for what we added before |
| 109 | + elif RepresentsInt(jump): |
| 110 | + if registers[target] != 0: |
| 111 | + i = i + int(jump) - 1 # -1 to compensate for what we added before |
| 112 | + |
| 113 | + elif instruction[0:3] == 'add': |
| 114 | + _, source, target = instruction.split(' ') |
| 115 | + if source == '0': |
| 116 | + pass |
| 117 | + else: |
| 118 | + if RepresentsInt(source): |
| 119 | + registers[target] += int(source) |
| 120 | + else: |
| 121 | + registers[target] += registers[source] |
| 122 | + |
| 123 | + elif instruction[0:3] == 'mul': |
| 124 | + _, source, target = instruction.split(' ') |
| 125 | + if source == '0': |
| 126 | + pass |
| 127 | + else: |
| 128 | + if RepresentsInt(source): |
| 129 | + registers[target] *= int(source) |
| 130 | + else: |
| 131 | + registers[target] *= registers[source] |
| 132 | + |
| 133 | + if i >= len(instructions): |
| 134 | + break |
| 135 | + |
| 136 | +puzzle_actual_result = registers['a'] |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +# -------------------------------- Outputs / results -------------------------------- # |
| 143 | + |
| 144 | +if verbose_level >= 3: |
| 145 | + print ('Input : ' + puzzle_input) |
| 146 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 147 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
0 commit comments