Esempio n. 1
0
File: 13.py Progetto: cymerrad/aoc
def lets_play_a_game(step_time=0.1, show=True):
    # dimensions first
    triples = group_in_triples()
    max_x = max(triples, key=lambda t: t[0])[0]
    max_y = max(triples, key=lambda t: t[1])[1]
    # 45 x 24
    width, height = max_x + 1, max_y + 1
    screen = np.zeros((height, width), dtype=int)
    score = 0

    program = Intcode.parse_program(actual_data)
    program[0] = 2
    machine = Intcode(program)
    time.sleep(1)
    ball_pos_x = 0
    paddle_pos_x = 0
    while machine.running:
        read_in = 0
        for triple in read_greedy(machine):
            read_in += 1

            if triple[0] == -1:
                score = triple[2]
                continue
            if triple[2] == BALL:
                ball_pos_x = triple[0]
            if triple[2] == PADDLE:
                paddle_pos_x = triple[0]

            if show:
                screen[triple[1], triple[0]] = triple[2]

        if show:
            print(display(screen, score))

        move = 0
        if ball_pos_x > paddle_pos_x:
            move = 1
        if ball_pos_x < paddle_pos_x:
            move = -1

        machine.put(move)
        time.sleep(step_time)

    try:
        [final_score] = filter(lambda tr: tr[0] == -1, read_greedy(machine))
        score = final_score[2]
    except ValueError:
        pass

    return machine, score
Esempio n. 2
0
def run_robot(panels):
    state = (complex(0, 0), complex(0, 1))

    def update_state(state, turn):
        new_dir = state[1] * turn
        return (state[0] + new_dir, new_dir)

    robot = Intcode(task_data)

    while robot.running:
        color_underneath = panels[state[0]]

        robot.put(color_underneath)
        color_to_paint = robot.get()
        direction = robot.get()

        turn = LEFT if direction == 0 else RIGHT

        panels[state[0]] = color_to_paint
        state = update_state(state, turn)

    return panels