Exemple #1
0
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
Exemple #2
0
def insert_config(adapter, configs: deque):
    for i in range(len(configs)):
        if configs[i][0] == adapter[0]:
            configs[i][1] += adapter[1]
            return
        elif configs[i][0] > adapter[0]:
            configs.insert(i, adapter)
            return
    configs.append(adapter)
Exemple #3
0
def move(l: deque):
    src = l.popleft()
    pickup = [l.popleft() for _ in range(3)]
    # print("pickup", pickup)
    i = find_dst(l, src)
    # print("dst", l[i])
    while pickup:
        l.insert(i + 1, pickup.pop())
    l.append(src)
    return l
Exemple #4
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)
Exemple #5
0
    def insert(self, window: deque, value: int) -> int:
        l, r = 0, len(window) - 1
        while l <= r:
            m = (l + r) // 2

            if window[m] == value:
                window.insert(m, value)
                return
            if window[m] < value:
                l = m + 1
            else:
                r = m - 1
        window.insert(l, value)
Exemple #6
0
 def __defilter(self,
                length: int,
                in_deque: deque,
                filter_dict: dict,
                replace=False):
     ptr = 0
     while ptr < length:
         if ptr in filter_dict:
             if replace:
                 if (in_deque[ptr] != self.CENSOR_CHAR):
                     in_deque[ptr] = filter_dict[ptr]
             else:
                 in_deque.insert(ptr, filter_dict[ptr])
         ptr += 1
Exemple #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
Exemple #8
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
Exemple #9
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
Exemple #10
0
def insert_binary_search(q: deque, e):
    if not q:
        q.append(e)
    elif q[-1] < e:
        q.append(e)
    elif q[0] > e:
        q.insert(0, e)
    else:
        st = 0
        en = len(q)
        while abs(st - en) > 1:
            index = (st + en) // 2
            if q[index] > e:
                en = index
            elif q[index] < e:
                st = index
            else:
                st = index
                en = index

        if abs(st - en) == 1:
            q.insert(en, e)
        else:
            q.insert(st, e)