Exemplo n.º 1
0
def run_intcode(data, id, other_input_queues, stop_queue, nat_queue):

    m = IntCode(data)
    in_queue = other_input_queues[id]

    m.set_input(id)
    m.set_input(-1)

    while True:

        if not stop_queue.empty():
            return

        while not in_queue.empty():
            in_data = in_queue.get()
            m.set_inputs([in_data[0], in_data[1]])

        out = m.get_outputs()
        if len(out) > 0:
            for oi in range(len(out) // 3):
                if out[oi * 3] == 255:
                    nat_queue.put((out[oi * 3 + 1], out[oi * 3 + 2]))
                else:
                    # directly feeds the correct input queue with data!
                    other_input_queues[out[oi * 3]].put(
                        (out[oi * 3 + 1], out[oi * 3 + 2]))
Exemplo n.º 2
0
def part2(memory):
    memory[0] = 2
    cpu = IntCode(memory, 0)
    program = cpu.run()

    score = 0
    tile_map = {0: ' ', 1: '#', 2: '=', 3: '-', 4: 'o'}
    display = [[' ' for _ in range(42)] for _ in range(23)]
    ball_pos = (0, 0)
    paddle_pos = (0, 0)
    while True:
        try:
            x, y, value = next(program), next(program), next(program)
            if x == -1 and y == 0:
                score = value
            else:
                if value == 4:
                    ball_pos = (x, y)
                if value == 3:
                    paddle_pos = (x, y)

                if ball_pos[0] < paddle_pos[0]:
                    cpu.set_input(-1)
                elif ball_pos[0] > paddle_pos[0]:
                    cpu.set_input(1)
                else:
                    cpu.set_input(0)

                display[y][x] = tile_map[value]
                #     print(''.join(row))
                # for row in display:
        except StopIteration:
            break
    return score