예제 #1
0
파일: aoc23.1.py 프로젝트: zafodB/aoc2020
def make_move(input_circle: deque, current_cup: int) -> deque:

    print(f'Current circle: {input_circle}')

    current_cup_index = input_circle.index(current_cup)

    pick_three = []

    for i in range(3):
        pick_index = i + current_cup_index + 1
        if pick_index > 8:
            pick_index -= 9

        pick_three.append(input_circle[pick_index])

    for cup in pick_three:
        input_circle.remove(cup)

    destination_cup = current_cup - 1
    if destination_cup < 1:
        destination_cup += 9

    while destination_cup in pick_three:
        destination_cup -= 1

        # wrap around
        if destination_cup < 1:
            destination_cup += 9

    destination_cup_index = input_circle.index(destination_cup)
    for cup in reversed(pick_three):
        input_circle.insert(destination_cup_index + 1, cup)

    return input_circle
예제 #2
0
def addition_then_multiplication_solver(stack: deque):
    while '+' in stack:
        stack.rotate(-stack.index('+'))
        stack.popleft()
        stack.appendleft(stack.popleft() + stack.pop())

    return reduce((lambda x, y: x * y), (n for n in stack if n != '*'))
예제 #3
0
def play_cups(cups: deque, nb_moves):
    for i in range(0, nb_moves):
        #print(f'-- move {i+1} --')
        current_cup = cups[0]

        # print(cups)
        # print(f'current: {current_cup}')

        cups.rotate(-1)

        pickups = [cups.popleft(), cups.popleft(), cups.popleft()]
        destination_cup = get_destination(current_cup, cups)

        # print(f'pick up: {pickups}')
        # print(f'destination: {destination_cup}')

        dest_in = cups.index(destination_cup)

        # push dest to end right end of the queue
        # and add pickups to the right
        cups.rotate(-dest_in - 1)
        cups.extend(pickups)

        # rotate the queue back into initial state shifted once
        cups.rotate(dest_in + 4)
        # print('')

    return cups
예제 #4
0
def get_destination_cup(arr: deque, pick_up_values: List,
                        target_label: int) -> int:
    while True:
        target_label -= 1
        if target_label <= 0:
            target_label = max_target_label
        if target_label not in pick_up_values:
            return arr.index(target_label)
예제 #5
0
def part1(input: deque):
    min_cup, max_cup = min(input), max(input)
    for i in range(100):
        current = max_cup if input[0] == min_cup else input[0] - 1
        input.rotate(-1)
        one, two, three = input.popleft(), input.popleft(), input.popleft()
        while current == one or current == two or current == three:
            current = max_cup if current == min_cup else current - 1

        destination = input.index(current) + 1

        input.rotate(-1 * destination)
        input.extendleft((three, two, one))
        input.rotate(destination)

    destination = input.index(1)
    input.rotate(-1 * destination)
    input.popleft()
    return "".join(map(str, input))
예제 #6
0
def iteration(p: deque, c_pos: int):
    c_pos %= len(p)
    p.rotate(-c_pos)
    sel = []
    for _ in range(3):
        v = p[1]
        sel.append(v)
        p.remove(v)
    d_value = calc_dest_value(p, sel, p[0])
    d_pos = p.index(d_value) + 1
    for i in range(3):
        p.insert(d_pos + i, sel[i])
    p.rotate(c_pos)
예제 #7
0
def crab_move(cups: deque) -> deque:
    """Performs a single crab move. Each move, the crab does the following actions:
    1) The crab picks up the three cups that are immediately clockwise of the
       current cup. They are removed from the circle; cup spacing is adjusted as
       necessary to maintain the circle.
    2) The crab selects a destination cup: the cup with a label equal to the
       current cup's label minus one. If this would select one of the cups that
       was just picked up, the crab will keep subtracting one until it finds a
       cup that wasn't just picked up. If at any point in this process the value
       goes below the lowest value on any cup's label, it wraps around to the
       highest value on any cup's label instead.
    3) The crab places the cups it just picked up so that they are immediately
       clockwise of the destination cup. They keep the same order as when they
       were picked up.
    4) The crab selects a new current cup: the cup which is immediately clockwise
       of the current cup.

    **Note 1: the input `cups` should always be structured such that the
      "current cup" is positioned at index -1 (i.e. a "reversed" list).
    **Note 2: deque is preferred over lists in the cases where we need quicker
      append and pop operations from both the ends of the container; deque provides
      an O(1) time complexity for append and pop operations as compared to
      lists, which provide O(n) time complexity (source: GeeksForGeeks).
    """
    # action 4  (**note: we do it here so we can do pop() immediately below)
    cups.rotate(1)  # shift the current cup to the end of the queue

    # action 1
    three_cups = [cups.pop() for _ in range(3)]

    # action 2
    destination_cup = cups[0] - 1
    while destination_cup in three_cups or destination_cup < 1:
        destination_cup -= 1
        if destination_cup < 0:
            destination_cup = max(cups)

    destination_cup_idx = cups.index(destination_cup)

    # action 3
    cups.insert(destination_cup_idx, three_cups[0])
    cups.insert(destination_cup_idx, three_cups[1])
    cups.insert(destination_cup_idx, three_cups[2])

    return cups
예제 #8
0
def perform_move(cups: deque):
    min_cup, max_cup = min(cups), max(cups)
    current = cups[0]
    cups.rotate(-1)
    picked_up = [cups.popleft() for _ in range(3)]

    destination = current - 1
    while destination in picked_up or destination < min_cup:
        if destination < min_cup:
            destination = max_cup
        else:
            destination -= 1

    position = cups.index(destination) + 1
    cups.rotate(-position)
    cups.extendleft(picked_up[::-1])
    cups.rotate(position)
    return cups
예제 #9
0
def check_peak_diff(data: deque, min_distance, max_distance, count_1, count_2):
    index_max = data.index(max(data))
    index_min = data.index(min(data))
    count_1_index = 0
    count_2_index = 0

    if min_distance <= max(data) - min(data) <= max_distance:
        if index_max > index_min:
            count_1_index = index_max
            for index in range(index_min, index_max + 1):
                data[index] = np.nan
            count_1 += 1
        else:
            count_2_index = index_min
            for index in range(index_max, index_min + 1):
                data[index] = np.nan
            count_2 += 1

    return data, count_1, count_2, count_1_index, count_2_index
예제 #10
0
def crab_moves_cups(cups: deque, rounds=10):
    current = cups[0]
    mini = min(cups)
    maxi = max(cups)
    l = len(cups)
    init_time = time.time()
    for turn in range(rounds):
        if turn % 500 == 0:
            duration = time.time() - init_time
        current = cups.popleft()
        picked = [cups.popleft() for _ in range(3)]
        cups.appendleft(current)
        destination = current - 1
        while destination in picked or destination < mini:
            destination -= 1
            if destination < mini:
                destination = maxi
        # THIS is slow: searching a value in a deque
        destination_index = cups.index(destination) + 1
        for add in reversed(picked):
            cups.insert(destination_index, add)
        # The crab selects a new current cup: the cup which is immediately clockwise of the current cup.
        cups.rotate(-1)  # this places the new current cup at the beginning of the deque
    return cups
예제 #11
0
def play_game_one(cups: deque, n_moves: int) -> deque:
    for _ in range(n_moves):
        current_cup_value = cups[0]
        cups.rotate(-1)
        pick_up = deque()
        for _ in range(3):
            pick_up.append(cups.popleft())

        destination_cup = None
        destination_cup_value = current_cup_value - 1
        while True:
            if destination_cup_value < min(cups):
                destination_cup_value = max(cups)
            try:
                destination_cup = cups.index(destination_cup_value)
                break
            except ValueError:
                destination_cup_value -= 1
                continue

        for i in range(3):
            cups.insert((destination_cup + i + 1) % len(cups), pick_up[i])

    return cups
예제 #12
0
def sel_loesung1(p: deque):
    inx_1 = p.index(1)
    p.rotate(-inx_1)
    p.popleft()
    return p
예제 #13
0
def get_result(arr: deque) -> str:
    pos_one = arr.index(1)
    return ''.join([str(arr[(pos_one + 1 + i) % 9]) for i in range(8)])
예제 #14
0
def partner(deque_: deque, a, b):
    exchange(deque_, deque_.index(a), deque_.index(b))
    return deque_