23.py
1 from lib import * 2 3 input = read_input(2015, 23) 4 5 program = input.splitlines() 6 7 pc = 0 8 registers = {"a": 0, "b": 0} 9 while pc < len(program): 10 cmd, args = program[pc].split(maxsplit=1) 11 args = args.split(", ") 12 if cmd == "hlf": 13 registers[args[0]] //= 2 14 pc += 1 15 elif cmd == "tpl": 16 registers[args[0]] *= 3 17 pc += 1 18 elif cmd == "inc": 19 registers[args[0]] += 1 20 pc += 1 21 elif cmd == "jmp": 22 pc += int(args[0]) 23 elif cmd == "jie": 24 pc += int(args[1]) if registers[args[0]] % 2 == 0 else 1 25 elif cmd == "jio": 26 pc += int(args[1]) if registers[args[0]] == 1 else 1 27 else: 28 break 29 30 print(registers["b"]) 31 32 33 pc = 0 34 registers = {"a": 1, "b": 0} 35 while pc < len(program): 36 cmd, args = program[pc].split(maxsplit=1) 37 args = args.split(", ") 38 if cmd == "hlf": 39 registers[args[0]] //= 2 40 pc += 1 41 elif cmd == "tpl": 42 registers[args[0]] *= 3 43 pc += 1 44 elif cmd == "inc": 45 registers[args[0]] += 1 46 pc += 1 47 elif cmd == "jmp": 48 pc += int(args[0]) 49 elif cmd == "jie": 50 pc += int(args[1]) if registers[args[0]] % 2 == 0 else 1 51 elif cmd == "jio": 52 pc += int(args[1]) if registers[args[0]] == 1 else 1 53 else: 54 break 55 56 print(registers["b"])