Пример #1
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()
        processor = IntcodeProcessor(program_str=line)

        num_blocks = 0
        while processor.last_opcode != 99:
            processor.get_next_output()
            processor.get_next_output()
            v = processor.get_next_output()

            if v == 2:
                num_blocks += 1

        return num_blocks
Пример #2
0
    def _init_processors(self):
        def input_func(idx):
            buffered = queued_values[idx]
            return lambda: buffered.popleft() if len(buffered) else -1

        line = self.load_only_input_line()

        processors = []
        queued_values = [deque() for _ in range(NUM_NODES)]
        for i in range(NUM_NODES):
            processor = IntcodeProcessor(line, input_value=i)
            processor.execute_current_step()
            processor.input_func = input_func(i)
            processors.append(processor)

        return processors, queued_values
Пример #3
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(program_str=line,
                                     input_func=self.input_queue.popleft)

        self._load_instructions()
        output = self._run_until_end(processor, debug=False)

        return ALL_NUMBERS_REGEX.findall(output)[0]
Пример #4
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
Пример #5
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(line)

        white_spots = set()
        painted_spots = set()

        cur_spot = 0, 0

        self._run_robot(processor, cur_spot, white_spots, painted_spots)

        return len(painted_spots)
Пример #6
0
    def solve_puzzle_two(self):
        def determine_input():
            if ball[0] < paddle[0]:
                return -1
            elif ball[0] == paddle[0]:
                return 0
            else:
                return 1

        line = self.load_only_input_line()
        processor = IntcodeProcessor(program_str=line,
                                     input_func=determine_input)
        processor.program[0] = 2

        board = {}
        ball = (0, 0)
        paddle = (0, 0)
        score = 0
        while processor.last_opcode != 99:
            x = processor.get_next_output()
            y = processor.get_next_output()
            v = processor.get_next_output()

            point = x, y

            if point == (-1, 0):
                score = v
            else:
                board[point] = v

            if v == 3:
                paddle = point
            if v == 4:
                ball = point

        return score
Пример #7
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
Пример #8
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(program_str=line)

        processor.program[1] = 12
        processor.program[2] = 2

        processor.run_to_completion()
        return processor.program[0]
Пример #9
0
    def solve_puzzles(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(line)

        grid = self._build_grid(processor)

        start = 0, 0
        end = self._find_end(grid)
        assert end is not None

        ans_one = self._find_shortest_path(grid, start, end)

        ans_two = self._spread_oxygen(grid, end)

        return ans_one, ans_two
Пример #10
0
    def solve_puzzle_two(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(line)

        white_spots = set()
        painted_spots = set()

        cur_spot = 0, 0
        white_spots.add(cur_spot)

        self._run_robot(processor, cur_spot, white_spots, painted_spots)

        bounds = self.Bounds()
        bounds.load(white_spots)
        grid = bounds.build_grid(white_spots)

        return read_output(grid)
Пример #11
0
    def solve_puzzle_two(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(line)

        # Start point picked based on approx guess from first part
        min_affected = 500
        max_affected = 500
        y = 500
        while True:
            min_affected, max_affected = self._get_row_bounds(
                processor, y, min_affected, max_affected)
            if max_affected - min_affected > 100:
                x = max_affected - 99
                output = self._test_pos(processor, x, y + 99)
                if output == 1:
                    return x * 10000 + y
            y += 1
Пример #12
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()

        affected = set()
        min_previous = 0
        processor = IntcodeProcessor(line)
        for x in range(50):
            min_next = None
            found = False
            for y in range(min_previous, 50):
                output = self._test_pos(processor, x, y)
                if output == 1:
                    found = True
                    affected.add((x, y))
                    if min_next is None or min_next > y:
                        min_next = y
                elif found:
                    break
            if min_next is None:
                min_next = 0
            min_previous = min_next

        return len(affected)
Пример #13
0
    def solve_puzzle_one(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(program_str=line, input_value=1)
        processor.run_to_completion()
        return processor.last_output
Пример #14
0
 def _run_initial_execution(self, line, seed, input_value):
     processor = IntcodeProcessor(line, seed)
     processor.stop_after_instruction(3)
     processor.input_value = input_value
     processor.stop_after_instruction(4)
     return processor, processor.last_output
Пример #15
0
    def solve_puzzle_two(self):
        line = self.load_only_input_line()

        processor = IntcodeProcessor(line, input_value=2)
        processor.run_to_completion()
        return processor.last_output
Пример #16
0
 def __init__(self, program_str):
     self.processor = IntcodeProcessor(program_str=program_str,
                                       input_func=self._input_func)