示例#1
0
def toggle(instr: Instruction):
    if len(instr.operation.operands) == 1:
        if instr.operation.identifier == 'inc':
            instr.operation = ops['dec']
        else:
            instr.operation = ops['inc']
    elif len(instr.operation.operands) == 2:
        if instr.operation.identifier == 'jnz':
            instr.operation = ops['cpy']
        else:
            instr.operation = ops['jnz']
示例#2
0
    def setUp(self):
        def inc_func(arguments: List[str], registers: Dict[str, Any],
                     pc: ProgramCounter) -> ProgramCounter:
            registers[arguments[0]] += 1
            return pc + 1

        inc = Operation('inc', inc_func, [Operand.REGISTER])
        self.program = [
            Instruction(inc, ['a']),
            Instruction(inc, ['a']),
            Instruction(inc, ['b']),
            Instruction(inc, ['a']),
        ]

        self.computer = Computer(registers={'a': 0, 'b': 0})
示例#3
0

ops = {
    'set':
    Operation('set', set_func,
              [Operand.REGISTER, Operand.REGISTER | Operand.CONSTANT]),
    'sub':
    Operation('sub', sub_func,
              [Operand.REGISTER, Operand.REGISTER | Operand.CONSTANT]),
    'mul':
    Operation('mul', mul_func,
              [Operand.REGISTER, Operand.REGISTER | Operand.CONSTANT]),
    'jnz':
    Operation('jnz', jnz_func,
              [Operand.REGISTER, Operand.REGISTER | Operand.CONSTANT])
}

program = []
with open('2017/23/input.txt') as in_file:
    for line in in_file:
        line = line.strip().split()
        if line[0] not in ops:
            continue
        op = ops[line[0]]
        args = line[1:]
        program.append(Instruction(op, args))

registers = {letter: 0 for letter in 'abcdefgh'}
comp1 = Computer(registers=registers, initial_pc=0)
comp1.execute_with_profiler(program)