Ejemplo n.º 1
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})
Ejemplo n.º 2
0
def attempt_execution(program: List[Instruction],
                      swap_instruction: int = -1) -> Tuple[int, bool]:
    comp = Computer({'acc': 0})
    executed_code = set()
    failed = False

    do_swap_instruction(program, swap_instruction)
    while 0 <= comp.pc < len(program):
        if comp.pc in executed_code:
            failed = True
            break
        else:
            executed_code.add(comp.pc)
        comp.execute_instruction(program[comp.pc])
    do_swap_instruction(program, swap_instruction)

    return comp.regs['acc'], failed
Ejemplo n.º 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)
Ejemplo n.º 4
0
    Operation('inc', inc_func, [Operand.REGISTER]),
    'dec':
    Operation('dec', dec_func, [Operand.REGISTER]),
    'jnz':
    Operation('jnz', jnz_func, [
        Operand.REGISTER | Operand.CONSTANT,
        Operand.REGISTER | Operand.CONSTANT
    ]),
    'out':
    Operation('out', out_func, [Operand.REGISTER | Operand.CONSTANT])
}

program = []
with open('2016/25/input.txt') as f:
    for line in f:
        cmd = line.strip().split()
        op = ops[cmd[0]]
        args = cmd[1:]
        program.append(Instruction(op, args))

for i in range(200):
    comp = Computer(registers={'a': i, 'b': 0, 'c': 0, 'd': 0}, initial_pc=0)
    comp.execute(program)
    if clock == [0, 1] * (MAX_CLOCK_LENGTH // 2):
        print('\nFinal answer:', i)
        break
    if i % 10 == 0:
        print('.', end='')
        sys.stdout.flush()
    clock = []
Ejemplo n.º 5
0
    'jnz':
    Operation('jnz', jnz_func, [
        Operand.REGISTER | Operand.CONSTANT,
        Operand.REGISTER | Operand.CONSTANT
    ]),
    'tgl':
    Operation('tgl', tgl_func, [Operand.REGISTER | Operand.CONSTANT])
}

program = []
with open('2016/23/input.txt') as f:
    for line in f:
        cmd = line.strip().split()
        op = ops[cmd[0]]
        args = cmd[1:]
        program.append(Instruction(op, args))

comp1 = Computer(registers={
    'a': puzzle_input_1,
    'b': 0,
    'c': 0,
    'd': 0
},
                 initial_pc=0)
comp1.execute(program)
print('Value in register \'a\' after run 1:', comp1.regs['a'])

# Program performs factorial on input, then adds 93*80=7440
print('Predicted value in register \'a\' after run 2:',
      factorial(puzzle_input_2) + (93 * 80))
Ejemplo n.º 6
0
    if registers[arguments[0]] == 1:
        return pc + int(arguments[1])
    else:
        return pc + 1


ops = {
    'hlf': Operation('hlf', hlf_func, [Operand.REGISTER]),
    'tpl': Operation('tpl', tpl_func, [Operand.REGISTER]),
    'inc': Operation('inc', inc_func, [Operand.REGISTER]),
    'jmp': Operation('jmp', jmp_func, [Operand.CONSTANT]),
    'jie': Operation('jie', jie_func, [Operand.REGISTER, Operand.CONSTANT]),
    'jio': Operation('jio', jio_func, [Operand.REGISTER, Operand.CONSTANT])
}

program = []
with open('2015/23/input.txt') as f:
    for line in f:
        cmd = line.replace(',', '').strip().split()
        op = ops[cmd[0]]
        args = cmd[1:]
        program.append(Instruction(op, args))

comp1 = Computer(registers={'a': 0, 'b': 0}, initial_pc=0)
comp2 = Computer(registers={'a': 1, 'b': 0}, initial_pc=0)

comp1.execute(program)
print('Value in register b after run 1:', comp1.regs['b'])
comp2.execute(program)
print('Value in register b after run 2:', comp2.regs['b'])
Ejemplo n.º 7
0
        print(regs['_sound'])
        return -1
    return pc+1

def jgz_func(args:ArgList,regs:RegisterDict,pc:ProgramCounter) -> ProgramCounter:
    if read_value(args[0]) > 0:
        return pc + read_value(args[1])
    else:
        return pc + 1
    
ops = {
    'snd': Operation('snd',snd_func,[Operand.REGISTER]),
    'set': Operation('set',set_func,[Operand.REGISTER,Operand.REGISTER|Operand.CONSTANT]),
    'add': Operation('add',add_func,[Operand.REGISTER,Operand.REGISTER|Operand.CONSTANT]),
    'mul': Operation('mul',mul_func,[Operand.REGISTER,Operand.REGISTER|Operand.CONSTANT]),
    'mod': Operation('mod',mod_func,[Operand.REGISTER,Operand.REGISTER|Operand.CONSTANT]),
    'rcv': Operation('rcv',rcv_func,[Operand.REGISTER]),
    'jgz': Operation('jgz',jgz_func,[Operand.REGISTER,Operand.REGISTER|Operand.CONSTANT])
}

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

comp1 = Computer(registers={'a':0,'b':0,'f':0,'i':0,'p':0},initial_pc=0)
comp1.execute(program)