#!/usr/bin/env python3 # -*- coding: utf-8 -*- from sys import exc_info from time import time start_time = time() ex_input = """Register A: 729 Register B: 0 Register C: 0 Program: 0,1,5,4,3,0 """ real_input = """Register A: 25358015 Register B: 0 Register C: 0 Program: 2,4,1,1,7,5,0,3,4,7,1,6,5,5,3,0 """ def read_input(input_var:str)->tuple[int, int, int, list[int]]: inp = input_var.split("\n") a, b, c = int(inp[0].split(':')[1]), int(inp[1].split(':')[1]), int(inp[2].split(':')[1]) program = list(map(int, inp[3].split(':')[1].split(","))) return a, b, c, program def do(instruction:int, operand:int, point:int): global ra, rb, rc, out if operand in [0, 1, 2, 3]: ope = operand elif operand == 4: ope = ra elif operand == 5: ope = rb elif operand == 6: ope = rc match instruction: case 0: ra = int(ra/(2**ope)) case 1: rb = rb ^ operand case 2: rb = ope % 8 case 3: if ra != 0: point = ope-2 case 4: rb = rb ^ rc case 5: out.append(f"{ope%8}") case 6: rb = int(ra/(2**ope)) case 7: rc = int(ra/(2**ope)) case _: pass return point if __name__ == "__main__": out = [] # ra, rb, rc, prog = read_input(ex_input) ra, rb, rc, prog = read_input(real_input) print(ra, rb, rc, prog) pointer = 0 while pointer < len(prog): pointer = do(prog[pointer], prog[pointer+1], pointer) pointer += 2 print(out) print(",".join(out)) print(f'Runtime: {time()-start_time:.5f} s')