Ejemplo n.º 1
0
    def solve_puzzle_two(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(program_str=line)

        for i in range(0, 99):
            for j in range(0, 99):
                processor.reset()
                processor.program[1] = i
                processor.program[2] = j

                processor.run_to_completion()

                if processor.program[0] == 19690720:
                    return (i * 100) + j

        return None
Ejemplo n.º 2
0
    class Springdroid(object):

        instructions = deque()

        def __init__(self, program_str):
            self.processor = IntcodeProcessor(program_str=program_str,
                                              input_func=self._input_func)

        def _input_func(self):
            if len(self.instructions):
                return self.instructions.popleft()
            else:
                return None

        def add_instruction(self, instruction):
            for c in instruction:
                self.instructions.append(ord(c))
            self.instructions.append(ord('\n'))

        def walk(self, debug=False):
            self.add_instruction('WALK')
            return self._execute(debug)

        def run(self, debug=False):
            self.add_instruction('RUN')
            return self._execute(debug)

        def _execute(self, debug):
            self.processor.reset()
            output = ''
            while True:
                out = self.processor.get_next_output()
                if self.processor.last_opcode != 99:
                    if out > 127:
                        if debug:
                            print(output)
                        output = out
                    else:
                        output += chr(out)
                else:
                    return output