Beispiel #1
0
def _solve_vm(inputs: tuple[str], init_c: int) -> int:
    inss = Assembunny.parse(inputs)
    program = Program(Assembunny.translate(inss))
    program.set_register_value("c", init_c)
    VirtualMachine().run_program(program)
    log(program.registers["a"])
    return program.registers["a"]
Beispiel #2
0
def _run_program(inputs: tuple[str], init_a) -> int:
    inss = Assembunny.parse(inputs)
    output = list()
    program = Program(Assembunny.translate(inss),
                      inf_loop_treshold=6_000,
                      output_consumer=lambda s: output.append(s))
    log(program.instructions)
    program.set_register_value("a", init_a)
    try:
        VirtualMachine().run_program(program)
    except RuntimeError:
        return output
Beispiel #3
0
def _build_program(lines: list[Instruction_]) -> Program:
    log(lines)
    instructions = list[Instruction]()
    for line in lines:
        line.check_valid_operation()
        if line.operation == "nop":
            instructions.append(Instruction.NOP())
        elif line.operation == "acc":
            instructions.append(Instruction.ADD("ACC", line.argument))
        elif line.operation == "jmp":
            instructions.append(Instruction.JMP(line.argument))
    return Program(instructions, inf_loop_treshold=1)
Beispiel #4
0
def _to_program_1(lines: list[Instruction_]) -> Program:
    mask: str
    instructions = list[Instruction]()
    for line in lines:
        if line.command == "mask":
            mask = line.args[0]
        elif line.command == "mem":
            address = int(line.args[0])
            value = _as_binary_string(int(line.args[1]))
            value = _apply_mask_1(value, mask)
            instructions.append(Instruction.MEM(address, value))
    return Program(instructions)
Beispiel #5
0
def _build_program(inss: list[Instruction_]) -> Program:
    def translate_instruction(ins: Instruction_) -> Instruction:
        if ins.operation == "hlf":
            return Instruction.DIV(ins.operands[0], 2)
        elif ins.operation == "tpl":
            return Instruction.MUL(ins.operands[0], 3)
        elif ins.operation == "inc":
            return Instruction.ADD(ins.operands[0], 1)
        elif ins.operation == "jmp":
            return Instruction.JMP(int(ins.operands[0]))
        elif ins.operation == "jie":
            return Instruction.JIE(ins.operands[0], int(ins.operands[1]))
        elif ins.operation == "jio":
            return Instruction.JI1(ins.operands[0], int(ins.operands[1]))
        else:
            raise ValueError("Invalid instruction")

    return Program([translate_instruction(ins) for ins in inss])
Beispiel #6
0
def _to_program_2(lines: list[Instruction_]) -> Program:
    mask: str
    instructions = list[Instruction]()
    for line in lines:
        if line.command == "mask":
            mask = line.args[0]
        elif line.command == "mem":
            value = _as_binary_string(int(line.args[1]))
            address = int(line.args[0])
            address = _as_binary_string(address)
            log(f"add in: {address}")
            address = _apply_mask_2(address, mask)
            log(f"add masked:{address}")
            new_adds = _perm_address(address)
            log(f"new adds: {new_adds}")
            for add in new_adds:
                add = int(add, 2)
                instructions.append(Instruction.MEM(add, value))
    return Program(instructions)