Example #1
0
def run_program(instructions, x, y):
    # print(f'running run_program {x=}, {y=}')
    global provided_x
    provided_x = False
    relative_base = 0
    memory = copy(instructions)
    instruction_pointer = 0

    while memory[instruction_pointer] != 99 and instruction_pointer >= 0:
        instruction = memory[instruction_pointer]
        opcode, param_modes = parse_instruction(instruction)
        params = get_params(memory, instruction_pointer, opcode, param_modes,
                            relative_base)
        instruction_pointer, relative_base = process_instruction(
            opcode,
            params,
            memory,
            instruction_pointer,
            (x, y),
            param_modes,
            relative_base,
        )

    assert instruction_pointer == -1
    # print(f'returning {relative_base=}')
    return relative_base  # i store it here in this output opcode 4 instruction processing
Example #2
0
def run_program(instructions):
    memory = copy(instructions)
    instruction_pointer = 0
    the_outputs = []

    while memory[instruction_pointer] != 99:
        instruction = memory[instruction_pointer]
        opcode, param_modes = parse_instruction(instruction)
        params = get_params(memory, instruction_pointer, opcode, param_modes)
        instruction_pointer = process_instruction(opcode, params, memory,
                                                  instruction_pointer,
                                                  the_outputs)

    return the_outputs
Example #3
0
def run_program(instructions):
    relative_base = 0
    memory = copy(instructions)  # + ([0] * 2000000)  # doesn't seem to help
    instruction_pointer = 0
    the_outputs = []

    while memory[instruction_pointer] != 99:
        instruction = memory[instruction_pointer]
        opcode, param_modes = parse_instruction(instruction)
        params = get_params(memory, instruction_pointer, opcode, param_modes, relative_base)
        instruction_pointer, relative_base = process_instruction(
            opcode, params, memory, instruction_pointer, the_outputs, param_modes, relative_base
        )

    return memory, the_outputs
Example #4
0
def run_program(instructions):
    relative_base = 0
    memory = copy(instructions)
    instruction_pointer = 0
    the_outputs = []

    while memory[instruction_pointer] != 99:
        instruction = memory[instruction_pointer]
        opcode, param_modes = parse_instruction(instruction)
        params = get_params(memory, instruction_pointer, opcode, param_modes,
                            relative_base)
        instruction_pointer, relative_base = process_instruction(
            opcode,
            params,
            memory,
            instruction_pointer,
            the_outputs,
            param_modes,
            relative_base,
        )
Example #5
0
    def run_program(self, first_input):
        global global_output_val
        while self.memory[self.instruction_pointer] != 99:
            instruction = self.memory[self.instruction_pointer]
            opcode, param_modes = parse_instruction(instruction)
            params = get_params(self.memory, self.instruction_pointer, opcode,
                                param_modes)
            if opcode == 1:
                self.memory[params[2]] = params[0] + params[1]
                self.instruction_pointer += 4
            elif opcode == 2:
                self.memory[params[2]] = params[0] * params[1]
                self.instruction_pointer += 4
            elif opcode == 3:
                self.memory[params[0]] = (first_input if self.provided_phase
                                          else self.phase)
                self.provided_phase = True
                self.instruction_pointer += 2
            elif opcode == 4:
                output_val = self.memory[params[0]]
                self.instruction_pointer += 2
                global_output_val = output_val
                return output_val
            elif opcode == 5:
                if params[0] != 0:
                    self.instruction_pointer = params[1]
                else:
                    self.instruction_pointer += 3
            elif opcode == 6:
                if params[0] == 0:
                    self.instruction_pointer = params[1]
                else:
                    self.instruction_pointer += 3
            elif opcode == 7:
                self.memory[params[2]] = 1 if params[0] < params[1] else 0
                self.instruction_pointer += 4
            elif opcode == 8:
                self.memory[params[2]] = 1 if params[0] == params[1] else 0
                self.instruction_pointer += 4

        return 'Done', global_output_val
 def test_parse_instruction(self):
     self.assertEqual(parse_instruction(1002), (2, [0, 1, 0]))