def hex_eq_len(hex_0: deque, hex_1: deque) -> 'pair deque': delta_len = len(hex_0) - len(hex_1) if delta_len < 0: hex_0.extendleft(['0' for _ in range(-delta_len)]) else: hex_1.extendleft(['0' for _ in range(delta_len)]) return hex_0, hex_1
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
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))
def process_line(content: deque): # macro_symbol = "" level = [] name_table = [] def_table = {} arg_table = {} while content: line = content.popleft() if line["op"] == "MACRO": level.append(line["symbol"]) name_table.append(line["symbol"]) def_table[line["symbol"]] = [] arg_table[line["symbol"]] = {} for idx, arg in enumerate(line["args"].split(",")): arg_table[line["symbol"]][arg] = "" arg_table[line["symbol"]][idx] = arg elif line["op"] == "MEND": level.pop() pass elif level: def_table[level[-1]].append(line) pass elif line["op"] in name_table: macro_lines = def_table[line["op"]] for idx,arg in enumerate(line["args"].split(",")): key = arg_table[line["op"]][idx] arg_table[line["op"]][key] = arg arg_replaced_lines = [] for mline in macro_lines: for arg in arg_table[line["op"]]: if isinstance(arg, str): mline["args"] = mline["args"].replace(arg,arg_table[line["op"]][arg]) arg_replaced_lines.append(mline) content.extendleft(reversed(arg_replaced_lines)) content[0]["symbol"] = line["symbol"] else: outfile.write("{}\t{}\t{}\n".format( line["symbol"], line["op"], line["args"]))
def extend_state(state: collections.deque) -> Tuple[int, collections.deque]: found = False ind = 0 offset = 0 for i in range(5): if state[i] == "#": found = True ind = i break if found: offset = 5-ind state.extendleft("." for _ in range(offset)) found = False ind = 0 sl = len(state) for i in range(sl - 1, sl - 6, -1): if state[i] == "#": found = True ind = i break if found: state.extend("." for _ in range(5-(sl-ind-1))) return offset, state