|
| 1 | +import sys |
| 2 | +from itertools import permutations |
| 3 | + |
| 4 | +data = open(sys.argv[1]).read() |
| 5 | + |
| 6 | +lines = data.split("\n") |
| 7 | + |
| 8 | +instructions = [line.split() for line in lines] |
| 9 | + |
| 10 | +regs = {'a': 0, 'b': 0} |
| 11 | + |
| 12 | +i = 0 |
| 13 | +while i < len(instructions): |
| 14 | + instr = instructions[i][0] |
| 15 | + if instr in ('hlf', 'tpl', 'inc'): |
| 16 | + reg = instructions[i][1] |
| 17 | + match instr: |
| 18 | + case 'hlf': regs[reg] //= 2 |
| 19 | + case 'tpl': regs[reg] *= 3 |
| 20 | + case 'inc': regs[reg] += 1 |
| 21 | + i += 1 |
| 22 | + elif instr == 'jmp': |
| 23 | + i += int(instructions[i][1]) |
| 24 | + elif instr in ('jie', 'jio'): |
| 25 | + reg_val = regs[instructions[i][1][0]] |
| 26 | + jmp = int(instructions[i][2]) |
| 27 | + if instr == 'jie' and reg_val % 2 == 0: |
| 28 | + i += jmp |
| 29 | + elif instr == 'jio' and reg_val == 1: |
| 30 | + i += jmp |
| 31 | + else: |
| 32 | + i += 1 |
| 33 | + |
| 34 | +print(regs['b']) |
| 35 | + |
| 36 | +regs = {'a': 1, 'b': 0} |
| 37 | + |
| 38 | +i = 0 |
| 39 | +while i < len(instructions): |
| 40 | + instr = instructions[i][0] |
| 41 | + if instr in ('hlf', 'tpl', 'inc'): |
| 42 | + reg = instructions[i][1] |
| 43 | + match instr: |
| 44 | + case 'hlf': regs[reg] //= 2 |
| 45 | + case 'tpl': regs[reg] *= 3 |
| 46 | + case 'inc': regs[reg] += 1 |
| 47 | + i += 1 |
| 48 | + elif instr == 'jmp': |
| 49 | + i += int(instructions[i][1]) |
| 50 | + elif instr in ('jie', 'jio'): |
| 51 | + reg_val = regs[instructions[i][1][0]] |
| 52 | + jmp = int(instructions[i][2]) |
| 53 | + if instr == 'jie' and reg_val % 2 == 0: |
| 54 | + i += jmp |
| 55 | + elif instr == 'jio' and reg_val == 1: |
| 56 | + i += jmp |
| 57 | + else: |
| 58 | + i += 1 |
| 59 | + |
| 60 | +print(regs['b']) |
0 commit comments